All
All
2.
/*Program to find whether the number is divisible by 3 */
#include<stdio.h>
void main()
{
int a;
clrscr();
printf("Enter a number:\n");
scanf("%d",&a);
if(a%3==0)
printf("The number %d is divisible by 3");
else
printf("The number %d is not divisible by 3");
getch();
}
Output:
Enter a number:
98
The number 96 is not divisible by 3
Process returned 0 (0x0) execution time : 3.625 s
Enter a number:
81
The number 81 is divisible by 3
Process returned 0 (0x0) execution time : 21.979 s
3.
/* Programto find the grade */
#include<stdio.h>
void main()
{
int m1,m2,m3,m4,avg;
clrscr();
printf(" Enter your marks for the four subjects");
scanf("%d %d %d %d",&m1,&m2,&m3,&m4);
avg=((m1+m2+m3+m4)/4)*100;
if(avg>50)
printf("Congrats you have passed");
else
printf("Sorry,you are fail.Try again");
getch();
}
Output:
4.
/* PROG TO FIND WHETHER THE NO IS ODD OR EVEN*/
#include<stdio.h>
void main()
{
int no;
clrscr();
printf("Enter a number: \n");
scanf("%d",&no);
if(no%2==0)
printf("The number %d is even",no);
else
printf("The number %d is odd",no);
getch();
}
Output:
Enter a number:
75
The number 75 is odd
Process returned 0 (0x0) execution time : 5.047 s
Enter a number:
46
The number 46 is even
Process returned 0 (0x0) execution time : 15.891 s
5.
/* PROGRAM TO FIND WHETHER THE NUMBER IS ZERO OR NON ZERO*/
#include<stdio.h>
void main()
{
int no;
clrscr();
printf("Enter a number:\n");
scanf("%d",&no);
if(no==0)
printf("The given number %d is zero",no);
else
printf("The given number %d is not zero",no);
getch();
}
Enter a number:
0
The given number 0 is zero
Process returned 0 (0x0) execution time : 2.969 s
Enter a number:
5
The given number 5 is not zero
Process returned 0 (0x0) execution time : 2.005 s
6.Largest
Enter 3 numbers:
98
56
72
98 is the largest
56 is the smallest
Enter 3 numbers:
78
88
98
98 is the largest
78 is the smallest
Enter a number:
81
Not Prime
Process returned 0 (0x0) execution time : 2.469 s
Enter a number:
7
The factorial of the number is 5040
Process returned 0 (0x0) execution time : 3.953 s
Enter a number:
25
1 * 25= 25
2 * 25= 50
3 * 25= 75
4 * 25= 100
5 * 25= 125
6 * 25= 150
7 * 25= 175
8 * 25= 200
9 * 25= 225
10 * 25= 250
10.
/*PROGRAM TO WRITE SUM OF THE NUMBERS*/
#include<stdio.h>
void main()
{
int i,no,sum=0;
clrscr();
printf("Enter a number:");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
sum=sum+i;
}
printf("Sum of the natural numbers till %d is %d",no,sum);
getch();
}
11.
/*PROGRAM TO WRITE SUM OF THE NUMBERS*/
#include<stdio.h>
void main()
{
int i=1,no,sum=0;
clrscr();
printf("Enter a number:");
scanf("%d",&no);
/*for(i=1;i<=no;i++)*/
/*while(i<=no)
{
sum=sum+i;
i++;
}*/
do
{
sum=sum+i;
i++;
}while(i<=no);
printf("Sum of the natural numbers till %d is %d",no,sum);
getch();
}
Enter a number:7
Sum of the natural numbers till 7 is 28
Process returned 0 (0x0) execution time : 11.547 s
Enter a number:
5
Enter the exponent value till it should get calculated:8
5^1=5
5^2=25
5^3=125
5^4=625
5^5=3125
5^6=15625
5^7=78125
5^8=390625
Process returned 0 (0x0) execution time : 11.641 s
13.
#include<stdio.h>
void main()
{
int a,b,c,d;
printf("Enter the value of a,b,c,d");
scanf("\n %d %d %d %d",a,b,c,d);
if(a>b)
{
if(b<=c)
{
if(c!=d)
printf("S1");
else
printf("S2");
}
else
printf("S3");
}
else
printf("S4");
printf("\n end\n");
if(((a>b)&&(b<=c))&&(c!=d))
printf("S1");
if(((a>b) &&(b<=c))&&(!(c!=d)))
printf("S2");
if(a>b&& (!(b<=c)))
printf("S3");
if(!(a>b))
printf("S4");
getch();
}
14
#include<stdio.h>
void main()
{
int i ,no,largest,current;
clrscr();
printf("Enter a number");
scanf("%d",&largest);
printf("Total numbers");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
printf("Enter a number:");
scanf("%d",¤t);
if(current>largest)
{
largest=current;
}
}
printf("largest=%d",largest);
getch();
}
Enter number: 45
Total numbers: 6
Enter number :45
Enter number :76
Enter number :87
Enter number :23
Enter number :098
Enter number :67
largest=98
Process returned 0 (0x0) execution time : 12.420 s
15
#include<stdio.h>
#include<math.h>
void main()
{
int no;
float s;
clrscr();
printf("Enter a number:");
scanf("%d",&no);
s=sqrt(no);
printf("Square root of a given number: %f",s);
getch();
}
Enter a number:144
Square root of a given number: 12.000000
Process returned 0 (0x0) execution time : 4.821 s
16. #include<stdio.h>
void main()
{
float overtime,payment,test_value,absent;
char name[100];
clrscr();
printf("Enter your name:");
scanf("%s",&name);
printf("Enter overtime:");
scanf("%f",&overtime);
printf("Enter the number of absent hours");
scanf("%f",&absent);
test_value=overtime-((2/3)*absent);
if (test_value>40)
payment=50;
else if(test_value>30)
payment=40;
else if(test_value>20)
payment=30;
else if(test_value>10)
payment=20;
else
payment=10;
printf("Bonus for %s is $%f",name,payment);
getch();
}
17
#include<stdio.h>
void main()
{
int n,temp,sum=0,rem;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
temp=n;
while(temp>0)
{
rem=temp%10;
sum=(sum*10)+rem;
temp=temp/10;
}
if(sum==n)
printf("The number %d is Palindrome",n);
else
printf("The number %d is not a palindrome",n);
getch();
}
Enter a number:656
The number 656 is Palindrome
Process returned 0 (0x0) execution time : 7.766 s
Enter a number:766
The number 766 is not a palindrome
18
#include<stdio.h>
int main()
{
int num,i,sum=0;
printf("Enter a number:");
scanf("%d",&num);
for(i=1;i<num;i++)
{
if(num%i==0)
{
sum+=i;
printf("\n Divisor and current sum is %d and %d",i,sum);
}
}
if(sum==num)
{
printf("\n The number is perfect");
}
else
printf("\n The number is not perfect");
return 0;
}
Enter a number:6
Enter a number:36
19.
#include<stdio.h>
int main()
{
int temp,num,sum=0,r;
printf("Enter a number:");
scanf("%d",&num);
temp=num;
while(temp>0)
{
r=temp%10;
sum=sum+pow(r,3);
temp=temp/10;
}
if(sum==num)
{
printf("It is a armstrong number.");
}
else{
printf("It is not a armstrong number.");
}
return 0;
}
Enter a number:153
It is a armstrong number.
Process returned 0 (0x0) execution time : 41.856 s
Enter a number:53
It is not a armstrong number.
Process returned 0 (0x0) execution time : 2.500 s
20.
Exchange
#include<stdio.h>
void main()
{
int var1,var2;
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&var1,&var2);
printf(" Before exchange \n Variable 1: %d \n Variable 2:
%d",var1,var2);
exchange(var1,var2);
getch();
}
exchange(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\n After exchange \n Variable 1: %d \n Variable 2: %d",a,b);
}
21.
#include<stdio.h>
void main()
{
int a=5,b=7;
clrscr();
printf("a=%d \n b=%d",a,b);
swap(&a,&b);
printf(“a=%d \n b=%d",a,b);
getch();
}
swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
22.
#include<stdio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
printf(" Given order: %d %d %d",a,b,c);
if(a>b)
swap(&a,&b);
if(b>c)
swap(&b,&c);
if(a>b)
swap(&a,&b);
printf("\n Ascending order: %d %d %d",a,b,c);
getch();
}
swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
23.GCD
#include<stdio.h>
void main()
{
int a,b,e;
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
e=gcd(a,b);
printf("%d",e);
getch();
}
int gcd(int c,int d)
{
if(d>c)
return(gcd(d,c));
else if(d==0)
return(c);
else
return(gcd(d,c%d));
}
24.
#include<stdio.h>
void main()
{
//char name[100];
int reg,lab,mids,fin,grade;
clrscr();
printf("Enter your registration number:");
scanf("%d",®);
while(reg!=00)
{
printf("Registration number:");
scanf("%d",®);
printf("Lab mark:");
scanf("%d",&lab);
printf("Mids mark:");
scanf("%d",&mids);
printf("Final mark:");
scanf("%d",&fin);
grade=0.20*lab+0.30*mids+0.50*fin;
printf("Final grade:%d",grade);
}
getch();
}
25.
GCD
#include<stdio.h>
void main()
{
int a,b,x,y,i,gcd;
clrscr();
printf("Enter the number:");
scanf("%d %d",&a,&b);
for(i=1;i<=a && i<=b;i++)
{
x=a%i;
y=b%i;
if(x==0 && y==0)
gcd=i;
}
printf("%d",gcd);
getch();
}
26.
#include<stdio.h>
void main()
{
int i,j,k=1,n;
clrscr();
printf("Enter the number of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d \t",k);
k++;
}
printf("\n");
}
getch();
}
27.
#include<stdio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("Enter the number of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=1;k<=n-i;k++)
{
printf(" ");
}
for(j=1;j<=(2*i)-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
27
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,m,n;
printf("Enter the order of matrix \n");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter A(%d,%d)",i+1,j+1);
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter B(%d,%d)",i+1,j+1);
scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Matrix A and Matrix B are \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf(" %d",a[i][j]);
printf("\t");
for(j=0;j<n;j++)
printf(" %d",b[i][j]);
printf("\n");
}
printf("\n Added matrix \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf(" %d",c[i][j]);
printf("\n");
}
return 0;
}
28. #include<stdio.h>
int main()
{
int minin,n,i,j,num[10],temp;
printf("Enter the number of items:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter number%d : ",i+1);
scanf("%d",&num[i]);
}
printf("\n The numbers before sort:\n");
for(i=0;i<n;i++)
printf("%d\t",num[i]);
for(i=0;i<n-1;i++)
{
minin=i;
for(j=i+1;j<n;j++)
{
if(num[j]<num[minin])
minin=j;
}
if(minin!=i)
{
temp=num[i];
num[i]=num[minin];
num[minin]=temp;
}
}
printf("\n The numbers after sort:\n");
for(i=0;i<n;i++)
printf("%d\t",num[i]);
Output:
29.Linear Search
#include<stdio.h>
int main()
{
int num[10],n,x,j,i;
printf("Enter the number of items:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter number%d : ",i+1);
scanf("%d",&num[i]);
}
j=1;
printf("Enter a number to be searched :");
scanf("%d",&x);
num[n+1]=x;
while(num[j]!=x)
j=j+1;
if(num[j]=num[n+1])
{
printf("Search was successful.");
printf("\nThe position in which %d located:%d\n",x,j+1);
}
Output;
30
#include<stdio.h>
int main()
{
int num[10],n,x,j,i,low,high,middle;
printf("Enter the number of items:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter number%d : ",i+1);
scanf("%d",&num[i]);
}
printf("Enter a number to be searched :");
scanf("%d",&x);
low=1;
high=n;
while(low<=high)
{
middle=(low+high)/2;
if(x<num[middle])
high=middle-1;
else if(x>num[middle])
low=middle+1;
else
{
printf("The position of %d is %d",x,middle+1);
return 0;
}
}
}
Output:
Enter the number of items:6
Enter number1 : 11
Enter number2 : 33
Enter number3 : 44
Enter number4 : 55
Enter number5 : 66
Enter number6 : 77
Enter a number to be searched :66
The position of 66 is 5
Process returned 0 (0x0) execution time : 13.751 s
Press any key to continue.
Quadratic eqn
#include<stdio.h>
#include<math.h>
int main()
{
float dis,R1,R2,disc;
int a,b,c;
printf("Write the coefficients of the quadratic equation:");
scanf("%d %d %d",&a,&b,&c);
if (a==0)
{
if(b==0)
{
if(c==0)
printf("The roots are arbitrary");
else
printf("There are no real roots");
}
else
printf("There is only one root which is :%f",(-c)/b);
}
else
{
dis=discriminant(a,b,c);
if(dis<0)
printf("There are no real roots");
else
if(dis==0)
{
R1=-b/(2*a);
printf("There is one real root :%f",R1);
}
else
{
R1=(-b+sqrt(dis))/(2*a);
R2=(-b-sqrt(dis))/(2*a);
printf("There are two real roots which are %f %f",R1,R2);
}
}
return 0;
}
int discriminant(int x,int y,int z)
{
float disc=(y*y)-(4*x*z);
return(disc);
}
Output
Write the coefficients of the quadratic equation:
1
6
9
There are two real roots which are -3 -3
Process returned 0 (0x0) execution time : 6.023 s
Press any key to continue.
Matrix multiplication:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k,l,m,n;
printf("Enter the order of matrix \n");
scanf("%d %d",&l,&m);
printf("Enter the number of rows in 2 nd matrix:");
scanf("%d",&n);
for(i=0;i<l;i++)
for(j=0;j<m;j++)
{
printf("Enter A(%d,%d)=",i+1,j+1);
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Enter B(%d,%d)=",i+1,j+1);
scanf("%d",&b[i][j]);
}
for(i=0;i<l;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("The resultant matrix when A and B are multplied \n");
printf("Matrix A and Matrix B are \n");
for(i=0;i<l;i++)
{
for(j=0;j<m;j++)
{
printf(" %d",a[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",b[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n Matrix C \n");
for(i=0;i<l;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",c[i][j]);
printf("\t");
}
printf("\n");
}
return 0;
}
2
2
Enter the number of rows in 2 nd matrix:3
Enter A(1,1)=1
Enter A(1,2)=2
Enter A(2,1)=3
Enter A(2,2)=4
Enter B(1,1)=2
Enter B(1,2)=3
Enter B(1,3)=4
Enter B(2,1)=5
Enter B(2,2)=2
Enter B(2,3)=3
The resultant matrix when A and B are multplied
Matrix A and Matrix B are
1 2
3 4
2 3 4
5 2 3
Matrix C
12 7 10
26 17 24
#include<stdio.h>
int main()
{
int a,b,choice;
printf("Enter the two operands:");
scanf("%d %d",&a,&b);
printf("Enter the operation you need to perform:\n1.Add\n2.Sub
\n3.Multiply\n4.Divide");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("%d+%d=%d",a,b,a+b);
break;
}
case 2:
{
printf("%d-%d=%d",a,b,a-b);
break;
}
case 3:
{
printf("%d*%d=%d",a,b,a*b);
break;
}
case 4:
{
printf("%d/%d=%d",a,b,a/b);
break;
}
default:
printf("Invalid choice");
}
return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
float dis,R1,R2,disc;
int a,b,c;
printf("Write the coefficients of the quadratic equation:");
scanf("%d %d %d",&a,&b,&c);
if (a==0)
{
if(b==0)
{
if(c==0)
printf("The roots are arbitrary");
else
printf("There are no real roots");
}
else
printf("There is only one root which is :%f",(-c)/b);
}
else
{
dis=discriminant(a,b,c);
if(dis<0)
printf("There are no real roots");
else
if(dis==0)
{
R1=-b/(2*a);
printf("There is one real root :%f",R1);
}
else
{
R1=(-b+sqrt(dis))/(2*a);
R2=(-b-sqrt(dis))/(2*a);
printf("There are two real roots which are %f %f",R1,R2);
}
}
return 0;
}
int main()
int met,x,y,a,b;
scanf("%d",&met);
if(met>0&&met<500)
else if(met>501&&met<1000)
x=met-500;
y=20+(x*0.03);
else
a=met-1000;
b=35+(a*0.02);
return 0;
#include<stdio.h>
int main()
int a[10][10],c[10][10];
int i,j,m,n;
scanf("%d",&m);
for(i=0;i<m;i++)
for(j=0;j<m;j++)
printf("Enter A(%d,%d)=",i+1,j+1);
scanf("%d",&a[i][j]);
for(i=0;i<m;i++)
for(j=0;j<m;j++)
printf(" %d",a[i][j]);
printf("\t");
printf("\n");
printf("\n\n\n");
for(i=0;i<=m;i++)
for(j=0;j<=m;j++)
c[i][j]=a[j][i];
c[j][i]=a[i][j];
for(i=0;i<m;i++)
for(j=0;j<m;j++)
printf("%d",c[i][j]);
printf("\t");
printf("\n");
return 0;
Enter A(1,1)=1
Enter A(1,2)=2
Enter A(1,3)=4
Enter A(2,1)=5
Enter A(2,2)=67
Enter A(2,3)=8
Enter A(3,1)=89
Enter A(3,2)=4
Enter A(3,3)=5
1 2 4
5 67 8
89 4 5
1 5 89
2 67 4
4 8 5
#include<stdio.h>
int main()
int n,fact;
printf("Enter a number:");
scanf("%d",&n);
fact=factorial(n);
return 0;
int factorial(int x)
{
if(x==0)
return(1);
else
return(x*factorial(x-1));
Enter a number:5
ncr
#include<stdio.h>
int main()
int n,r,nf,rf,nrf,ncr;
scanf("%d",&n);
scanf("%d",&r);
nf=factorial(n);
rf=factorial(r);
nrf=factorial(n-r);
ncr=nf/(rf*nrf);
return 0;
int factorial(int x)
if(x==0)
return(1);
else
return(x*factorial(x-1));
}
#include<stdio.h>
int main()
int x,fibo;
scanf("%d",&x);
fibo=fibonacci(x);
return 0;
int fibonacci(int n)
if(n==1)
return(1);
else if(n==2)
return(1);
else
return (fibonacci(n-1)+fibonacci(n-2));
Enter a number: 6