1) WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by
the student.
#include <stdio.h>
#include <math.h>
void main()
{
int a, b, c, d, e, S;
float P;
printf("Enter the marks of Hindi, English, Maths, Science and
Computer Science:");
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
S = a + b + c + d + e;
printf("Total marks is: %d\n", S);
P = (S * 100) / 500;
printf("Total Percentage: %.2f", P);
}
OUTPUT
Enter the marks of Hindi, English, Maths, Science and Computer
Science:25 25 25 25 25
Total marks is:125
Total Percentage:25.00
2) WAP that calculates the Simple Interest and Compound Interest. The Principal, Amount, Rate of
Interest and Time are entered through the keyboard.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float p,r,t,n;
float si,ci,a;
printf("Enter p,r,t,n");
scanf("%f%f%f%f", &p, &r, &t, &n);
si=(p*r*t)/100;
printf("\nSI=%.2f", si);
a=p*pow((1+r/100),n);
printf("\nAmount = %.2f", a);
ci= a-p;
printf("\nCI=%.2f", ci);
}
OUTPUT
Enter p,r,t,n 1200 5.4 2
1
SI=129.60
Amount = 1333.09
CI=133.09
3) WAP to calculate the area and circumference of a circle.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
Float r, P;
printf("\nEnter the value of radius:");
scanf("%f", &r);
A2=3.14*r*r;
P = 2 * 3.14 * r;
printf("The area of circle:%.2f", A2);
printf("\nPerimeter of circle is %.2f", P);
}
OUTPUT
Enter the value of radius:4
The area of circle:50.24
Perimeter of circle is 25.12
4) WAP that accepts the temperature in Centigrade and converts into Fahrenheit using the formula
C/5=(F-32)/9.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float F, C;
printf("Enter the value of temperature in Fahrenheit:");
scanf("%f", &F);
C = 5 * ((F - 32)) / 9;
printf("The value of temperature in Centigrade is:%.2f", C);
}
OUTPUT
Enter the value of temperature in Fahrenheit:25
The value of temperature in Centigrade is:-3.89
5) WAP that swaps values of two variables using a third variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
printf("Enter the value of a: ");
scanf("%d", &a);
printf("Enter the value of b: ");
scanf("%d", &b);
c = a;
a = b;
b = c;
printf("Value of a : %d", a);
printf("\nValue of b : %d", b);
}
OUTPUT
Enter the value of a: 3
Enter the value of b: 2
Value of a: 2
Value of b: 3
6) WAP that checks whether the two numbers entered by the user are equal or not.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int x, y;
printf("Enter the values of x and y:");
scanf("%d%d", &x, &y);
if (x > y)
{
printf("%d is greater than %d.", x, y);
}
if (x==y)
{
printf("Both numbers are equal.");
}
else
{
printf("%d is greater than %d.", y, x);
}
}
OUTPUT
Enter the values of x and y:5 5
Both number are equal.
7) WAP to find the greatest of three numbers.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int x, y, z;
printf("Enter the values of x, y and z:");
scanf("%d%d%d", &x, &y, &z);
if (x > y)
{
if (x > z)
{
printf("x is greatest.");
}
else
{
printf("z is greatest.");
}
}
else
{
if (y > z)
{
printf("y is greatest.");
}
else
{
printf("z is greatest.");
}
}
}
OUTPUT
Enter the values of x, y and z:2 4 1
y is greatest.
8) WAP that finds whether a given number is even or odd.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int n;
printf("Enter the number:");
scanf("%d", &n);
if (n % 2 == 0)
{
printf("The given number is even.");
}
else
{
printf("Given number is odd");
}
}
OUTPUT
Enter the number: 5
The given number is odd.
9) WAP that tells whether a given year is a leap year or not.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int n;
printf("Enter the year:");
scanf("%d", &n);
if (n % 4 == 0 && n % 100 != 0)
printf("Given year is leap year.");
else
printf("Given year is not leap year.");
}
OUTPUT
Enter the year:2024
Given year is leap year.
10) WAP that accepts marks of five subjects and finds percentage and prints grades according to the
following criteria:
Between 90-100%--------------Print ‘A’ 80-90%----------------------------Print ‘B’ 60-80%---------------
------------Print ‘C’ Below 60%----------------------Print ‘D’
#include <stdio.h>
void main()
{
int a, b, c, d, e, S;
float P;
printf("Enter the marks of 5 main subjects: English, Hindi,
Maths,Science and Social Science:");
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
S = a + b + c + d + e;
P = (S * 100) / 500;
printf("\nThe percentage is %.2f", P);
if (100 >= P && P >= 90)
{
printf("\nGrade A", P);
}
else if (90 > P && P >= 80)
{
printf("\nGrade B", P);
}
else if (80 > P && P >= 60)
{
printf("\nGrade C", P);
}
else
{
printf("\nGrade D", P);
}
}
OUTPUT
Enter the marks of 5 main subjects: English, Hindi, Maths, Science and
Social Science:50 50 50 70 70
The percentage is 58.00
Grade D
11) WAP that takes two operands and one operator from the user and perform the operation and
prints the result by using Switch statement.
#include <stdio.h>
void main()
{
char ch;
int x, y, z;
printf("\nEnter + for addition");
printf("\nEnter - for subtraction");
printf("\nEnter * for multiplication");
printf("\nEnter / for division");
scanf("%c", &ch);
switch (ch)
{
case '+':
printf("Enter the value of two integers: ");
scanf("%d%d", &x, &y);
z = x + y;
printf("z = %d", z);
break;
case '-':
printf("Enter the values of two integers: ");
scanf("%d%d", &x, &y);
z = x - y;
printf("z = %d", z);
break;
case '*':
printf("Enter the values of two integers: ");
scanf("%d%d", &x, &y);
z = x * y;
printf("z = %d", z);
break;
case '/':
printf("Enter the values of two integers: ");
scanf("%d%d", &x, &y);
z = x / y;
printf("z = %d", z);
break;
default:
printf("Wrong choice.");
}
}
OUTPUT
Enter + for addition
Enter - for subtraction
Enter * for multiplication
Enter / for division+
Enter the value of two integers: 4 5
z = 9
12) WAP to print the sum of all numbers up to a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int s, i, n;
s = 0;
printf("Enter the value:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
s = s + i;
}
printf("s=%d", s);
}
OUTPUT
Enter the value:10
s=55
13) WAP to find the factorial of a given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int f, n, i;
f=1;
printf("Enter the number:");
scanf("%d", &n);
for ( i = n; i > 0; i--)
{
f=f*i;
}
printf("%d", f);
}
OUTPUT
Enter the number:5
120
14) WAP to print sum of even and odd numbers from 1 to N numbers.
#include <stdio.h>
void main()
{
int i, n,E, O;
printf("\nEnter the number: ");
scanf("%d", &n);
E = 0;
O = 0;
for (i = 1; i <= n; i++)
{
if (i%2 == 0 )
{
E = E + i;
}
else
{
O = O + i;
}
}
printf("\nSum of even numbers is : %d", E);
printf("\nSum of odd numbers is : %d", O);
OUTPUT
Enter the number: 100
Sum of even numbers is: 2550
Sum of odd numbers is: 2500
15) WAP to print the Fibonacci series.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, x, y, z;
x=0;
y=1;
printf("Enter the value of n:");
scanf("%d", &n);
for ( i = 1; i <=n; i++)
{
z=x+y;
printf("\n%d", z);
x=y;
y=z;
}
OUTPUT
Enter the value of n:5
1
2
3
5
8
16) WAP to check whether the entered number is prime or not.
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, flag;
flag = 0;
printf("Enter n:");
scanf("%d", &n);
for (i = 2; i <= n / 2; i++)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
}
OUTPUT
Enter n:29
29 is a prime number.
17) WAP to find the sum of digits of the entered number.
#include <stdio.h>
void main()
{
int n, s, r;
s = 0;
printf("Enter Number:");
scanf("%d", &n);
for ( ; n > 0 ; )
{
r = n % 10;
s = s + r;
n = n / 10;
}
printf("Sum of digits is %d", s);
}
OUTPUT
Enter Number:1256
Sum of digits is 14
18) WAP to find the reverse of a number.
#include <stdio.h>
void main()
{
int n, s, r;
s = 0;
printf("Enter Number:");
scanf("%d", &n);
for ( ; n > 0 ; )
{
r = n % 10;
s = s*10 + r;
n = n / 10;
}
printf("%d", s);
}
OUTPUT
Enter Number:12978
87921
19) WAP to print Armstrong numbers from 1 to 100.
20) WAP to convert binary number into decimal number and vice versa.
a) Decimal to Binary
#include <stdio.h>
void main()
{
int num, ans, mul;
printf("Enter the number: ");
scanf("%d", & num);
int rem;
ans = 0;
mul = 1;
while (num > 0)
{
// remainder
rem = num % 2;
// quotient
num = num / 2;
// answer
ans = rem * mul + ans;
// multiplying factor
mul = mul * 10;
}
printf("Answer : %d\n", ans);
}
OUTPUT
Enter the number: 56
Answer : 111000
b) Binary to Decimal
#include<stdio.h>
#include<conio.h>
void main()
{
int num, mul, ans, rem;
printf("Enter the number: ");
scanf("%d", &num);
ans = 0;
mul = 1;
while(num > 0)
{
rem = num % 10;
num = num / 10;
ans = mul * rem + ans;
mul = mul * 2;
}
printf("Answer is %d", ans);
OUTPUT
Enter the number: 10101100
Answer is 172
21) WAP that simply takes elements of the array from the user and find the sum of these elements.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum;
sum = 0;
printf("Enter the size of array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: ");
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
sum = sum + arr[i];
}
printf("Sum of the array elements: %d", sum);
}
OUTPUT
Enter the size of array: 5
Enter the elements of the array: 10 12 14 19 15
Sum of the array elements: 70
22) WAP that input two arrays and save sum of corresponding elements of these arrays in a third
array and print them.
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
printf("Enter the size of the arrays: ");
scanf("%d", &n);
int arr1[n], arr2[n], arr3[n];
printf("Enter the elements of the 1st array: ");
for (i = 0; i < n; i++)
{
scanf("%d", &arr1[i]);
}
printf("Enter the elements of second array: ");
for (i = 0; i < n; i++)
{
scanf("%d", &arr2[i]);
}
for (i = 0; i < n; i++)
{
arr3[i] = arr1[i] + arr2[i];
}
printf("The sum of the corresponding elements is: ");
for (i = 0; i < n; i++)
{
printf(" %d ", arr3[i]);
}
printf("\n");
}
OUTPUT
Enter the size of the arrays: 5
Enter the elements of the 1st array: 5 4 6 7 9
Enter the elements of second array: 1 2 3 4 5
The sum of the corresponding elements is: 6 6 9 11 14
23) WAP to find the minimum and maximum element of the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, arr[10]={5, 6, 7, 3, 2, 11, 23, 4, 15, 6};
int min = arr[0];
int max = arr[0];
for ( i = 0; i < 10; i++)
{
min = min < arr[i] ? min : arr[i];
max = max < arr[i] ? arr[i] : max;
}
printf("\nminimum value = %d", min);
printf("\nmaximum value = %d", max);
OUTPUT
minimum value = 2
maximum value = 23
24) WAP to search an element in an array using Linear Search.
#include<stdio.h>
void main()
{
int num;
int i, keynum, found = 0;
printf("Enter the numbers of elements: ");
scanf("%d", &num);
int arr[num];
printf("Enter the elements one by one: ");
for(i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to be searched: ");
scanf("%d", &keynum);
/* Linear search begins */
for(i = 0; i < num; i++)
{
if (keynum == arr[i])
{
found = 1;
break;
}
}
if (found == 1)
printf("Element is present in the array at position %d.", i+1);
else
printf("Element is not present in the array.\n");
}
OUTPUT
Enter the numbers of elements: 5
Enter the elements one by one: 21 45 2 56 3
Enter the element to be searched: 45
Element is present in the array at position 2.
25) WAP to sort the elements of the array in ascending order using Bubble Sort technique.
#include<stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void bubbleSort(int arr[], int n)
{
for(int i = 0; i < n - 1; i++)
{
for(int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(&arr[j], &arr[j + 1]);
}
}
}
}
void printArray(int arr[], int size)
{
for(int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[] = {3, 1, 5, 2, 6, 4};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
26) WAP to add and multiply two matrices of order n*n.
a) For addition
#include <stdio.h>
void main()
{
int n;
printf("Enter the value of order of matrices:");
scanf("%d", &n);
int a[n][n], b[n][n], c[n][n], i, j;
printf("\nEnter the 1st matrix elements: ");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\nEnter the 2nd matrix elements: ");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
c[i][j] = 0;
c[i][j] += a[i][j] + b[i][j];
}
}
printf("\nAddition of two matrix is:\n ");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
printf(" %d ", c[i][j]);
printf(" \n ");
}
}
OUTPUT
Enter the value of order of matrices: 2
Enter the 1st matrix elements: 1 1 1 1
Enter the 2nd matrix elements: 1 1 1 1
Addition of two matrix is:
2 2
2 2
b)
27) WAP that finds the sum of diagonal elements of a m*n matrix.
#include <stdio.h>
void main()
{
int m, n, i, j, sum;
printf("Enter the value of row: ");
scanf("%d", &m);
printf("Enter the value of column: ");
scanf("%d", &n);
int a[m][n];
sum = 0;
printf("Enter the value of 1st matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
{
sum = sum + a[i][j];
}
}
}
printf("Sum of diagonal elements is %d", sum);
}
28) WAP to implement strlen(), strcat() and strpy() using the concept of
Functions.
• For length of string
#include<stdio.h>
#include<string.h>
void main()
{
char myString[] = "Hello, World";
int length = strlen(myString);
printf("Length of the string : %d\n", length);
}
OUTPUT:
Length of the string : 12
• For adding two strings
#include<stdio.h>
#include<string.h>
int main()
{
char String1[50] = "Hello, ";
char String2[] = "World!";
strcat(String1, String2);
printf("Concatenated string : %s\n ", String1);
return 0;
}
OUTPUT:
Concatenated string : Hello, World!
• For copy of string
#include<string.h>
int main()
{
char source[] = "Hello, World!";
char destination[50];
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Copied string: %s\n", destination);
return 0;
}
OUTPUT:
Source string: Hello, World!
Copied string: Hello, World!
29) Define a structure data type TRAIN_INFO. The type contains Train No.:
integer type, Train Name: string, Departure Time: aggregate type TIME
Arrival Time: aggregate type TIME Start station: string End Station: string
The structure type Time contains two integer members: hour and minute.
Maintain a train time table and implement the following operations:
• List all the trains (sorted according to train number) that
depart from a particular section.
• List all the trains that depart from a particular station at a
particular time.
• List all the trains that depart from a particular station within
the next one hour of a given time.
• List all the trains between a pair of start station and end
station.
30) WAP to swap two elements using the concept of pointers.
#include<stdio.h>
void main()
{
int x, y, *p1, *p2;
printf("Enter the value of x and y: ");
scanf("%d%d", &x, &y);
p1 = &x;
p2 = &y;
*p1 = *p1 + *p2;
*p2 = *p1 - *p2;
*p1 = *p1 - *p2;
printf("The value of x : %d\n", *p1);
printf("The value of y : %d", *p2);
}