Name: Regno: Rollno
Name: Regno: Rollno
/*EX NO:1 To write a program for Simple menu driven calculator program using switch statement*/ #include<stdio.h> #include<conio.h> void main() { int a,b,c,n; clrscr(); printf("Menu--\n"); printf("1.ADDITION--\n"); printf("2.SUBTRACTION--\n"); printf("3.MULTIPLICATION-\n"); printf("4.DIVISION --\n"); printf("Exit\n"); printf("Enter your Choice"); scanf("%d",&n); if(n<=4&&n>0) { 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 0: exit(0); break; default: printf("Invalid operation code"); break; } getch(); } OUTPUT Menu-1.ADDITION-2.SUBTRACTION-3.MULTIPLICATION4.DIVISION -Exit Enter your Choice 1 enter two numbers 10 20 Addition: 30
/*EX:NO:2 To write a program to calculate the nCr using functions.*/ #include<stdio.h> #include<conio.h> int factorial(int n); void main() { int n,r,nCr; clrscr(); printf("enter the number n and r"); scanf("%d%d",&n,&r); nCr = factorial(n)/(factorial(n-r) * factorial (r)); printf("the nCr is %d",nCr); getch(); } int factorial(int n) { int f; if((n==0) || (n==1)) { return 1; } f = n * factorial(n-1); // uses recursion return f; } OUTPUT ENTER THE NUMBER N AND R 3 2 THE NCR IS 3
/*EX:NO:3 To write a program to find the largest and smallest number using arrays.*/ #include<stdio.h> #include<conio.h> void main() { int i,n; int a[50],large,small; clrscr(); printf ("enter the n term in array\n"); scanf("%d",&n); printf("enter the elements in the array"); for(i=0;i<n;i++) scanf("%d",&a[i]); large=a[0]; small=a[0]; for(i=1;i<n;i++) { if(a[i]>large) large=a[i]; else if(a[i]<small) small=a[i]; } printf("largest element in array is %d\n",large); printf("smallest element in array is %d\n",small); getch(); } Output enter the n term in array 2 enter the elements in the array 10 20 30 largest element in array is 30.000000 smallest element in array is 10.000000 /*EX:NO:4 TO GENERATE THE FIBONACCI SERIES
//Fibonacci series*/
#include<stdio.h> #include<conio.h> void main() { int a=0,b=1,i,c,n; clrscr(); printf("Enter the Value for N :"); scanf("%d",&n); printf("\n%d\n%d",a,b); for(i=3;i<=n;i++) { c=a+b; printf("\n%d",c); a=b; b=c; } getch(); } output: Enter the Value for N : 5 01123 /*EX:NO:5
TO WRITE A PROGRAM TO FIND THE FACTORIAL OF A NUMBER USING RECURSION.*/ #include<stdio.h> #include<conio.h> int factorial(int ); void main() { int n; printf("enter the number n "); scanf("%d",&n); printf("the factorial of the given number is %d",factorial(n)); getch(); } int factorial(int num) { int f; if((num==0) || (num==1)) { return 1; } f = num * factorial(num-1); /* uses recursion*/ return f; } OUTPUT: enter the number n 5 the factorial of the given number is 120
/* EX NO:6 TO WRITE A PROGRAM TO PRINT THE SUM OF ELEMENTS OF AN ARRAY USING POINTERS.*/ #include<stdio.h> #include<conio.h> Void main() { int i,b, total =0; int a[5]; int *c ; for( i=0;i<5;i++ ) { printf( enter the number %d ,i+1 ); scanf(%d ,&a[i]); } c=a; for( b=0;b<5;b++) { printf( %d,*c); total=total+*c; c=c+1; } printf(total=%d,total); } Output enter the number 1 10 enter the number 2 20 enter the number 3 30 enter the number 4 40 enter the number 5 50 102030405 0 total 150
#include<stdio.h> #include<conio.h> void main() { FILE *f1; char s[10][10]; int i,n,r[10]; clrscr(); printf("Enter no of students\n"); scanf("%d",&n); printf("Enter a content in file1 : \n"); f1=fopen("f1.cpp","w"); for(i=1;i<=n;i++) { printf("Enter name and rollno\n"); scanf("%s%d",s[i],&r[i]); fprintf(f1,"%s\n",s[i]); fprintf(f1,"%d",r[i]); } fclose(f1);
f1=fopen("f1.cpp","r"); printf("\n\nName Rollnumber\n----- -----\n\n"); for(i=1;i<=n;i++) { fscanf(f1,"%s",s[i]); fscanf(f1,"%d",r[i]); printf("%s %d\n",s[i],r[i]); } fclose(f1); getch(); } output :
Enter no of students :2 Enter a content in file1 : Enter name and rollno manju Enter name and rollno usha 555 505
Name Rollnumber
manju usha
555 505
/*MATRIX ADDITION*/ #include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10],m,n,i,j,k,p,q; clrscr(); printf("\t Matrix Addtion\n\n"); printf("\t ~~~~~~~~~~~~~~~~~ \n\n"); printf("Enter the number of row & colunm of A matrix=\n"); scanf("%d %d",&m,&n); printf("Enter the number of row & column of B matrix=\n"); scanf("%d %d",&p,&q); if((m==p)&&(n==q)) { printf("Matrix Addtion is possible \n\n");
} } printf("Enter the element of B matrix:\n"); for (i=0;i<p;i++) { for (j=0;j<q;j++) { 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("The Resultant matrix is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d\t",c[i][j]); }
Output
Matrix Addtion
~~~~~~~~~~~~~~~~~
Enter the number of row & colunm of A matrix= 22 Enter the number of row & column of B matrix= 22 Matrix Addtion is possible
/* Ex:No:8.B
*/
#include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10],m,n,i,j,k,p,q; clrscr(); printf("\t Matrix Multiplication \n\n"); printf("\t ~~~~~~~~~~~~~~~~~ \n\n"); printf("Enter the number of row & colunm of A matrix=\n"); scanf("%d %d",&m,&n); printf("Enter the number of row & column of B matrix=\n"); scanf("%d %d",&p,&q); if((m==p)&&(n==q)) {
printf("Matrix multiplication is possible \n\n"); printf("Enter the element of A matrix:\n"); for (i=0;i<m;i++) { for (j=0;j<n;j++)
{ scanf("%d",&a[i][j]); } } printf("Enter the element of B matrix:\n"); for (i=0;i<p;i++) { for (j=0;j<q;j++) { scanf("%d",&b[i][j]); } } for (i=0;i<m;i++) { for (j=0;j<n;j++) { c[i][j]=0; for(k=0;k<p;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } printf("The Resultant matrix is:\n");
for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d\t",c[i][j]); } printf("\n\n"); } } else printf("Matrix multiplication is not possible \n\n");
getch(); }
OUT PUT
2 2
2 2
2 2
2 2
8 8
8 8
/*EX NO: 9
/*To check whether the given number is Perfect or not*/ #include<stdio.h> #include<conio.h> void main() { int sum=0,i,n; clrscr(); printf("enter the no"); scanf("%d number to be checked",&n); for(i=1;i<n;i++) { if(n%i==0) sum=sum+i; } if(sum==n) printf("the number is perfect",n); else printf("the number is not perfect",n); getch(); }
output
enter the no 6
/*EX NO: 10 STRING MANIPULATION FUNCTIONS WITHOUT USING LIBRARY FUNCTIONS*/ /*10.a STRING CONCATENATION*/ #include<stdio.h> #include<conio.h> void main() { int i ,j; char str[20],str2[20],str3[20]; clrscr(); printf(enter the first and second string); scanf(%s%s,str1,str2); for(i=0;str[i]!=\0;i++) { str3[i]==str1[i]; }
for(j=0;str[j]!=\0;j++) { str3[i]==str2[j]; i++; } str3[i]=\0; printf(The concatenated string is %s\n,str3); getch(); } Output:1 Enter the two string :Manj usha The concatenated string is Manjusha
/*10.b STRING COMPARISON*/ #include<stdio.h> #include<conio.h> void main() { int i ; char str1[20],str2[20]; clrscr(); printf(enter the first and second string); scanf(%s%s,str1,str2); for(i=1;i<2;i++) { if(str1[i]==str2[i]) printf(The both string is equal); else printf(The both string is not equal);
getch(); } }
Output :1 Enter the first and second string :manju manju The both string is equal.
Output :2 Enter the first and second string :manju priya The both string is not equal.
/*10.C STRING LENGTH*/ #include<stdio.h> #include<conio.h> void main() { int len ; char str[50]; clrscr(); printf(Enter the string); scanf(%s,&str); for( len=0;str[len]!=\0;len++) printf(The length of string is%d\n,len); getch(); } Output Enter the string:Manju The length of string is 4
/*10.D .STRING COPY*/ #include<stdio.h> #include<conio.h> void main() { int i ; char str1[20],str2[20]; clrscr(); printf(Enter the first string:); scanf(%s,&str1); printf( The first string is :%s,str1); for(i=0; str1[i]!=\0;i++) { str2[i]==str1[i]; } str2[i]=\0; printf( \n Copy the string 1 to string2 without using strcpy() function); printf(The Copied string is : %s,str2); getch(); }
Copy the string 1 to string2 without using strcpy() function The Copied string is : Manju
*EX NO: 11 */ /*TO WRITE A PROGRAM TO PERFORM ASCII EQUIVALENT KEYSTROKES*/ #include<stdio.h> #include<conio.h> int assi(char c) { int a; a=(int)c; return a; } void main() { char ch; printf("enter the character") ; scanf ("%c",&ch); printf("%d",assi(ch)); getch(); } Output:1 enter the character a 97 Output:2 enter the character b 98
/*EX NO 12 TO WRITE A PROGRAM TO SOLVE A POLYNOMIAL EQUATION*/ #include<stdio.h> #include<conio.h> struct node { int coe,exp; struct node *next; }; struct node *start,*ptrn,*ptrp; typedef struct node *polynomial; polynomial get_poly (); void show (); polynomial add (polynomial p1, polynomial p2); polynomial get_poly () { char c='y'; ptrn=ptrp=start=NULL; while (c=='y' || c=='Y') { ptrn=(struct node*)malloc(sizeof(struct node)); ptrp->next=ptrn; if (start==NULL) start=ptrn; ptrp=ptrn; printf("Enter the coefficient:"); scanf("%d",&ptrn->coe); printf("Enter the exponent:"); scanf("%d",&ptrn->exp); ptrn->next=NULL; printf ("Enter y to add more nodes:"); scanf(" %c",&c); } return start; } void show (polynomial start)
{ struct node *ptr; ptr=start; while (ptr!=NULL) { printf(" %d X^ %d + ",ptr->coe,ptr->exp); ptr=ptr->next; } printf(" "); } polynomial add(polynomial p1,polynomial p2) { struct node *p1ptr,*p2ptr; int coe,exp; ptrn=ptrp=start=NULL; p1ptr=p1; p2ptr=p2; while (p1ptr!=NULL && p2ptr!=NULL) { if (p1ptr->exp==p2ptr->exp) { coe=p1ptr->coe+p2ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; p2ptr=p2ptr->next; } else if (p1ptr->exp>p2ptr->exp) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; } else if (p1ptr->exp<p2ptr->exp) { coe=p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; } ptrn=(struct node*)malloc(sizeof(struct node));
if (start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } if (p1ptr==NULL) { while (p2ptr!=NULL) { coe=p2ptr->coe; exp=p2ptr->exp; p2ptr=p2ptr->next; ptrn=(struct node*)malloc(sizeof(struct node)); if (start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn; ptrp=ptrn; } } else if (p2ptr==NULL) { while (p1ptr!=NULL) { coe=p1ptr->coe; exp=p1ptr->exp; p1ptr=p1ptr->next; ptrn=(struct node*)malloc(sizeof(struct node)); if (start==NULL) start=ptrn; ptrn->coe=coe; ptrn->exp=exp; ptrn->next=NULL; ptrp->next=ptrn;
ptrp=ptrn; } } return start; } void main () { polynomial p1,p2,sum,diff; clrscr (); printf("First Polynomial.\n"); p1=get_poly (); printf("\nSecond Polynomial.\n"); p2=get_poly (); clrscr (); printf("\n The First Polynomial is:"); show (p1); printf("\n The Second Polynomial is:"); show (p2); printf("\n The Sum of two polynomials is:"); sum=add(p1,p2); show (sum); getch (); }
Output
First Polynomial. Enter the coefficient:3 Enter the exponent:2 Enter y to add more nodes:1
The First Polynomial is: 3 X^ 2 + The Second Polynomial is: 3 X^ 4 + The Sum of two polynomials is: 3 X^ 4 + 3 X^ 2 +