Upload
harinee
View
215
Download
0
Embed Size (px)
DESCRIPTION
computer programs c language
Citation preview
TO CALCULATE SUM OF TWO NUMBERS
#include<stdio.h>#include<conio.h>void main(){ int a,b,sum;
printf(“Enter any two numbers\n”);scanf(“%d%d”,&a,&b);sum=a+b;printf(“The sum of a and b is :%d”,sum);getch();
}
OUTPUT:Enter any two numbers23The sum of a and b is : 5
TO CALCULATE THE AREA OF A CIRCLE
#include<stdio.h>#include<conio.h>void main(){ int r; float circle;
printf(“Enter the radius of the circle\n”);scanf(“%d”,&r);circle=3.14*r*r;printf(“The area of the circle is:%f\n”,circle);getch();
}
OUTPUT:Enter the radius of the circle10The area of the circle is:314.000000
BIGGEST OF THREE NUMBERS
#include<stdio.h>#include<conio.h>void main(){
int a,b,c;clrscr();printf("Enter any three numbers:");scanf("%d%d%d",&a,&b,&c);if(a>b&&a>c){
printf("A is big\n");}else if(b>c){
printf("B is big\n");}else{
printf("C is big\n" );}getch();
}
OUTPUT:Enter any three numbers: 104030B is big
TO FIND THE LEAP YEAR OR NOT
#include<stdio.h>#include<conio.h>void main(){ int year;
printf(“Enter the year”); scanf(“%d”,&year);
if(year%4==0){ printf(“Given the year is leap year\n”);}else{printf(“Given the year is not a leap year\n”);}getch();}
OUTPUT:Enter the year2012Given the year is leap year
Enter the year2010Given the year is not a leap year
TO CONVERT CELICIUS TO FARENHEIT
#include<stdio.h>#include<conio.h>void main(){
float celsi,fahren;clrscr();printf("enter the celius value:");scanf("%f",&celsi);fahren=(1.8*celsi)+32;printf("the fahrenheit value of the given %f celsius
value is %f",celsi,fahren);getch();
}
OUTPUT:Enter the celius value:28The fahrenheit value of the given 28.000000 Celsius value is 82.400002
TO FIND WHETHER GIVEN NUMBER IS EVEN OR ODD
#include<stdio.h>#include<conio.h>void main(){ int n;
printf("Enter any numbers\n”);scanf("%d",&n);
if(n%2==0){
printf("Given number is Even\n”);}else{
printf("Given number is Odd\n”);}
getch();}
OUTPUT:Enter any numbers5Given number is Odd
Enter any numbers6Given number is Even
TO FIND THE SQUARE ROOT OF A NUMBER
#include<stdio.h>#include<conio.h>void main(){ int n,square;
printf("Enter any numbers\n”);scanf("%d",&n);square=n*n;printf("The square of the given number is :%d\n”,square);getch();
}
OUTPUT:Enter any numbers4The square of the given number is: 16
TO CHECK WHETHER THE GIVEN NUMBER IS AMSTRONG OR NOT
#include<stdio.h>#include<conio.h>void main(){
int n,r,m,sum,arms;clrscr();printf("Enter the numbers:");scanf("%d",&n);arms=n;sum=0;while(n>0){r=n%10;sum=sum+r*r*r;n=n/10;}if(arms==sum){printf("Given number is an Amstrong\n”);}else{printf("Given number is not an Amstrong\n”);}getch();}
OUTPUT:Enter the numbers153Given number is an Amstrong
TO FIND NUMBER IS PALINDROME OR NOT
#include<stdio.h>#include<conio.h>void main(){
int n,r,Rev,pal;clrscr();printf("Enter the number:");scanf("%d",&n);pal=n;Rev=0;while(n>0){
r=n%10;Rev=Rev*10+r;n=n/10;
}if(pal==Rev){
printf("Given number is palidrome");}else{
printf("Given number is not a palidrome");}getch();
}OUTPUT:Enter the number:345Given number is not a palidrome
Enter the number:151Given number is palidrome
TO FIND FACTORIAL OF A GIVEN NUMBER
#include<stdio.h>#include<conio.h>#include<math.h>void main(){
int n,fact,i;clrscr();printf("Enter the numbers:");scanf("%d",&n);fact=1;for(i=1;i<=n;i++){
fact=fact*i;}printf("The factorial value is:%d",fact);getch();
}
OUTPUT:
Enter the numbers:6The factorial value is:720
TO FIND ROOTS OF QUADRATIC EQUATION
#include<stdio.h>#include<conio.h>#include<math.h>void main(){
int a,b,c,d;float r1,r2;clrscr();printf("Enter the value A:");scanf("%d",&a);printf("Enter the value of B:");scanf("%d",&b);printf("Enter the value of C:");scanf("%d",&c);d=(b*b)-(4*a*c);if(d>=0){
r1=(-b+sqrt(d))/(2*a);r2=(+b+sqrt(d))/(2*a);printf("The Root 1 value is:%f\n",r1);printf("The Root 2 value is:%f",r2);
}else{
printf("The Roots are imaginary”);}getch();
}
OUTPUT:
Enter the value A:5Enter the value of B:20Enter the value of C:6The Root 1 value is:-0.326680The Root 2 value is:3.673320
Enter the value A:20Enter the value of B:6Enter the value of C:5The Roots are imaginary
Enter the value A:4Enter the value of B:4Enter the value of C:1The Root 1 value is:-0.500000The Root 2 value is:0.500000
TO REVERSE THE DIGITS OF A GIVEN NUMBER
#include<stdio.h>#include<conio.h>void main(){
int n,r,Rev,pal;clrscr();printf("Enter the number:");scanf("%d",&n);pal=n;Rev=0;while(n>0){
r=n%10;Rev=Rev*10+r;n=n/10;
}printf(“Reverse of given number is %d”,Rev);getch();
OUTPUT:Enter the number:345Reverse of given number is 543
TO FIND THE SUM OF DIGITS OF A GIVEN NUMBERS
#include<stdio.h>#include<conio.h>void main(){
int n,r,sum;clrscr();printf("Enter the number:");scanf("%d",&n);sum=0;while(n>0){r=n%10;sum=sum+r;n=n/10;}printf(“The sum of digits of a given number is :%d\
n”,sum);getch();}
OUTPUT:
Enter the number12345The sum of digits of a given number is :15
TO GENERATE FIBONNACI SERIES
#include<stdio.h>#include<conio.h>void main(){
int n,fib,x,y,i;clrscr();printf("Enter the limit\n");scanf("%d",&n);x=0;y=1;printf("Generarte the fibonacci series\n");printf("%d\n%d\n",x,y);for(i=2;i<n;i++){
fib=x+y;x=y;y=fib;printf("%d\n",fib);
}getch();
}
OUTPUT:Enter the limit:5Generarte the fibonacci series01123
IMPLEMENTATION OF PASCAL TRIANGLE
#include<stdio.h>#include<conio.h>void main(){
int noline,i,j,temp;clrscr();printf("Enter the number of lines to print:");scanf("%d",&noline);printf("\n");for(i=1;i<=noline;i++){
for(j=1;j<=noline-i;j++)printf(" ");
temp=i;for(j=1;j<=i;j++)
printf("%4d",temp++);temp=temp-2;for(j=1;j<i;j++)
printf("%4d",temp--);printf("\n\n");
}getch();
}
OUTPUT:
Enter the number of lines to print:5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
LARGEST AND SMALLEST ElEMENT OF A GIVEN ARRAY
#include<stdio.h>#include<conio.h>void main(){
int a[100],i,large,small,no;clrscr();printf("Lagest & Smallest of an array\n\n");printf("-------------------------------\n\n");printf("In how many numbers do u want to find:");scanf("%d",&no);printf("Enter the elements of the array:");for(i=0;i<no;i++)
scanf("%d",&a[i]);printf("The Elements Of The Array:\n");for(i=0;i<no;i++)
printf("%d\t",a[i]);small=a[0];large=a[0];for(i=0;i<no;i++){
if(a[i]>large)large=a[i];
else if(a[i]<small)small=a[i];
}printf("The largest of the given array is %d\n",large);printf("The smallest of the given array is %d\n",small);getch();
}
OUTPUT:Lagest & Smallest of an array
-------------------------------
In how many numbers do u want to find:5Enter the elements of the array:1234568743
The Elements Of The Array:
12 34 56 87 43
The largest of the given array is 87
The smallest of the given array is 12
TO FIND ASCENDING & DESCENDING ORDER OF ELEMENTSUSING ARRAY
#include<stdio.h>#include<conio.h>void main(){
int num[100],no,i,j,a;clrscr();printf("Enter the no. of elements for the array:");scanf("%d",&no);printf("Enter the numbers:");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("\nThe ascending order of the given numbers-");for(i=0;i<no;i++)
printf("\n%d",num[i]);printf("\nThe descending number of the given numbers-");for(j=no-1;j>=0;j--)
printf("\n%d",num[j]);getch();
}
OUTPUT:
Enter the no. of elements for the array:5Enter the numbers:1030506020The ascending order of the given numbers-6050302010The descending number of the given numbers-1020305060
MATRIX ADDITION#include<stdio.h>#include<conio.h>void main(){
int a[10][10],b[10][10],c[10][10],i,j,row,col;clrscr();printf("Enter the row Size:");scanf("%d",&row);printf("Enter the column Size:");scanf("%d",&col);printf("Enter the A matrix:");for (i=1;i<=row;i++){
for(j=1;j<=col;j++){
scanf("%d",&a[i][j]);}
}printf("Enter the B matrix:");for(i=1;i<=row;i++){
for(j=1;j<=col;j++){
scanf("%d",&b[i][j]);}
}for(i=1;i<=row;i++){
for(j=1;j<=col;j++){
c[i][j]=a[i][j]+b[i][j];}
}
printf("A matrix :\n");for(i=1;i<=row;i++){
printf("\t");for(j=1;j<=col;j++){
printf("%d\t",a[i][j]);}printf("\n");
}printf("B matrix :\n");for(i=1;i<=row;i++){
printf("\t");for(j=1;j<=col;j++){
printf("%d\t",b[i][j]);}printf("\n");
}printf("The addition of two matrix is:\n");for(i=1;i<=row;i++){
printf("\t");for(j=1;j<=col;j++){
printf("%d\t",c[i][j]);}printf("\n");
}getch();
}
OUTPUT:Enter the row Size:3Enter the column Size:3Enter the A matrix:123456789Enter the B matrix:123456789A matrix : 1 2 3 4 5 6 7 8 9B matrix : 1 2 3 4 5 6 7 8 9The addition of two matrix is: 2 4 6 8 10 12 14 16 18
MATRIX MULTIPLICATION#include<stdio.h>#include<conio.h>void main(){ int a[10][10],b[10][10],c[10][10],i,j,k,row,col;
clrscr();printf("Enter the row Size:");scanf("%d",&row);printf("Enter the column Size:");scanf("%d",&col);printf("Enter the A matrix:");for (i=1;i<=row;i++){
for(j=1;j<=col;j++){
scanf("%d",&a[i][j]);}
}printf("Enter the B matrix:");for(i=1;i<=row;i++){
for(j=1;j<=col;j++){
scanf("%d",&b[i][j]);}
}for(i=1;i<=row;i++){
for(j=1;j<=col;j++){
c[i][j]=0;for(k=1;k<=row;k++){
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}}
}printf("\nA matrix-");for(i=1;i<=row;i++){
printf("\t");for(j=1;j<=col;j++){
printf("%d\t",a[i][j]);}printf("\n");
}printf("\nB matrix-");for(i=1;i<=row;i++){
printf("\t");for(j=1;j<=col;j++){
printf("%d\t",b[i][j]);}printf("\n");
}printf("The multiplication of two matrix is:\n");for(i=1;i<=row;i++){
printf("\t");for(j=1;j<=col;j++){
printf("%d\t",c[i][j]);}printf("\n");
}getch();}
OUTPUT:Enter the row Size:3Enter the column Size:3Enter the A matrix:123456789Enter the B matrix:123456789
A matrix- 1 2 3 4 5 6 7 8 9B matrix- 1 2 3 4 5 6 7 8 9
The multiplication of two matrix is: 30 36 42 66 81 96 102 126 150
Transpose of a Matrix
#include<stdio.h>#include<conio.h>void main(){
int i,j,a[25][25],row,col;clrscr();printf("\nEnter the number of rows and columns for the
matrix:");scanf("%d%d",&row,&col);printf("Enter the elements of the matrix:");for(i=0;i<row;i++){
for(j=0;j<col;j++)scanf("%d",&a[i][j]);
}printf("The given matrix-\n");for(i=0;i<row;i++){
printf("\n");for(j=0;j<col;j++)
printf("\t%d",a[i][j]);}printf("\n\nThe transpose of the given matrix-\n");for(i=0;i<row;i++){
printf("\n");for(j=0;j<col;j++)
printf("\t%d",a[j][i]);}getch();
}
OUTPUT:
Enter the number of rows and columns for the matrix:33Enter the elements of the matrix:123456789The given matrix-
1 2 34 5 67 8 9
The transpose of the given matrix-
1 4 72 5 83 6 9
MENU DRIVEN PROGAM USING SWITCH CASE
#include<stdio.h>#include<conio.h>#include<process.h>void main(){
int a,b,c,n;clrscr();printf("--MENU--\n");printf("1.ADDITION\n");printf("3.SUBTRACTION\n");printf("4.DIVISION\n");printf("5.EXIT\n");printf("Enter your choice:");scanf("%d",&n);printf("Enter two numbers:");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);break;
case 5:exit(0);break;
default:printf("Invalid operation code");
}getch();
}
OUTPUT:--MENU--1.ADDITION2.SUBTRACTION3.MULTIPLICATION4.DIVISION5.EXITEnter your choice:1Enter two numbers:34Addition:7
--MENU--1.ADDITION2.SUBTRACTION3.MULTIPLICATION4.DIVISION5.EXITEnter your choice:2Enter two numbers:73Subtraction:4
--MENU--1.ADDITION2.SUBTRACTION3.MULTIPLICATION4.DIVISION5.EXITEnter your choice:3Enter two numbers:45Multiplication:20
--MENU--1.ADDITION2.SUBTRACTION3.MULTIPLICATION4.DIVISION5.EXITEnter your choice:4Enter two numbers:204Division:5
TO FIND GIVEN NUMBER IS PRIME OR NOT
#include<stdio.h>#include<conio.h>#include<math.h>void main(){
int n,i;clrscr();printf("Enter the number:");scanf("%d",&n);for(i=2;i<n;i++){
if(n%i==0){
printf("The Given number is not a prime\n");getch();exit(0);
}}printf("The Given number is a prime\n");getch();
}
OUTPUT:Enter the number5The Given number is a prime
Enter the number4Given number is not a prime
TO DISPLAY STUDENT DETAILS USING STRUCTURE
#include<stdio.h>#include<conio.h>typedef struct stud{
int rno,mark;char name[10];
}student;student s[10];void main(){
int i,n;clrscr();printf("Enter the No. of students:");scanf("%d",&n);printf("Enter the student details\n");printf("Enter the roll no ,name and mark:");for(i=1;i<=n;i++){
printf(“\nStudent %d\n“,i); scanf("%d%s%d",&s[i].rno,&s[i].name,&s[i].mark);
}printf("\n");printf("Student details are:\n\n");printf("\t rollno\tname\t\tmarks\n");for(i=1;i<=n;i++)printf("\t%d\t\t%s\t\t%d\n",s[i].rno,s[i].name,s[i].mark);getch();
}
OUTPUT:Enter the No. of students:4Enter the student detailsEnter the roll no ,name and mark:Student 11Mani80Student 22Shankar56Student 33Anu97Student 44Deepika76
Student details are:Rollno Name Marks1 mani 802 Shankar 563 anu 974 Deepika 76
IMPLEMENTATION OF UNION
#include<stdio.h>#include<conio.h>union marks{ float perc;
char grade;};void main(){
union marks student;stud.perc=98.5;stud.grade='A';clrscr();printf("\nMark %f stored in address %lu\n",stud.perc,
&stud.perc); printf("\nGrade %c is stored in address%lu\n",stud.grade,
&stud.grade);getch();
}
OUTPUT:
Mark 98.500496 is stored in address 4325362
Grade A is stored in address 4325362
Swapping of Two Variables using Call By Value
#include<stdio.h>#include<conio.h>void main(){
int x,y;clrscr();printf("\nEnter the two numbers: ");scanf("%d %d",&x,&y);printf("\nBefore Swapping\n\nThe value of x=%d y=%d\
n",x,y);display(x,y);getch();
}display(int a,int b){
int t;t=a;a=b;b=t;printf("\nAfter swapping\n\nThe value of x=%d y=%d",a,b);
}
OUTPUT:
Enter the two numbers:35
Before Swapping
The value of x=3 y=5
After swapping
The value of x=5 y=3
Swapping of Two Variables using Call By Reference
#include<stdio.h>#include<conio.h>void main(){
int x,y;clrscr();printf("\nEnter the two numbers: ");scanf("%d %d",&x,&y);printf("\nBefore Swapping\n\nThe value of x=%d y=%d\
n",x,y);display(&x,&y);printf("\nAfter swapping\n\nThe value of x=%d y=%d",x,y);getch();
}display(int *a,int *b){
int t;t=*a;*a=*b;*b=t;
}
OUTPUT:
Enter the two numbers: 3 4
Before Swapping
The value of x=3 y=4
After swapping
The value of x=4 y=3