0% found this document useful (0 votes)
2 views

C language assignment

The document contains a series of programming assignments focused on using while loops, functions, and arrays in various tasks. These tasks include calculating sums, factorials, printing series, and implementing basic arithmetic operations, among others. Additionally, it includes exercises on error handling, input validation, and array manipulation.

Uploaded by

snehasingh270804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C language assignment

The document contains a series of programming assignments focused on using while loops, functions, and arrays in various tasks. These tasks include calculating sums, factorials, printing series, and implementing basic arithmetic operations, among others. Additionally, it includes exercises on error handling, input validation, and array manipulation.

Uploaded by

snehasingh270804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

While loop Assignments (Basic Level)

1) Write a program to read n numbers from the keyboard and find their sum.
Example:
Enter n number: 3

Enter 3 numbers: 10
20
10
Sum: 40
2) Write a program to read a number n, and print sum of all the numbers from 1 to n.
Example:
Enter n number: 3
Sum of all numbers from 1 to 3 is 6

3) Write a program to read a number n and print the factorial of n.


Example:
Enter n number: 4
Factorial of 4 is 24

4) Write a program to print the value of below series:


Example:
Enter n number: 3
1 - 1/2 + 1/3

Generic formula: 1 - 1/2 + 1/3 - 1/4 .... + 1/n etc

5) Write a program to multiply two numbers without using * operator,


Example:
Enter two numbers:
5
3
Result is 15.

6) Write a program to find the power of a number to the given number.


eg.,input : 3 5
output: 3 to the power of 5 -> 243 Write the following programs, using while loop, with
counting based logic :
1) Read n number of numbers from user (n value taken as input) and print if each number is
even or odd.
Example:
Enter n number: 2
Enter x value: 5
The given number 5 is ODD.
Enter x value: 4
The given number 4 is EVEN.

2) Read n characters from user, and print if each character is vowel or not.
Example:
Enter n characters: 2
Enter a character: b
The given character 'b' is not a vowel.
Enter a character: i
The given character 'i' is a vowel.

3) Read n characters from user and print if each character is a capital alphabet, small
alphabet, numeric character or special character.
Example:
Enter n characters: 3
Enter a character: b
The given character 'b' is a small alphabet.
Enter a character: ?
The given character '? ' is a special character.
Enter a character: B
The Giiven character 'B ' is a Capital alphabet.

4) Read n numbers from the user, and while reading every number, print if the number is bigger
or smaller than the previous number. For the first number, there won't be any output as it is the
first one.
Example:
Enter n number: 3
5
6
The given number is bigger than the previous number.
2
The given number is smaller than the previous number.
5) Read n numbers in ascending order. If a number entered is bigger than the previous number,
then count it. If it is smaller, then don't count it, instead let the loop repeat itself and read
another number.
Example:
Enter n number: 5
Enter 5 numbers:
3
4
2
6
8
The count is 3
6) Read n numbers from the user ,and print the smallest number of all.
Example:
Enter n number: 5
Enter 5 numbers:
3
4
2
6
8
The smallest number is 2.
7) Read a number from the user, and print its multiplication table upto 10 multiples.
Example:
Enter n number: 2
Enter x number: 2
2*1 =2
2*2 =4
2*3 =6
2*4 =8
2*5 =10
2*6 =12
2*7 =14
2*8 =16
2*9 =18
2*10 =20
Enter x number: 5
5*1 =5
5*2 =10
5*3 =15
5*4 =20
5*5 =25
5*6 =30
5*7 =35
5*8 =40
5*9 =45
5*10 =50

8) For n students from a class, read 6 subject marks for every student, and calculate their
percentage of marks, and print who is the topper of all (print the student number).
Example:
Enter n students: 2
Enter 1st student 6 subjects marks:
40
50
45
75
60
55
Student 1 percentage is: 54.16%
Enter 2nd student 6 subjects marks:
45
65
55
80
40
55
Student 2 percentage is: 56.66%
Topper student ID is 2

9) Read two numbers from the user, and print all serial numbers between those numbers.
eg., inputs: 10 20
output: 10 11 12 13 14 15 16 17 18 19 20

10) write the calculator program to read two numbers and one character (+, -, *, / , %) from the
user, and based on character, do appropriate operations on numbers and print the output.
Modify the program to repeat this task n number of times.
Example:
Enter n number: 2
Enter two numbers:
5
4
Enter a character (+,-,*,/,%):
+
Result of 5 + 4 is 9
Enter two numbers:
4
3
Enter a character (+,-,*,/,%):
-
Result of 4 - 3 is 1

11) Read two numbers from the user, and print all odd numbers between those numbers and
then all even numbers.

eg., inputs: 10 20
output:
even numbers: 10 12 14 16 18 20
odd numbers: 11 13 15 17 19

12) Read two numbers from the user, and print all numbers in reverse order.

eg., inputs: 10 20
output:
20 19 18 17 16 15 14 13 12 11 10

1 using while ,do while and for loops :


*
**
***
****
*****
2 using while ,do while and for loops :
*****
****
***
**
*

3 using while ,do while and for loops :


*****
****
​ ***
​ **
​ *

4 using while ,do while and for loops :


​ *
​ **
​ ***
****
*****

5 using while ,do while and for loops :


​ *
​ * *
​ * * *
* * * *
* * * * *

6 1
22
333
4444
55555
7 1
12
123
1234
12345

8 1
01
101
0101
10101

9 5
44
333
2222
11111

10 5
54
543
5432
54321

11 1
2​ 3
4​ 5 6
7​ 8 9​ 10
11 12 13 14 15

12 without using nested loops :


*
**
***
****
*****

13 ​ *
​ ***
​ *****
*******
*********
*******
​ *****
​ ***
​ *
14) Write a program to print all prime numbers between 2 given integers.

eg., input: 10 20
prime numbers between 10 & 20 are: 11 13 17 19
Write the below programs by writing a separate function and calling it in the main function
and implement proper ERROR Handling wherever necessary.

1) Write a function to calculate simple interest. Call it in main function with appropriate
inputs and print the total amount the user will get after the tenure (principle + interest)

2) Write a function that takes two numbers, a and n as input arguments and returns the
value of a to the power of n.

3) Write a function that takes two numbers a and b as input arguments and returns their
product as return value , without using * operator.

4) Write a function that takes two numbers a and b, and returns the quotient after dividing a
with b.

5) Write a function that takes two numbers a and b, and returns the remainder after dividing
a with b.

6) Write a function that takes an integer number as input and prints its multiplication
table.return type is void.

//when different outputs need to be returned for different conditions,you can store the
value in a variable for different conditions, and return the variable at the end. for example
if you have to return 1 or 0 for prime / not prime conditions, you can store the value in a
variable.

// or you can write different return statements in different conditions. only one of them will
execute. we will discuss more on this tomorrow.

7) write a function that can take an integer as input and return 1 if the number is prime
number, return 0 if it is not prime and print appropriate output message in main according
to output.
return type is integer. function name IsPrime - returns int (0 or 1)
8) Write a function to determine if a character is alphanumeric or not and print the
appropriate output in main function. (return 1 if it is alphanumeric, 0 if it is not alpha
numeric).
Hint: int fun_alpha_num(char c).

9) Write a calculator program with 5 functions below to do the operations of addition,


subtraction, multiplication, division for quotient and division for remainder.
1) int add (int a, int b) // to be called when user chooses +
2) int sub (int a,int b) // to be called when user chooses -
3) int mul(int a, int b) // to be called when user chooses *
4) int quotient(int a, int b) // to be called when user chooses /
5) int remainder(int a,int b) // to be called when user choose%

10) Write a function to accept a year as input and return 1 if the year is a leap year,
otherwise 0.

11) Write a function to accept a month and year as input, and return the number of days in
that month as output. print the number of days in main.

12) Write a function ,that can take two integers, swap their values and print their new
values. return type should be void. function should print the values.

13) Write a function that takes 2 dates as inputs, and returns 1 if the first date is old, 2 if the
second date is old.

14) Write a function that takes a date as input, and returns 1 if the date is valid, 0 if the date
is invalid.
Array

1 Write the following program :

Declare an integer array of size 100.

Assign numbers 1-100 to each element of the array.

Print all the numbers.

Print all even elements.

Print all odd elements.

Add also 5 to each element and print 6 – 105 numbers.

2 Write the following program :

Declare a character array without size.

Initialize the array with characters of your name.

Print your name using for loop.

3 Declare an array of size 10. Enter 10 elements and save them in the array. Print the array .
Find out the biggest and smallest numbers and their indexes and print.

4 Try experimenting with the below array syntaxes and see the results,check their sizes, try to
also read values for elements and print them:</br>

int arr[5] = {};

int arr[10] = {2.4,5.6,7.3};

int arr[3] = {1,2,3,4,5};

int arr[0] = {};

int arr[0] = {1,2,3,4,5};

int arr[] = {};


int arr[‘a’];

5 Write the following program.

Declare an integer variable ‘size’.

Take the value of ‘size’ from user using scanf.

Declare an array of any datatype, and give the integer variable ‘size’, in place of size of the
array and initialize it to some values.(syntax: float array[size] = {1.0,2.5};)

Using for loop, try to take user input using scanf. Loop should execute ‘size’ number of times.

Using for loop, print the elements of the array.

6 Write a function to search for a specific element given by the user, in an array and display its
index. Function takes array and element to search as inputs, and returns the index as output. If
element not found, return -1.

7 Write a program to print an array in reverse order.

8 Write a program to reverse an array and print it (Reverse the array by swapping both edges of
the array and move inside and repeat till middle of the array):

Input : array1 ={1,2,3,4,5};

Output : array1 = {5,4,3,2,1};

9 Write a program to print an integer in binary format using arrays.

10 Write a program to swap two arrays in reverse order.

Example:

input :

array1 = {1,2,3,4,5};

array2 = {6,7,8,9,10};
output:

array1 : {10,9,8,7,6}

array2: { 5,4,3,2,1}

11 Write a function to return the average of marks of all students in a class.

12 Write a function to modify the array such that all negative numbers are converted to positive.

13) write a function to take 2 arrays as input and return 1 if they are equal and 0 if they are not
equal ( sizes to be sent as seperate arguments)

14) write a function to take an array as input, and count how many times the biggest number is
repeated in that array, and return the count.

eg., array : {10, 14,16,10, 10 , 16, 14, 14, 16, 16};

output : biggest number is repeated 4 times

Functions

Write the below programs by writing a separate function and calling it in the main function
and implement proper ERROR Handling wherever necessary.

1) Write a function to calculate simple interest. Call it in main function with appropriate
inputs and print the total amount the user will get after the tenure (principle + interest)

2) Write a function that takes two numbers, a and n as input arguments and returns the
value of a to the power of n.

3) Write a function that takes two numbers a and b as input arguments and returns their
product as return value , without using * operator.

4) Write a function that takes two numbers a and b, and returns the quotient after dividing a
with b.
5) Write a function that takes two numbers a and b, and returns the remainder after dividing
a with b.

6) Write a function that takes an integer number as input and prints its multiplication
table.return type is void.

//when different outputs need to be returned for different conditions,you can store the
value in a variable for different conditions, and return the variable at the end. for example
if you have to return 1 or 0 for prime / not prime conditions, you can store the value in a
variable.

// or you can write different return statements in different conditions. only one of them will
execute. we will discuss more on this tomorrow.

7) write a function that can take an integer as input and return 1 if the number is prime
number, return 0 if it is not prime and print appropriate output message in main according
to output.
return type is integer. function name IsPrime - returns int (0 or 1)

8) Write a function to determine if a character is alphanumeric or not and print the


appropriate output in main function. (return 1 if it is alphanumeric, 0 if it is not alpha
numeric).
Hint: int fun_alpha_num(char c).

9) Write a calculator program with 5 functions below to do the operations of addition,


subtraction, multiplication, division for quotient and division for remainder.
1) int add (int a, int b) // to be called when user chooses +
2) int sub (int a,int b) // to be called when user chooses -
3) int mul(int a, int b) // to be called when user chooses *
4) int quotient(int a, int b) // to be called when user chooses /
5) int remainder(int a,int b) // to be called when user choose%

10) Write a function to accept a year as input and return 1 if the year is a leap year,
otherwise 0.

11) Write a function to accept a month and year as input, and return the number of days in
that month as output. print the number of days in main.
12) Write a function ,that can take two integers, swap their values and print their new
values. return type should be void. function should print the values.

13) Write a function that takes 2 dates as inputs, and returns 1 if the first date is old, 2 if the
second date is old.

14) Write a function that takes a date as input, and returns 1 if the date is valid, 0 if the date
is invalid.

Array Assis

1 Write the following program :

Declare an integer array of size 100.

Assign numbers 1-100 to each element of the array.

Print all the numbers.

Print all even elements.

Print all odd elements.

Add also 5 to each element and print 6 – 105 numbers.

2 Write the following program :

Declare a character array without size.

Initialize the array with characters of your name.

Print your name using for loop.

3 Declare an array of size 10. Enter 10 elements and save them in the array. Print the array .
Find out the biggest and smallest numbers and their indexes and print.

4 Try experimenting with the below array syntaxes and see the results,check their sizes, try to
also read values for elements and print them:</br>
int arr[5] = {};

int arr[10] = {2.4,5.6,7.3};

int arr[3] = {1,2,3,4,5};

int arr[0] = {};

int arr[0] = {1,2,3,4,5};

int arr[] = {};

int arr[‘a’];

5 Write the following program.

Declare an integer variable ‘size’.

Take the value of ‘size’ from user using scanf.

Declare an array of any datatype, and give the integer variable ‘size’, in place of size of the
array and initialize it to some values.(syntax: float array[size] = {1.0,2.5};)

Using for loop, try to take user input using scanf. Loop should execute ‘size’ number of times.

Using for loop, print the elements of the array.

6 Write a function to search for a specific element given by the user, in an array and display its
index. Function takes array and element to search as inputs, and returns the index as output. If
element not found, return -1.

7 Write a program to print an array in reverse order.

8 Write a program to reverse an array and print it (Reverse the array by swapping both edges of
the array and move inside and repeat till middle of the array):

Input : array1 ={1,2,3,4,5};

Output : array1 = {5,4,3,2,1};

9 Write a program to print an integer in binary format using arrays.


10 Write a program to swap two arrays in reverse order.

Example:

input :

array1 = {1,2,3,4,5};

array2 = {6,7,8,9,10};

output:

array1 : {10,9,8,7,6}

array2: { 5,4,3,2,1}

11 Write a function to return the average of marks of all students in a class.

12 Write a function to modify the array such that all negative numbers are converted to positive.

13) write a function to take 2 arrays as input and return 1 if they are equal and 0 if they are not
equal ( sizes to be sent as seperate arguments)

14) write a function to take an array as input, and count how many times the biggest number is
repeated in that array, and return the count.

eg., array : {10, 14,16,10, 10 , 16, 14, 14, 16, 16};

output : biggest number is repeated 4 times

pointers Assi

1 Write a program to print the address of a variable , its value ,its size, try different
executions and note the address printed. Assign the address of the variable to a pointer
variable.Print the size of pointer and its indirected value (apply * operator).
2 Write a program to add two variables using their pointers.

3 Write a program to take input for two character variables using pointers and find out
which character has higher ascii value.

4 Declare 3 integer variables. Declare an integer pointer. Assign the address of each
variable to the pointer in succession and print the value of the variable using indirection
operator on the pointer.

5 Declare 3 pointer variables of integer type, and an integer variable. Assign the address of
integer variable to the 3 pointers. Print the variable value using indirection on each pointer
variable. Change the variable value directly and check the values of each of the pointers
using indirection. Change the variable value , using each pointer and print the variable value
and indirected values of all pointers every time you change.

6 declare different pointers with different data types. Print the sizes of the pointer variables
using sizeof operator. Why do u think all of them are giving same size irrespective of the
data type ?

7 Write a program to find the biggest of three numbers using pointers that point to those
numbers.

8 Take three input integers x,y and z. Write a program to rotate their values such that, x has
the value of y, y has the value of z and z has the value of x. Do this using pointers that point
to x,y and z.

9 Declare an integer array of size 10 and initialize it to some values. Print the addresses of
each element of the array using a pointer. using indirection operator , print the value stored
in each element of the array.

Call By value & Call by References

1) Write a program to swap two numbers using Call by Value and Call by Reference.
2) Note down the differences between Call by Value and Call by Reference and when to use
what.
3) Write a function that can rotate the values of three variables. print the results in the main
function.
4) Write a function that can take two integers as input, and gives 5 outputs : addition,
subtraction, multiplication, quotient and reminder of those two numbers. Print the outputs in
the main function.
5) "Write a function that communicates with main using a single static variable without
taking any input arguments.
Everytime function returns something using the static variable,after using it, main sends
another input using the same variable and calls the function again.
eg., print the square of each number of an array :
for each number of the array :
call the function
main gets the static variable address as return value from function.
main puts the array element in static variable.
in the function :
create static variable.
if static variable value is not zero, print its square.
function sends static variable address back to main."
6) Write two source files, main.c and swap.c. The main function initializes a two-element
array of ints, and then calls the swap function to swap the pair.

Preprocessor directive Lab Assignments

1) Write down a macro to find out the biggest of two numbers.


MAX(x,y) should define code to find biggest of x and y. Implement array sorting function
using this macro (Bubble sort)

2) Write down a macro to find the biggest of four numbers using biggest of 2 macro.

3) WAP to print if a character is alphanumeric or special character, using macro


conditions

4) Define a macro that receives an array and the number of elements in the array as
arguments. Write a program using this macro to print out the elements of the array. Try
using this macro for different data types of arrays.

5) Define a generic function, for different types of array printing , by taking array and
array size as arguments.

6) define a macro to generate swapping function for int, float , double and character
datatypes, as a generic function using macros.
7) Read Pre-processor directives complete chapter and complete textbook exercises in
notebook and dont execute exercises directly in laptop.

C Basics Seminar List

1 Type of programming Languages


2 Explain GCC Compilation stages and how to stop each stage?
3 character set and escape sequence
4 Keywords, Identifiers , Constants and Data types
5 variables , Variable declaration and variable Initialization
6 Arithmetic Operators & Increment, Decrement Operators
7 Assignment Operators
8 Conditional Operator
9 Comma Operator
10 Sizeof() operator
11 Bitwise Operators
12 Block statements: Simple if, if else, else if ladder
13 SWITCH statement
14 Loops: while , do while, for loop
15 Jump statements: break,goto, continue
16 Functions: fun declaration, fun call,fun defination
Differentiate Library functions and User defined functions . Implementation of our
17 own header file
18 Explaination of auto and register storage classes with memory layout
19 Explaination of static storage class with memory layout
20 Explaination of extern storage class with memory layout
21 Arrays : Declaration of Array, Accessing of 1-D array, Processing of 1-D array
22 Intialization of 1-D array
23 sharing array as an argument in a function
24 string literals, string variables
25 string library functions with examples
26 pointers introduction
27 pointers concept and Call by value and Call by reference
28 Explain Compilation stages in detail
29 preprocessive Directives

Bitwise Operators Lab Assignments

Write a program to Set, clear and toggle a particular bit using bit wise operator?
2. WAP whether a number is ODD or EVEN using bitwise.
3. Write a printbinary(int , int ) function consists of 2 integer variables. First one is the
value of the variable and the second one is the size of the variable.
Example:
char x=5;
printbinary(x,sizeof(x));
output: 00000101
4. WAP to count the bits set (bit value 1 ) in an integer? Find out and compare different
possibilities?
5. WAP whether a number is a power of 2 or not?
6. WAP implements subtraction functionality without using SUB('-') Operator.
7. WAP implements addition functionality without using ADD('+') Operator.
8. WAP implements XOR functionality without using XOR(^) operator.
9. WAP to implement the sizeof operation using the bitwise operator.
10. WAP to convert Little endian integer to Big endian integer
11. WAP multiply a number by 9 using bit shift.
12. WAP Check if the 20th bit of a 32-bit integer is on or off?
13. Write a functionsetbits(x,p,n,y)that return x with then bits that begin at position p set to
the rightmost n bits of y, leaving the other bits unchanged.
e.g. if x = 10101010 (170 decimal) and y = 10100111 (167 decimal) and n = 3 and p = 6
then
you need to strip off 3 bits of y (111) and put them in x at position 10xxx010 to get
answer
10111010. Your answer should print out the result in binary form although input can be
in
decimal form.
Your output should be like this:
x = 10101010 (binary)
y = 10100111 (binary)
setbits n = 3, p = 6 gives x = 10111010 (binary).
14. WAP Swap any two numbers using bitwise operators. How does it work?

11 of 290 2 Dimensional Arrays Assignments

1 Declare a two-dimensional array of elements for sales details of a


store, for each item, for each salesman. Take the sales values as
input and print the array in matrix form.
Find out and print the following :
1 ) Total sales by each sales man
2 ) Total sales for a item
3 ) Total sales
2 Write the following program :
Declare a two dimensional array of elements and find and print its
transpose.
eg., if the matrix is : ​ 13
​ 45
​ 78

its transpose should be : ​ 1 4 7


​ 358

3 Write a program for matrix addition and subtraction.


4 Write a program for matrix multiplication.
5 Search for an element in a two dimensional array, and print its
position - as row and column numbers. write a search function that
will receive the array and return row and column indexes as output.
6 Declare a two dimensional array of characters, read names from the
user a
Pointer Arithmetic Lab Assignments

implement below functions using pointer increment or decrement method, use below
declarations. Change function name.
1) int strlen(char s[]);
2) char * strrev( char s[]); // returns string base address
3) char * strcpy( char d[] , char s[]); // returns destination base address
4) int strcmp (char s1[], char s2[]);
5) int strncmp(char s1[]. char s2[], int n);
6) int stricmp (char s1[], char s2[]);
7) char * strcat(char d[], char s[]); // returns destination base address
8) char * strlwr(char s[]); // returns s base address
9) char * strchr( char s[], char c); // returns address of given character first occurrence in
given string
10) char * strstr(char str[], char sub[]); //returns the address of the character in the main
string from where the substring is matching.

DMA Assignments

create an integer dynamically,read and print its value.

2) create a string dynamically, read and print its value.

3) create a string dynamically, copy a string constant to it ( "Kernel"). Create another string
dynamically. Copy the first string to the second string.

4) Create a dynamic array of integers. Take input from user , sort the array and display the
output.

5) Implement itoa function and return the string from the function. Print the result in main.
itoa function takes an integer as input, and stores the integer in a string.

6) Write a function, that takes two strings as input and concatenates only the first n
characters from first string and first m characters from second string and makes a new
string.
eg., input str1 = "kernel masters", str2 = "raayan systems", n = 3, m = 5
output : kerraaya
7)Write a function that takes a main string and two sub strings (need not be same length),
and replace every occurrence of first substring with the second substring in main string
and return a new string as output.

eg., input : kernel e xxx


output: kxxxrnxxxl

8) Write a function that takes two sorted integer arrays as input, and returns an array of
integers from both arrays in sorted order, excluding duplicate numbers.
Assume that the input arrays are sorted.
eg., array1 : 10,12,13,14,15,16
array2 : 9,12,15,19,20
output array3 : 9,10,12,13,14,15,16,19,20

9) Write a program to create a scenario where malloc() fails? And mentioned after how
many bytes allocation is FAIL?

You might also like