Program List
Program List
h>
/*1. WAP that accepts the marks of 5 subjects and finds the sum and
percentage marks obtained by the student.*/
void main()
float per;
scanf("%d",&hindi);
scanf("%d",&english);
scanf("%d",&science);
scanf("%d",&math);
scanf("%d",&computer);
sum=hindi+english+science+math+computer;
printf("Sum of marks=%d",sum);
per=(float)sum/5;
printf("Percentage of marks=%f",per);
Output:
Enter marks of Hindi=12
#include<math.h>
/*2. WAP that calculates the Simple Interest and Compound Interest.
The Principal, Amount, Rate of Interest and Time are entered through
the keyboard.*/
void main()
printf("Enter Principle=");
scanf("%f",&p);
printf("Enter Rate=");
scanf("%f",&r);
printf("Enter Time=");
scanf("%f",&t);
si=(p*r*t)/100;
printf("Simple Interest=%f",si);
ci = a - p;
printf("\nCompound Interest=%f",ci);
Output
Enter Principle=1250
Enter Rate=4
Enter Time=2
Simple Interest=100.000000
Compound Interest=101.999878
#include<stdio.h>
float r,c,a;
printf("Enter radius=");
scanf("%f",&r);
float pi=3.14;
c=2*pi*r;
a=pi*r*r;
printf("\nCircumference of a circle=%f",c);
printf("\nArea of a circle=%f",a);
Output:
Enter radius=22
Circumference of a circle=138.160004
Area of a circle=1519.760010
#include<stdio.h>
float c,f;
printf("Enter Centigrade=");
scanf("%f",&c);
f=(9*c)/5+32;
printf("Fahrenheit=%f",f);
}
Output:
Enter Centigrade=52
Fahrenheit=125.599998
#include<stdio.h>
/*5. WAP that swaps values of two variables using a third variable. */
void main()
{
int a,b,temp;
printf("Enter a=");
scanf("%d",&a);
printf("Enter b=");
scanf("%d",&b);
temp=a;
a=b;
b=temp;
printf("\nAfter swapping");
printf("\na=%d",a);
printf("\nb=%d",b);
}
Output:
Enter a=2
Enter b=3
After swapping
a=3
b=2
#include<stdio.h>
int a,b;
printf("Enter a=");
scanf("%d",&a);
printf("Enter b=");
scanf("%d",&b);
if(a==b)
{
printf("\na and b are equal.");
}
else
{
printf("\na and b are not equal.");
}
}
Output:
Enter a=2
Enter b=2
a and b are equal.
#include<stdio.h>
if(a>b)
{
if(a>c)
{
printf("\na is greatest.");
}
else
{
printf("\nc is greatest.");
}
}
else
{
if(b>c)
{
printf("\nb is greatest.");
}
else
{
printf("\nc is greatest.");
}
}
}
Output:
Enter a=2
Enter b=3
Enter c=5
c is greatest.
#include<stdio.h>
/* WAP that finds whether a given number is even or odd. */
void main()
{
int n;
printf("Enter a number=");
scanf("%d",&n);
if(n%2==0)
{
printf("\nn is even number.");
}
else
{
printf("\nn is odd number.");
}
}
Output:
Enter a number=3
n is odd number.
#include<stdio.h>
/* WAP that tells whether a given year is a leap year or not. */
void main()
{
int year;
printf("Enter a number=");
scanf("%d",&year);
if((year%4==0||year%400==0)&&year%100!=0)
{
printf("\nYear is leap year.");
}
else
{
printf("\nYear is not leap year.");
}
}
output:
Enter a number=2004
Year is leap year.
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>
/* WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by the
student.*/
void main()
{
int hindi, english, science,math,computer,sum ;
float per;
printf("Enter marks of Hindi=");
scanf("%d",&hindi);
printf("Enter marks of English=");
scanf("%d",&english);
printf("Enter marks of Science=");
scanf("%d",&science);
printf("Enter marks of Math=");
scanf("%d",&math);
printf("Enter marks of Computer=");
scanf("%d",&computer);
sum=hindi+english+science+math+computer;
printf("\nSum of marks=%d",sum);
per=(float)sum/5;
printf("\nPercentage of marks=%f",per);
if(per>=90&&per<=100)
{
printf("\nGrade A");
}
else if(per>=80&&per<90)
{
printf("\nGrade B");
}
else if(per>=60&&per<80)
{
printf("\nGrade C");
}
else if(per<60)
{
printf("\nGrade D");
}
}
Output:
Enter marks of Hindi=99
Enter marks of English=89
Enter marks of Science=79
}
}
3- Mul:
4- Div:
5- Mod:
Enter number a=6
Output:
Enter number=12
0
1
1
2
3
5
8
13
21
34
55
89
WAP to check whether the entered number is prime or
not.
#include<stdio.h>
void main()
{
int i,n,prime=1;
printf("Enter number=");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
prime=0;
break;
}
}
if(prime==1)
{
printf("Prime Number");
}
else
{
printf("Not Prime Number");
}
}
Output:
Enter number=44
Not Prime Number
WAP to find the sum of digits of the entered number.
#include<stdio.h>
void main()
{
int n,r,sumDigits=0;
printf("Enter number=");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sumDigits=sumDigits+r;
n=n/10;
}
printf("Sum of Digits=%d",sumDigits);
}
Output:
Enter number=125
Sum of Digits=8
WAP to find the reverse of a number.
#include<stdio.h>
void main()
{
int n,r,rev=0;
printf("Enter number=");
scanf("%d",&n);
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf("Reverse number=%d",rev);
}
Output:
Enter number=124
\Reverse number=421
WAP to print Armstrong numbers from 1 to 1000.
(1*1*1)+(5*5*5)+(3*3*3) = 153
#include<stdio.h>
void main()
{
int i=1,r,aNum=0,num;
for(i=1;i<=1000;i++)
{
num=i;
aNum=0;
while(num>0)
{
r=num%10;
aNum=aNum+r*r*r;
num=num/10;
}
if(i==aNum)
{
printf("Armstrong Number=%d\n",i);
}
}
}
Output:
Armstrong Number=1
Armstrong Number=153
Armstrong Number=370
Armstrong Number=371
Armstrong Number=407
WAP to convert binary number into decimal number and
vice versa.
#include<stdio.h>
#include
void main()
{
int n,r,rev=0,p=0;
printf("Enter binary number=");
scanf("%d",&n);
while(n>0)
{
r=n%10;
if(r!=0)
{
rev= rev+(int)pow(2,p);
}
n=n/10;
p++;
}
printf("Decimal number=%d",rev);
}
Output:
for(i=0;i<=9;i++)
{
sum[i]=ar1[i]+ar2[i];
}
printf("Sum of arrays:-");
for(i=0;i<=9;i++)
{
printf("\nsum[%d]=%d",i,sum[i]);
}
Output:
Enter first array:-
ar1[0]=1 1 1 1 1 1 1 1
ar1[1]=ar1[2]=ar1[3]=ar1[4]=ar1[5]=ar1[6]=ar1[7]=ar1[8]=11
ar1[9]=11
Enter second array:-
ar2[0]=1
ar2[1]=11
ar2[2]=11
ar2[3]=11
ar2[4]=11
ar2[5]=1
ar2[6]=11
ar2[7]=111
ar2[8]=2
ar2[9]=1
Sum of arrays:-
sum[0]=2
sum[1]=12
sum[2]=12
sum[3]=12
sum[4]=12
sum[5]=2
sum[6]=12
sum[7]=112
sum[8]=13
sum[9]=12
WAP to find the minimum and maximum element of the
array.
#include<stdio.h>
void main()
{
int i,ar[10],min,max;
printf("Enter array:-\n");
for(i=0;i<=9;i++)
{
printf("ar[%d]=",i);
scanf("%d",&ar[i]);
}
min=ar[0];
max=ar[0];
for(i=0;i<=9;i++)
{
if(min > ar[i])
{
min=ar[i];
}
if(max < ar[i])
{
max=ar[i];
}
}
printf("\nMin=%d",min);
printf("\nMax=%d",max);
}
Output:
Enter array:-
ar[0]=1 11 11 11 12 22 33 66 66
ar[1]=ar[2]=ar[3]=ar[4]=ar[5]=ar[6]=ar[7]=ar[8]=ar[9]=99
Min=1
Max=99
WAP to search an element in a array using Linear Search.
#include<stdio.h>
void main()
{
int i,ar[10],n,pos=-1;
printf("Enter array:-\n");
for(i=0;i<=9;i++)
{
printf("ar[%d]=",i);
scanf("%d",&ar[i]);
}
printf("Enter number to be search=");
scanf("%d",&n);
for(i=0;i<=9;i++)
{
if(n==ar[i])
{
pos=i+1;
}
}
if(pos==-1)
{
printf("Number not found.");
}
else
{
printf("Position=%d",pos);
}
}
Output:
Enter array:-
ar[0]=1 2 3 4 5 6 7 8 9
ar[1]=ar[2]=ar[3]=ar[4]=ar[5]=ar[6]=ar[7]=ar[8]=ar[9]=22
Enter number to be search=6
Position=6
WAP to sort the elements of the array in ascending order
using Bubble Sort technique.
#include<stdio.h>
void main()
{
int i,j,ar[10],temp;
printf("Enter array:-\n");
for(i=0;i<=9;i++)
{
printf("ar[%d]=",i);
scanf("%d",&ar[i]);
}
for(i=0;i<=9;i++)
{
for(j=0;j<=9-i;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
printf("Sorted array:-");
for(i=0;i<=9;i++)
{
printf("\n%d",ar[i]);
}
}
Output:
Enter array:-
ar[0]=1 23 415 7788 899 2 6 6 6
ar[1]=ar[2]=ar[3]=ar[4]=ar[5]=ar[6]=ar[7]=ar[8]=ar[9]=33
Sorted array:-
1
2
6
6
6
23
33
415
899
7788
Multiply two matrices Program in C
#include<stdio.h>
void main()
{
int i,j,k,m1[3][3],m2[3][3],mul[3][3];
printf("Enter first matrix:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter second matrix:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&m2[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
mul[i][j]=0;
for(k=0;k<=2;k++)
{
mul[i][j]=mul[i][j]+(m1[i][k]*m2[k][j]);
}
}
}
printf("Multiplication of metrices:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",mul[i][j]);
}
printf("\n");
}
}
Output
: Enter first matrix:-
1231
12 3 3 3
1222
Enter second matrix:-
12
12
12
2
3
Multiplication of metrices:-
10 12 13
20 32 23
11 14 12
WAP that finds the sum of diagonal elements of a mxn
matrix.
#include<stdio.h>
void main()
{
int i,j,k,matrix[3][3],sum=0;
printf("Enter matrix:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&matrix[i][j]);
}
}
printf("Matrix is:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",matrix[i][j]);
}
printf("\n");
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i==j)
{
sum = sum + matrix[i][j];
}
}
}
123
234
456
Matrix is:-
1 2 3
2 3 4
4 5 6
Sum of diagonal is = 10