Upload
micheal-vincent
View
215
Download
0
Embed Size (px)
Citation preview
7/30/2019 Cp c Programs(1)
1/18
Greatest Of Three numbers
#include#includevoid main(){
int a,b,c;clrscr();printf("enter the values for a , b and c\n");scanf("%d%d%d",&a,&b,&c);if(a>b){
if(a>c){printf("%d is greater",a);
}else{printf("%d is greater",c);}
}else
{if(b>c)
{printf("%d is greater",b);
}else
{printf("%d is greater",c);
}}
getch();}
Output
enter the values for a , b and c45677878 is greater
Roots Of The Quadratic Equations
1
7/30/2019 Cp c Programs(1)
2/18
#include#include#includevoid main(){
int a,b,c,d;float root1,root2;clrscr();printf("enter the values of a,b,c\n");scanf("%d%d%d",&a,&b,&c);d=b*b-4*a*c;if(d>=0){
root1=(-b+sqrt(d))/(2*a);root2=(b+sqrt(d))/(2*a);printf("the roots are %f\n and %f\n ",root1,root2);
}
else{
printf("the roots are imaginary");}
getch();}
Output:
enter the values of a,b,c2 3 1the roots are -0.500000
and 1.000000
Conversion of centigrade to Fahrenheit and vice versa
#include#includevoid main()
2
7/30/2019 Cp c Programs(1)
3/18
{float c,f,cn,fn;clrscr();printf("enter the temperature in centigrdade");scanf("%f",&c);fn=1.8*c+32;printf("fahrenheit equivalent is:%f\n",fn);printf("enter the temperature in fahrenheit:");scanf("%f",&f);cn=(f-32)/1.8;printf("centigrade equivalent is :%f",cn);getch();}
Output:
enter the temperature in centigrade: 36fahrenheit equivalent is: 96.800003enter the temperature in fahrenheit: 96centigrade equivalent is : 35.555557
Factorial Of The Given Number
#include#includevoid main(){
3
7/30/2019 Cp c Programs(1)
4/18
int fact=1,i,num;clrscr();printf("Enter the value for num \n");
scanf("%d",&num);for(i=1;i
7/30/2019 Cp c Programs(1)
5/18
fib=fib+a;a=b;b=fib;printf("The fibonacci series is %d\n ",fib);
}
getch();}
Output:
Enter the value for num5
The fibonacci series is 0 The fibonacci series is 1The fibonacci series is 1
The fibonacci series is 2The fibonacci series is 3
Palindrome
#include#include
void main(){int a,num,rnum=0,rem;clrscr();printf("enter the value for num");scanf("%d",&num);a=num;while(num!=0){
rem=num%10;rnum=rnum*10+rem;num=num/10;
}printf("the reverse of the %d is %d\n",a,rnum);
5
7/30/2019 Cp c Programs(1)
6/18
if(a==rnum){
printf("the given number is palindrome\n");}
else{
printf("the given number is not the palindrome\n");}
getch();}
Output
enter the value for num 12521the reverse of the 12521 is 12521the given number is palindrome
Prime number
#include#includevoid main(){
int num,i=2;clrscr();
printf("enter the value for num");scanf("%d",&num);while(i
7/30/2019 Cp c Programs(1)
7/18
}getch();
}
Output
enter the value for num 78the given number is not a prime number
Armstrong Number
#include#includevoid main(){int a,b,c=0,e;clrscr();printf("enter the number");scanf("%d",&a);
e=a;while(a>0){b=a%10;c=c+(b*b*b);a=a/10;}if(e==c){printf(" armstrong number");}else{printf("not an armstrong number");
7
7/30/2019 Cp c Programs(1)
8/18
}getch();}
Output
enter the number 153armstrong number
Switch Case
#include#include#includevoid main(){int a,b,c,n;clrscr();printf("Menu\n 1 addition\n 2 subtraction 3 multiplication 4 division ");printf("enter your choice :\n");scanf("%d",&n);printf("enter the two numbers a and b\n");scanf("%d%d",&a,&b);switch(n){case 1:
c=a+b;printf("addition :%d\n",c);break;case 2:
c=a-b;printf("subtraction :%d\n",c);break;
case 3:c=a*b;
printf("multiplication :%d\n",c);break;
case 4:c=a/b;printf("division : %d\n",c);
8
7/30/2019 Cp c Programs(1)
9/18
break;default:
printf("invalid code");break;
}getch();}
Output:Menu1 addition2 subtraction
3 multiplication4 divisionenter your choice :4enter the two numbers a and b1005
division : 20
Matrix Addition
#include#includevoid main(){
int a[20][20],b[20][20],c[20][20],i,j,m,n;clrscr();printf("enter the no of rows and column\n");scanf("%d%d",&m,&n);printf("enter the elements of matrix A");for(i=0;i
7/30/2019 Cp c Programs(1)
10/18
}printf("\nthe elements of matrix B are");for(i=0;i
7/30/2019 Cp c Programs(1)
11/18
Matrix Multiplication
#include#includevoid main(){int a[10][10],b[10][10],c[10][10],i,j,k;
int m,n;printf("enter the rows and columns of matrix A and B");scanf("%d%d",&m,&n);printf("enter the values of matrix A");for(i=0;i
7/30/2019 Cp c Programs(1)
12/18
for(j=0;j
7/30/2019 Cp c Programs(1)
13/18
Sort the Array elements
#include#includevoid main(){
int num[20],no,i,j,a;clrscr();printf("enter the limit no");scanf("%d",&no);printf("enter the number");for(i=0;i
7/30/2019 Cp c Programs(1)
14/18
Output
enter the limit no 5enter the number 67233520455-12the ascending order of the given array
-12067455
23352the descending order of the given array23352455670-12
Factorial using recursion
#include#includerec(int n)
14
7/30/2019 Cp c Programs(1)
15/18
{int fact=1;if(n==1){return 1;}else{fact=n*rec(n-1);return fact;}}void main(){int num,a;clrscr();printf("enter the number");
scanf("%d",&num);a=rec(num);printf("the factorial of the given number is %d",a);getch();
}
Output:
enter the number 5the factorial of the given number is 120
Swapping the two numbers using reference
#include#includevoid display(int *a,int *b){int t;t=*a;
15
7/30/2019 Cp c Programs(1)
16/18
*a=*b;*b=t;}void main(){int x,y;clrscr();printf("enter the two numbers");scanf("%d%d",&x,&y);display(&x,&y);printf("after swapping%d and %d",x,y);getch();}
Output:
Enter the two numbers 5 10
After swapping 10 5
Print the student marklist using structure
#include#includevoid main(){int i=-1,j=0,op;struct student{char name[10];int rno,s1,s2,s3,tot;float avg;};
16
7/30/2019 Cp c Programs(1)
17/18
struct student stud[10];clrscr();do{i++;printf("enter your name");scanf("%s",stud[i].name);printf("enter roll no");scanf("%d",&stud[i].rno);printf(" enter three marks");scanf("%d%d%d",&stud[i].s1,&stud[i].s2,&stud[i].s3);stud[i].tot=stud[i].s1+stud[i].s2+stud[i].s3;stud[i].avg=stud[i].tot/3;printf("do you want to continue press 1");scanf("%d",&op);}while(op==1);
while(j
7/30/2019 Cp c Programs(1)
18/18
}
Output:
Marks are 98.500000 address is 65522Grade is A address is 4325362
18