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

CP C Programs

The document contains C program code snippets for various algorithms and data structures including: 1) Finding the greatest of three numbers, roots of quadratic equations, temperature conversion, factorial, Fibonacci series, palindrome, prime number check, Armstrong number check, switch case, matrix addition and multiplication, sorting an array, factorial using recursion, swapping two numbers using reference, printing a student marklist using structures, and demonstrating a union. 2) Each code snippet is preceded by a brief description and followed by sample input/output to demonstrate the program's functionality.

Uploaded by

Micheal Vincent
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

CP C Programs

The document contains C program code snippets for various algorithms and data structures including: 1) Finding the greatest of three numbers, roots of quadratic equations, temperature conversion, factorial, Fibonacci series, palindrome, prime number check, Armstrong number check, switch case, matrix addition and multiplication, sorting an array, factorial using recursion, swapping two numbers using reference, printing a student marklist using structures, and demonstrating a union. 2) Each code snippet is preceded by a brief description and followed by sample input/output to demonstrate the program's functionality.

Uploaded by

Micheal Vincent
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

Greatest Of Three numbers

#include<stdio.h> #include<conio.h> void 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 c 45 67 78 78 is greater

Roots Of The Quadratic Equations

#include<stdio.h> #include<conio.h> #include<math.h> void 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,c 2 3 1 the roots are -0.500000 and 1.000000

Conversion of centigrade to Fahrenheit and vice versa

#include<stdio.h> #include<conio.h> void main() 2

{ 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: 36 fahrenheit equivalent is: 96.800003 enter the temperature in fahrenheit: 96 centigrade equivalent is : 35.555557

Factorial Of The Given Number

#include<stdio.h> #include<conio.h> void main() { 3

int fact=1,i,num; clrscr(); printf("Enter the value for num \n"); scanf("%d",&num); for(i=1;i<=num;i++) { fact=fact*i; } printf("The factorial of %d is %d",num,fact); getch(); }

Output Enter the value for num 5 The factorial of 5 is 120

Fibonacci series #include<stdio.h> #include<conio.h> void main() { int num,i,fib=0,a=0,b=1; clrscr(); printf("Enter the value for num \n"); scanf("%d",&num); for(i=0;i<num;i++) { 4

fib=fib+a; a=b; b=fib; printf("The fibonacci series is %d\n ",fib); } getch(); } Output: Enter the value for num 5 The fibonacci series is 0 The fibonacci series is 1 The fibonacci series is 1 The fibonacci series is 2 The fibonacci series is 3

Palindrome #include<stdio.h> #include<conio.h> 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

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 12521 the reverse of the 12521 is 12521 the given number is palindrome

Prime number #include<stdio.h> #include<conio.h> void main() { int num,i=2; clrscr(); printf("enter the value for num"); scanf("%d",&num); while(i<=num-1) { if(num%i==0) { printf("the given number is not a prime number "); break; } i++; } if(i==num) { printf("the given number is a prime number"); 6

} getch(); } Output enter the value for num 78 the given number is not a prime number

Armstrong Number #include<stdio.h> #include<conio.h> void 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

} getch(); } Output enter the number 153 armstrong number

Switch Case #include<stdio.h> #include<conio.h> #include<math.h> void 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

");

break; default: printf("invalid code"); break; } getch(); } Output: Menu 1 addition 2 subtraction 3 multiplication 4 division enter your choice :4 enter the two numbers a and b 100 5 division : 20 Matrix Addition #include<stdio.h> #include<conio.h> void 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<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("\nenter the elements of matrix B"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); } } printf("\nthe elements of matrix A are"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<n;j++) { printf("\t%d",a[i][j]); } 9

} printf("\nthe elements of matrix B are"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<n;j++) { printf("\t%d",b[i][j]); } } printf("\nthe addition of two matrices"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; printf("\t%d",c[i][j]); } } getch(); } Output enter the no of rows and column 2 2 enter the elements of matrix A 2 2 2 2 enter the elements of matrix B 2 2 2 2 The 2 2 The 2 2 elements of matrix A 2 2 elements of matrix B 2 2

The addition of matrix 4 4 4 4

10

Matrix Multiplication #include<stdio.h> #include<conio.h> void 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<m;i++) { for(j=0;j<n;j++) { scanf("\t%d",&a[i][j]); } } printf("enter the values for matrix B"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("\t%d",&b[i][j]); } } printf("elements of matrix A"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("\t%d",a[i][j]); } } printf("elements of matrix B"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("\t%d",b[i][j]); } } for(i=0;i<m;i++) { 11

for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<m;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } printf("matrix multiplication"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("\t%d",c[i][j]); } } getch(); } Output: enter the rows and columns of matrix A and B 2 2 enter the values of matrix A 2 2 2 2 enter the values for matrix B 2 2 2 2 elements of matrix A 2 2 2 2 elements of matrix B 2 2 2 2 matrix multiplication 8 8 8 8

12

Sort the Array elements #include<stdio.h> #include<conio.h> void 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<no;i++) { scanf("%d",&num[i]); } for(i=0;i<no-1;i++) { for(j=i+1;j<no;j++) { if(num[i]>num[j]) { a=num[i]; num[i]=num[j]; num[j]=a; } } } printf(" the ascending order of the given array"); for(i=0;i<no;i++) { printf(" \n%d",num[i]); } for(j=no-1;j>=0;j--) { printf("\n%d",num[j]); } getch(); }

13

Output enter the limit no 5 enter the number 67 23352 0 455 -12 the ascending order of the given array -12 0 67 455 23352 the descending order of the given array 23352 455 67 0 -12

Factorial using recursion #include<stdio.h> #include<conio.h> rec(int n) 14

{ 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 5 the factorial of the given number is 120

Swapping the two numbers using reference #include<stdio.h> #include<conio.h> void display(int *a,int *b) { int t; t=*a; 15

*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 After swapping 10 5 5 10

Print the student marklist using structure #include<stdio.h> #include<conio.h> void main() { int i=-1,j=0,op; struct student { char name[10]; int rno,s1,s2,s3,tot; float avg; }; 16

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<=i) { printf("\n\n name %s \n rollno %d \n total %d \naverage %f",stud[j].name,stud[j].tot,stud[j].avg); j++; } getch(); } Output enter your name xxx enter roll no 101 enter three marks 34 45 56 do you want to continue press 1 2 name xxx rollno 101 total 135 average 45.000000 Union #include<stdio.h> #include<conio.h> union marks { float perc; char grade; }; void main() { union marks student1; student1.perc=98.5; clrscr(); printf("Marks are %f address is %16lu\n",student1.perc,&student1.perc); student1.grade='A'; printf("Grade is %c address is %16lu\n",student1.grade,&student1.grade); getch(); 17

} Output: Marks are 98.500000 address is Grade is A address is 4325362 65522

18

You might also like