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

All

The document contains 20 code snippets of C programs that demonstrate various programming concepts like finding the average of marks, checking divisibility by a number, determining grade based on marks, checking even/odd number, finding largest of 3 numbers, checking prime number, calculating factorial, printing tables, calculating sum of numbers, power calculation, if-else conditions, square root, payment calculation, palindrome checking, perfect number checking, Armstrong number checking, variable exchange. Each code is followed by sample input/output demonstrating its execution.

Uploaded by

apoorvaereddy
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)
41 views

All

The document contains 20 code snippets of C programs that demonstrate various programming concepts like finding the average of marks, checking divisibility by a number, determining grade based on marks, checking even/odd number, finding largest of 3 numbers, checking prime number, calculating factorial, printing tables, calculating sum of numbers, power calculation, if-else conditions, square root, payment calculation, palindrome checking, perfect number checking, Armstrong number checking, variable exchange. Each code is followed by sample input/output demonstrating its execution.

Uploaded by

apoorvaereddy
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/ 26

1.

/* Program to find the average of given marks using while */


#include<stdio.h>
void main()
{
int mark,no=0;
float sum=0,avg;
clrscr();
printf(" Enter your marks : enter -1 at the end \n");
scanf("%d",&mark);
while(mark!=-1)
{
sum+=mark;
no++;
scanf("%d",&mark);
}
avg=sum/no;
printf("The average is %2f",avg);
getch();
}

Enter your marks : enter -1 at the end


95
96
99
100
98
-1
The average is 97.599998
Process returned 0 (0x0) execution time : 22.016 s

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

/* PROGRAM TO FIND LARGEST OF THREE NUMBERS*/


#include<stdio.h>
void main()
{
int n1,n2,n3;
clrscr();
printf("Enter 3 numbers:\n");
scanf("%d %d %d",&n1,&n2,&n3);
if(n1>n2)
{
if(n1>n3)
{
printf("%d is the largest\n",n1);
{
if(n2<n3)
printf("%d is the smallest\n",n2);
else
printf("%d is the smallest\n",n3);
}
}
else
{
printf("%d is the largest\n",n3);
printf("%d is the smallest\n",n2);
}
}
else
{
if(n2>n3)
{
printf("%d is the largest\n",n2);
{
if(n1<n3)
printf("%d is the smallest\n",n1);
else
printf("%d is the smallest\n",n3);
}
}
else
{
printf("%d is the largest\n",n3);
{
if(n1<n2)
printf("%d is the smallest\n",n1);
else
printf("%d is the smallest\n",n2);
}
}
}
getch();
}

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

7. /*program to find whether the no. is prime or not*/


#include<stdio.h>
void main()
{
int i,a,count=0;
clrscr();
printf("Enter a number:\n");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
if(a%i==0)
count++;
}
if(count==2)
printf("Prime");
else
printf("Not Prime");
getch();
}
Enter a number:
5
Prime
Process returned 0 (0x0) execution time : 4.047 s

Enter a number:
81
Not Prime
Process returned 0 (0x0) execution time : 2.469 s

8/* Program to write factorial*/


#include<stdio.h>
void main()
{
int a,fact=1,i;
clrscr();
printf("Enter a number:\n");
scanf("%d",&a);
for(i=1;i<=a;i++)
fact=i*fact;
printf("The factorial of the number is %d",fact);
getch();
}

Enter a number:
7
The factorial of the number is 5040
Process returned 0 (0x0) execution time : 3.953 s

9./*program to find tables*/


#include<stdio.h>
void main()
{
int a,i,n;
clrscr();
printf("Enter a number:\n");
scanf("%d",&a);
for(i=1;i<=10;i++)
{
n=i*a;
printf("%d * %d= %d\n",i,a,n);
}
getch();
}

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

Process returned 0 (0x0) execution time : 11.609 s

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

12. Program to find power


#include<stdio.h>
void main()
{
int i,no,pow,count,pdt=1;
printf(" Enter a number:\n");
scanf("%d",&no);
printf("Enter the exponent value till it should get calculated:");
scanf("%d",&pow);
for(i=1;i<=pow;i++)
{
pdt=pdt*no;
printf("\n %d^%d=%d",no,i,pdt);
}
getch();
}

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",&current);
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

Divisor and current sum is 1 and 1


Divisor and current sum is 2 and 3
Divisor and current sum is 3 and 6
The number is perfect
Process returned 0 (0x0) execution time : 3.547 s

Enter a number:36

Divisor and current sum is 1 and 1


Divisor and current sum is 2 and 3
Divisor and current sum is 3 and 6
Divisor and current sum is 4 and 10
Divisor and current sum is 6 and 16
Divisor and current sum is 9 and 25
Divisor and current sum is 12 and 37
Divisor and current sum is 18 and 55
The number is not perfect
Process returned 0 (0x0) execution time : 2.937 s

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",&reg);
while(reg!=00)
{
printf("Registration number:");
scanf("%d",&reg);
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:

Enter the number of items:5


Enter number1 : 23
Enter number2 : 12
Enter number3 : 0
Enter number4 : 78
Enter number5 : 56

The numbers before sort:


23 12 0 78 56
The numbers after sort:
0 12 23 56 78
Process returned 0 (0x0) execution time : 9.683 s

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;

Enter the number of items:5


Enter number1 : 34
Enter number2 : 56
Enter number3 : 67
Enter number4 : 89
Enter number5 : 45
Enter a number to be searched :67
Search was successful.
The position in which 67 located:3

Process returned 0 (0x0) execution time : 12.606 s


Press any key to continue.

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.

Write the coefficients of the quadratic equation:0


0
0
The roots are arbitrary
Process returned 0 (0x0) execution time : 7.969 s
Press any key to continue.

Write the coefficients of the quadratic equation:3


5
1
There are two real roots which are -0.232408 -1.434259
Process returned 0 (0x0) execution time : 5.391 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

Process returned 0 (0x0) execution time : 13.493 s


Press any key to continue.

#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 discriminant(int x,int y,int z)


{
float disc=(y*y)-(4*x*z);
return(disc);
}
#include<stdio.h>

int main()

int met,x,y,a,b;

printf("Enter the meter readings:");

scanf("%d",&met);

if(met>0&&met<500)

printf("The rate is: $20");

else if(met>501&&met<1000)

x=met-500;
y=20+(x*0.03);

printf("The rate is: $%d",y);

else

a=met-1000;

b=35+(a*0.02);

printf("The rate is: $%d",b);

return 0;

#include<stdio.h>

int main()

int a[10][10],c[10][10];

int i,j,m,n;

printf("Enter the order of matrix \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");

printf("The transpose of the given matrix:\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 the order of matrix

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

The transpose of the given matrix:

1 5 89

2 67 4

4 8 5

Process returned 0 (0x0) execution time : 9.542 s

#include<stdio.h>

int main()

int n,fact;

printf("Enter a number:");

scanf("%d",&n);

fact=factorial(n);

printf("The factorial of the given number: %d",fact);

return 0;

int factorial(int x)

{
if(x==0)

return(1);

else

return(x*factorial(x-1));

Enter a number:5

The factorial of the given number: 120

Process returned 0 (0x0) execution time : 2.422 s

ncr

#include<stdio.h>

int main()

int n,r,nf,rf,nrf,ncr;

printf("Enter the value of n:");

scanf("%d",&n);

printf("Enter the value of r:");

scanf("%d",&r);

nf=factorial(n);

rf=factorial(r);

nrf=factorial(n-r);

ncr=nf/(rf*nrf);

printf("The NCR value is: %d",ncr);

return 0;

int factorial(int x)

if(x==0)

return(1);

else

return(x*factorial(x-1));
}

Enter the value of n:5

Enter the value of r:3

The NCR value is: 10

Process returned 0 (0x0) execution time : 4.234 s

#include<stdio.h>

int main()

int x,fibo;

printf("Enter a number: ");

scanf("%d",&x);

fibo=fibonacci(x);

printf("The number which is situated in the place of %d is :%d",x,fibo);

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

The number which is situated in the place of 6 is :8

Process returned 0 (0x0) execution time : 6.424 s

You might also like