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

Write A Program To Swap Two Numbers

The document contains C code for several programs: 1. A program to swap two numbers using a third variable. It takes input, performs the swap operation, and displays output. 2. A program to find the greatest of 6 numbers by comparing them and storing the greatest in a variable. 3. A menu driven program that performs basic math operations like addition, subtraction etc based on user input. It takes input, performs the operation, and displays output. 4. Several other programs that perform operations like checking if a number is Armstrong, evaluating a series, finding factorial using functions, generating Fibonacci series using recursion, searching an item in an array, and finding transpose of a matrix. All programs take required input

Uploaded by

jithm09
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)
129 views

Write A Program To Swap Two Numbers

The document contains C code for several programs: 1. A program to swap two numbers using a third variable. It takes input, performs the swap operation, and displays output. 2. A program to find the greatest of 6 numbers by comparing them and storing the greatest in a variable. 3. A menu driven program that performs basic math operations like addition, subtraction etc based on user input. It takes input, performs the operation, and displays output. 4. Several other programs that perform operations like checking if a number is Armstrong, evaluating a series, finding factorial using functions, generating Fibonacci series using recursion, searching an item in an array, and finding transpose of a matrix. All programs take required input

Uploaded by

jithm09
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/ 51

/*WRITE A PROGRAM TO SWAP TWO NUMBERS*/ #include<stdio.h> #include<conio.

h> void main() { int a,b,c; clrscr(); printf("Enter the value of a:\n"); scanf("%d",&a); printf("Enter the value of b:\n"); scanf("%d",&b); c=a+b; b=c-b; a=c-a; printf("AFTER EXCHANGE\na=%d\nb=%d",a,b); getch(); }

OUTPUT Enter the value of a: 34 Enter the value of b: 21 AFTER EXCHANGE a=21 b=34

ALGORITHM Step1 : Start Step2 : Accept a,b Step3 : c=a+b b=c-b a=c-a Step4 : Display a & b Step5 : Stop

/*WRITE A PROGRAM TO FIND THE GREATEST OF 6 NUMBERS*/ #include<stdio.h> #include<conio.h> void main() { int a,b,c,d,e,f,G1,G2; clrscr(); printf("Enter 6 numbers:\n"); scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f); if((a>b)&&(a>c)) G1=a; else if(b>c) G1=b; else G1=c; if((d>e)&&(d>f)) G2=d; else if(e>f) G2=e;

else G2=f; if(G1>G2) printf("The greatest number is %d",G1); else printf(The greatest number is %d,G2); getch(); }

OUTPUT Enter 6 numbers: 34 23 41 19 55 39 The greatest number is 55

ALGORITHM Step1 : Start Step2 : Accept a,b,c,d,e,f Step3 : if((a>b)&&(a>c)) G1=a Step4 : Otherwise if(b>c) G1=b Step5 : Otherwise G1=c Step6 : if((d>e)&&(d>f)) G2=d Step7 : Otherwise if(e>f) G2=e Step8 : Otherwise G2=f Step9 : if(G1>G2) Display G1 Step10: Otherwise Display G2

Step11: Stop

/*WRITE A PROGRAM TO CREATE A MENU DRIVEN PROGRAM*/ #include<stdio.h> #include<conio.h> #include<process.h> void main() { float a,b,S,D,P,Q; int ch; clrscr(); printf("1.ADDITION\n2.SUBTRACTION\n3.MULTIPLICATION\n4.DIVISION\n5.EXIT\n"); printf("Enter your choice:\n"); scanf("%d",&ch); if(ch!=5) { printf("Enter the values:\n"); scanf("%f%f",&a,&b); } switch(ch) {

case 1 : S=a+b; printf("SUM=%f",S); break; case 2 : D=a-b; printf("DIFFERENCE=%f",D); break; case 3 : P=a*b; printf("PRODUCT=%f",P); break; case 4 : Q=a/b; printf("QUOTIENT=%f",Q); break; case 5 : exit(0); default: printf("Invalid Option"); break; } getch(); }

OUTPUT 1.ADDITION 2.SUBTRACTION 3.MULTIPLICATION 4.DIVISION 5.EXIT Enter your choice: 4 Enter the values: 564 31 QUOTIENT=18.193548

ALGORITHM Step1 : Start Step2 :Accept a,b,ch Step3 : if(ch=1) S=a+b Display S Step4 : Otherwise if(ch=2) D=a-b Display D Step5 : Otherwise if(ch=3) P=a*b Display P Step6 : Otherwise if(ch=4) Q=a/b Display Q Step7 :Otherwise exit(0) Step8 : Stop

/*WRITE A PROGRAM TO CHECK WHETHER THE ENTERED STRING IS ARMSTRONG OR NOT*/ #include<stdio.h> #include<conio.h> void main() { int n,m,p,num,s=0; clrscr(); printf("Enter the value of n:\n"); scanf("%d",&n); num=n; while(n>0) { m=n%10; p=m*m*m; s=s+p; n=n/10; } printf("SUM=%d\n",s);

if(s==num) printf("The number is ARMSTRONG"); else printf("The number is NOT ARMSTRONG"); getch(); }

OUTPUT Enter the value of n: 153 SUM=153 The number is ARMSTRONG

ALGORITHM Step1 : Start Step2 : Accept n,m,p,num Step3 : Initialize s=0 Step4 : Assign num=n Step5 : Repeat Step6 until n>0 Step6 : m=n%10 p=m*m*m s=s+p n=n/10 Step7 : Display s Step8 : if(s=num) Display Number is ARMSTRONG Otherwise DisplayNumber is NOT ARMSTRONG Step9 : Stop

/*WRITE A PROGRAM TO EVALUATE THE SERIES 1+x+pow(x,2)+...+pow(x,n)*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { int n,x,i=0,s=0,a; clrscr(); printf("Enter the limit:\n"); scanf("%d",&n); printf("Enter the value of x:\n"); scanf("%d",&x); while(i<=n) { a=pow(x,i); s=s+a; printf("%d+",a); i++; }

printf("=%d",s); getch(); }

OUTPUT Enter the limit: 6 Enter the value of x: 3 1+3+9+27+81+243+729+=1093

ALGORITHM Step1 : Start Step2 : Accept x,n Step3 : Initialize s=0,i=0 Step4 : Repeat Step5 to Step 7 until i<=n Step5 : a=pow(x,i) Step6 : s=s+a Step7 : Display a Step8 : Display s Step9 : Stop

/*WRITE A PROGRAM TO FIND THE FACTORIAL OF A GIVEN NUMBER USING FUNCTION WITH ARGUMENTS AND RETURN TYPE*/ #include<stdio.h> #include<conio.h> #include<math.h> int fact(int); void main() { int n,fac; clrscr(); printf("Enter a number:\n"); scanf("%d",&n); fac=fact(n); printf("The factorial is:%d",fac); getch(); } fact(int n) { int i=1,f=1;

while(i<=n) { f=f*i; i++; } return(f); }

ALGORITHM Step1 : Start Step2 : Accept n Step3 : fac=fact(n) Step4 : Display fac Step5 : Stop fact(n) Step1 :Start Step2 : Accept i Step3 : Initalize f=1 Step4 : Repeat Step5 from i=1 to n Step5 : f=f*i Step6 : return f Step 7: Stop

OUTPUT Enter a number: 6 The factorial is:720

/*WRITE A PROGRAM TO GENERATE FIBONACCI SERIES USING RECURSION*/ #include<stdio.h> #include<conio.h> int fib(int); void main() { int n,i; clrscr(); printf("Enter the limit:"); scanf("%d",&n); for(i=0;i<n;i++) printf("\n%d",fib(i)); getch(); } int fib(int p) { int fi; if(p==0) return 0;

if(p==1) return 1; if(p>1) fi=fib(p-1)+fib(p-2); return fi; }

OUTPUT Enter the limit:9 0 1 1 2 3 5 8 13 21

ALGORITHM Step1 : Start Step2 : Accept n,i Step3 : call fib(int p) Step4 : Display fib(i) Step5 : Stop fib(int p) Step1 : Start Step2 : if(p=0) return 0 Step3 : if(p=1) return 1 Step4 : if(p>1) fi=fib(p-1)+fib(p-2) return fi Step5 : Stop

/*WRITE A PROGRAM TO SEARCH AN ITEM AND PRINT ITS POPSITION IN AN ARRAY*/ #include<stdio.h> #include<conio.h> void main() { int a[10],i,ITEM,n; clrscr(); printf("Enter the limit:\n"); scanf("%d",&n); printf("Enter numbers:\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Enter the item to be searched:"); scanf("%d",&ITEM); for(i=0;i<n;i++) {

if(ITEM==a[i]) printf("The Item %d is found at location:%d",ITEM,i); } getch(); }

ALGORITHM Step1 : Start Step2 : Accept n,a[i],ITEM Step3 : if(ITEM=a[i]) Display ITEM,i Step4 : Stop

OUTPUT Enter the limit: 5 Enter numbers: 45 68 32 54 71 Enter the item to be searched:32 The Item 32 is found at location:2

/*WRITE A PROGRAM TO FIND THE TRANSPOSE OF A MATRIX*/ #include<stdio.h> #include<conio.h> void main() { int a[10][10],i,j,m,n; clrscr(); printf("Enter number of rows and columns:\n"); scanf("%d%d",&m,&n); printf("Enter elements:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("TRANSPOSE OF THE MATRIX\n---------------------\n"); for(i=0;i<n;i++)

{ for(j=0;j<m;j++) { printf("\t%d\t",a[j][i]); } printf("\n"); } getch(); }

OUTPUT Enter number of rows and columns: 3 3 Enter elements: 452 867 319 TRANSPOSE OF THE MATRIX --------------------4 5 2 8 6 7 3 1 9

ALGORITHM Step1 : Start Step2 : Accept m,n,a[i][j] Step3 : Display a[i][j] Step5 : Stop

You might also like