0% found this document useful (0 votes)
11 views15 pages

Project

The document contains a collection of C programming exercises that cover various topics such as calculating the sum of digits, checking for leap years, sorting arrays, swapping variables, converting between binary and decimal, and performing matrix operations. Each exercise includes a brief description followed by the corresponding C code implementation. The exercises are designed to enhance programming skills and understanding of fundamental concepts in C.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views15 pages

Project

The document contains a collection of C programming exercises that cover various topics such as calculating the sum of digits, checking for leap years, sorting arrays, swapping variables, converting between binary and decimal, and performing matrix operations. Each exercise includes a brief description followed by the corresponding C code implementation. The exercises are designed to enhance programming skills and understanding of fundamental concepts in C.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

17 WAP to find the sum of digits of the entered number

#include<stdio.h> int
main()
{
int num,tempn,digit; int sum =0;
printf("Enter the number is= \n");
scanf("%d",&num); tempn = num;
while(num>0)
{
digit = num%10; sum =
sum + digit; num =
num/10;
}
printf("The sum of digit %d = %d\n",tempn,sum);
}

9. WAP that tells whether a given year is a leap year or not


#include<stdio.h> int
main()
{
int year; printf("Enter the
year= "); scanf("%d",&year);
if(year%4==0)
{
printf("%d is the leap year.",year);
}
else if(year%400==0)
{
printf("%d is the leap year.",year);
}
else
{
printf("%d is not a leap year.",year);
}
}

[Link] to sort the elements of the array in ascending order using Bubble Sort technique.
#include<stdio.h> 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])
{
int temp = arr[j]; arr[j]
= arr[j + 1]; arr[j + 1] =
temp;
}
}
}
}
int main()
{
int n; printf("Enter the number of the elments in the array =");
scanf("%d",&n); int arr[n]; printf("Enter %d element = \n",n);
for(int i=0;i < n; ++i)
{
scanf("%d",&arr[i]);
}
bubblesort(arr, n); printf("Sorted array in ascending
order = \n"); for(int i=0;i < n;++i)
{ printf("%d",arr[i]);
} printf("\n");
}

5. WAP that swaps values of two variables using a third variable.


#include<stdio.h> int
main()
{
int a,b,newb; printf("Enter the two
number : "); scanf("%d%d",&a,&b);
printf("\nbefore swapping\n");
printf("a=%d\nb=%d\n",a,b); newb =
a;
a=b; b=
newb;
printf("\nAfter swaping\n"); printf("a =
%d\nb =%d\n",a,b);
}

[Link] to convert binary number into decimal number and vice versa.
#include <stdio.h> #include
<math.h>
// Function to convert binary to decimal int
binaryToDecimal(long long binary) { int
decimal = 0, base = 1; while (binary > 0) { int
digit = binary % 10; decimal += digit * base;
base *= 2; binary /= 10;
}
return decimal;
}
int main() { long long
binaryNum;
// Input
printf("Enter a binary number: "); scanf("%lld", &binaryNum); // Convert
and output printf("Decimal equivalent: %d\n",
binaryToDecimal(binaryNum)); return 0;
}

21. WAP that simply takes elements of the array from the user and finds the sum
of these
elements.
#include<stdio.h> int
main()

{
int n; printf("Enter the number of element of the array =");
scanf("%d",&n); int arr[n]; printf("Enter %d elemengt =\
n",n); for(int i=1;i<=n;++i)
{
scanf("%d",&arr[i]);
}
int sum =0;
for(int i=1;i<=n;++i)
{
sum = sum + arr[i];
}
printf("Sum of the element =%d\n",sum);
}

12. WAP to print the sum of all numbers up to a given number.


#include<stdio.h> int
main()
{
int n,sum = 0,i; printf("Enter the
number= \n"); scanf("%d",&n);
for(i=1;i<=n;++i)
{
sum= sum + i;
}
printf("The summation of the number =%d",sum);
}

6. WAP that checks whether the two numbers entered by the user are equal or not.
#include<stdio.h> int
main()
{
int num1,num2; printf("Enter the two
number = "); scanf("%d
%d",&num1,&num2);
if(num1==num2)

{
printf("The given two number are equal.");
}
else
{
printf("The given number are not equal.");
}
}

[Link] to check whether the entered number is prime or not.


#include<stdio.h>
#include<conio.h> int
main()
{
int n,c=2; printf("Enter the number to check if it prime
="); scanf("%d",&n); for(c=2;c<=n-1;c++)
{
if(n%c==0)
{
printf("%d is not the prime number.\n",n); break;
}
}
if(c==n)
{
printf("%d is the prime number.\n",n);
}
}

1.

#include<stdio.h> int main()

int phy,chem,math,english,hindi;

int sum; float

percen;

printf("Enter the marks of five subject = "); scanf("%d%d%d%d

%d",&phy,&math,&chem,&english,&hindi); sum = phy + chem + english + hindi + math; printf("The sum

of total obtained marks =%d\n",sum);

percen = sum/5;

printf("The percentage of obtained marks=%f",percen);

[Link] to print Armstrong numbers from 1 to 100.

#include<stdio.h> int main()

int count = 1,x,y,z; printf("armstrong number between 1 and 500 are =\n"); while(count<=500)

x = count%10; y = (count/10)%10; z = count/100; if((x*x*x) +

(y*y*y) + (z*z*z) == count)

printf("%d, ",count);

count++;

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> int

main(){ float si,p,r,t;

printf("Enter the principal= "); scanf("%f",&p); printf("Enter the amount of

interest= "); scanf("%f",&r); printf("Enter the time= "); scanf("%f",&t); si =

p*r*t/100; printf("the amount of simple interest= %f",si);

}
11. WAP that takes two operands and one operator from the user, perform the operation, and
prints the result by using Switch statement.
#include<stdio.h> int
main()
{
float a,b; char operator; printf("Enter the
operator(*,+,-,/)=\n");
scanf("%c",&operator); printf("Enter two
operand =\n"); scanf("%f%f",&a,&b);
switch(operator)
{
case '+': printf("Addition =%f",a+b);
break; case '*':
printf("Multiplication =%f",a*b);
break; case '-': printf("Subtraction =
%f",a-b); break; case '/':
printf("Division =%f",a/b); break;
default:
printf("The operation is not ferform");
}
}

[Link] to search an element in a array using Linear Search.


#include<stdio.h> int linearsearch(int arr[],int
n, int target)
{
for(int i=0;i<n;++i)
{ if(arr[i]==target)
{
return i;
}
}
return -1;
}
int main()
{
int n; printf("Enter the number of element of array =");
scanf("%d",&n); int arr[n]; printf("Enter %delement =\
n",n); for(int i=0;i<n;++i)
{
scanf("%d",&arr[i]);
}
int target; printf("Enter the element for search
= "); scanf("%d",&target); int result =
linearsearch(arr,n,target); if(result != -1)
{
printf("Element %d found in the index%d.\n ",target,result);
}
else
{
printf("Element %d not found in the array.\n",target);
}
}

[Link] 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> int
main()
{
float sum,phy,chem,mathe,evs,pps,per; printf("Enter the
marks of five subject =\n"); scanf("%f%f%f%f
%f",&phy,&chem,&mathe,&evs,&pps); sum = phy + chem
+ mathe + evs + pps; per = sum/5; printf("The obtained
percentage = %f\n",per); if(per>90 && per<=100)
{
printf(" A grade");
}
else if(per>80 && per<=90)
{
printf("B grade");
}
else if(per>=60 && per<=80)
{
printf(" C grade");
}
else
{
printf("D grade");
}
}

15. WAP to print the Fibonacci series.


#include<stdio.h> int
main()
{
int n,x1=0,x2=1,i,m; printf("Enter the number =\
n"); scanf("%d",&n); printf(" Fibonacci series is =
%d,%d,",x1,x2); for(i=3;i<=n;++i)
{
m = x1 + x2;
printf("%d,",m);
x1 = x2; x2 = m;
}
}

7. WAP to find the greatest of three numbers.


#include<stdio.h> int
main()
{
int a,b,c; printf("Enter the three number
=\n"); scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("%d is the greatest number.",a);
}
else
{
printf("%d is the greatest number.",c);
}
}
if(b>a)
{
if(b>c)
{
printf("%d is the greatest number.",b);
}
else
{
printf("%d is the greatest number.",c);
}
}
}

[Link] to convert binary number into decimal number and vice versa.
#include <stdio.h>
// Function to convert decimal to binary long
long decimalToBinary(int decimal) { long long
binary = 0; int base = 1; while (decimal > 0)
{ int remainder = decimal % 2; binary +=
remainder * base; base *= 10; decimal /= 2;

}
return binary;
}
int main() { int decimalNum; // Input printf("Enter a decimal number: ");
scanf("%d", &decimalNum); // Convert and output printf("Binary
equivalent: %lld\n", decimalToBinary(decimalNum)); return 0;
}

[Link] to add and multiply two matrices of order nxn.


#include <stdio.h>
// Function to add two matrices void addMatrices(int mat1[][100], int mat2[][100],
int result[][100], int n) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
int main() { int n; printf("Enter the order of the square
matrices (n): "); scanf("%d", &n); int mat1[100][100],
mat2[100][100], result[100][100]; printf("Enter elements of
the first matrix:\n"); for (int i = 0; i < n; ++i) { for (int j = 0; j <
n; ++j) { scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of the second matrix:\n"); for
(int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j)
{ scanf("%d", &mat2[i][j]);
}
}
// Perform matrix addition addMatrices(mat1,
mat2, result, n); printf("Resultant matrix after
addition:\n"); for (int i = 0; i < n; ++i) { for (int j =
0; j < n; ++j) {
printf("%d ", result[i][j]);
} printf("\n");
}
return 0;
}

[Link] to print sum of even and odd numbers from 1 to N numbers.


#include<stdio.h> int
main()
{
int n,sumev=0,sumod=0,i;
printf("Enter the number =\n");
scanf("%d",&n); for(i=1;i<=n;++i)
{
if(i%2==0)
{
sumev += i;
}
else
{
sumod +=i;
}
}
printf("The sum of even number from 1 to %d = %d\n",n,sumev); printf("The sum
of odd numberfrom 1 to % d = %d\n",n,sumod);
}

[Link] to print Armstrong numbers from 1 to 100.


#include<stdio.h> #include<math.h> int main(){ int
i,sum,num,count = 0; printf("All armstrong number between 1
and 100 are:\n"); for(i = 1;1<=1000;i++){ num =i; while(num!=0)
{ num=num/10; count++;
}
num = i; sum = pow(num%10,count)+pow((num%100 - num%10)/10,count)+pow((num%1000
num%100)/100,count); if(sum ==i){ printf("%d",i);
}
count = 0;
}
return 0;
}

3. WAP to calculate the area and circumference of a circle.


#include<stdio.h> int
main()
{
float r,area,cir; printf("Enter the radius of the circle= ");
scanf("%f",&r); float pi=3.14; area = pi*r*r; printf("The
area of the circle =%f\n",area); cir = 2*pi*r; printf("The
circumference of the circle=%f",cir);
}

[Link] to find the minimum and maximum element of the array.


#include<stdio.h> int
main()
{
int n; printf("Enter the number of element of the array =");
scanf("%d",&n); int arr[n];

printf("Enter %d element =\n",n); for(int


i=0;i<n;++i)
{
scanf("%d",&arr[i]);
}
int min = arr[0], max = arr[0];
for(int i=1;i<n;++i)
{ if(arr[i]<min)
{
min = arr[i];
}
if(arr[i]>max)
{
max = arr[i];
}
}
printf("Minimum element =%d\n",min); printf("Maximum
element = %d\n",max);
}

[Link] that inputs two arrays and saves sum of corresponding elements of these
arrays in a third array and prints them.
#include<stdio.h> int
main()
{
int n;
printf("Enter the number of elements in array =");
scanf("%d",&n); int arr1[n], arr2[n], arr3[n];
printf("Enter %d element for the 1st array=\n",n); for(int
i=0;i<=n;i++)
{
scanf("%d",&arr1[i]);
}
printf("Enter %d element for 2nd array=\n ",n); for(int
i=0;i<n;++i)
{
scanf("%d",&arr2[i]);
}
for(int i=0;i<n;++i)
{
arr3[i] = arr1[i] + arr2[i];
}
printf("sumation of corresponding element =\n"); for(int
i=0;i<n;++i)
{
printf("%d",arr3[i]);
} printf("\
n"); }

[Link] that finds the sum of diagonal elements of a mxn matrix.


#include<stdio.h> int
main()
{
int m,n; printf("Enter the number of rows =");
scanf("%d",&m); printf("Enter the number of
columns ="); scanf("%d",&n); if(m !=n)
{
printf("Sum of diagonal is only possible in square matrix.\n"); return 1;
}
int matrix[100][100];
printf("Enter the element of matrix =\n"); for(int
i=0;i<m;++i)
{
for(int j=0;j<n;++j)
{
scanf("%d",&matrix[i][j]);
}
}
int diagonalsum =0; for(int
i=0;i<m;++i)
{
diagonalsum = diagonalsum + matrix[i][i];
}
printf("Sum of the diagonal element of matrix = %d\n",diagonalsum);
}

30.
WAP to swap two elements using the concept of pointers.
#include <stdio.h>
// Function to swap two integers using pointers void
swap(int* a, int* b) { int temp = *a; *a = *b;
*b = temp;
}
int main() { int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2); //
Call the swap function swap(&num1,
&num2);
printf("After swapping:\n"); printf("First
integer: %d\n", num1); printf("Second
integer: %d\n", num2); return 0;
}

[Link] to find the reverse of a number.


#include<stdio.h> int
main()
{
int n,reverse=0; printf("Enter a
number =\n"); scanf("%d",&n);
while(n!=0)
{
reverse = reverse*10; reverse =
reverse + n%10; n = n/10;
}
printf("reverse of the given number = %d\n",reverse);
}
13. WAP to find the factorial of a given number.
#include<stdio.h> int
main()
{ int i,n,f=1; printf("Enetr the
umber =\n"); scanf("%d",&n);
for(i=1;i<=n;i++)
{
f = f*i;
}
printf("The factorial of the given number =%d",f);
}

8. WAP that finds whether a given number is even or odd.


#include<stdio.h> int
main()
{
int a; printf("Enter the number =
"); scanf("%d",&a); if(a%2==0)
{
printf("%d is the even number.",a);
}
else
{
printf("%d is the odd number.",a);
}
}

4. WAP that accepts the temperature in Centigrade and converts into Fahrenheit using the
formula C/5=(F-32)/9.
#include<stdio.h> int
main()
{
float celcius,fahrenheit; printf("Enter the
temperature in centigrate="); scanf("%f",&celcius);

fahrenheit= (celcius*9/5)+32; printf("The temperature in


fahrenheit =%f",fahrenheit);
}

You might also like