You are on page 1of 69

EXPERIMENT 1: PROGRAM TO PRINT THE SIZES AND RANGES OF DIFFERENT DATA TYPES #include<stdio.h> #include<conio.h> #include<limits.h> #include<float.

h> void main() { clrscr(); printf(" DATA TYPE SIZE IN BYTES MINIMUM VALUE MAXIMUM VALUE"); printf("\n INTEGER(SIGNED) %d %i %d ", sizeof(int),INT_MIN,INT_MAX); printf("\nSHORT INTEGER %d %hi %hd", sizeof(short),SHRT_MIN,SHRT_MAX); printf("\nLONG INTEGER %d %ld %ld", sizeof(long),LONG_MIN,LONG_MAX); printf("\nUNSIGNED INTEGER %d %u %u", sizeof(unsigned int),0,UINT_MAX); printf("\nUNSIGNED SHORT INTEGER %d %u %u", sizeof(unsigned short),0,USHRT_MAX); printf("\nUNSIGNED LONG INTEGER %d %d %lu", sizeof(unsigned long),0,ULONG_MAX); printf("\nFLOAT %d 3.4E%d 3.4E+%d", sizeof(float),FLT_MIN_10_EXP,FLT_MAX_10_EXP); printf("\nDOUBLE %d 1.7E%d 1.7E+%d", sizeof(double),DBL_MIN_10_EXP,DBL_MAX_10_EXP); printf("\nLONG DOUBLE %d 3.4E%d 1.1E+%d", sizeof(long double),LDBL_MIN_10_EXP,LDBL_MAX_10_EXP); printf("\nCHARACTER(SIGNED) %d %d %d", sizeof(char),SCHAR_MIN,SCHAR_MAX); printf("\nUNSIGNED CHAR %d %d %d",

1

sizeof(unsigned char),0,UCHAR_MAX); getch(); } EXPERIMENT 2: DEMONSTRATING INTEGER CONSTANTS #include<stdio.h> #include<conio.h> void main() { clrscr(); printf("\n integer constants are three types"); printf("\n[1] decimal integer consist of a set of digits,0 through 9,\npreceded by an optional - or + sign"); printf("\n valid examples of decimal integer constants are:"); printf("\nint %d \n int %d \n long int %ld \n unsigned long int %lu \n long int %ld",+123,-378,-32271L,76542LU,12789845L); printf("\n[2] an octal integer constant consists of any combination of 0 through 7, with a leading 0(means starting with 0)"); printf("\n valid examples of octal integers:"); printf("\n octal integers %o \t %o \t %o \t %o",037,0,0435,0551); printf("\n[3] A sequence of digits preceded by 0x and 0X is considered as hexadecimal integer constants\nthey may also include alphabets A through F or a through f\n the letters A or a through F or f represents the numbers 10 to 15"); printf("\n following are the examples of valid hex integers"); printf("\n %X \t %x \t %X \t %x",0X2,0x9F,0Xbcd,0); getch(); } EXPERIMENT 3: DEMONSTRATING FLOATING POINT CONSTANTS #include<conio.h> #include<stdio.h> void main() {

2

clrscr(); printf("\nReal constants are often known as floating point constants"); printf("\ninteger constants are unfit for many quantities for example length,breadth,radius,price etc"); printf("\n real numbers are in decimal notation, having a whole number followed by a decimal point and a fractional part"); printf("\n for example %f \t %f \t %f \t %f are valid real numbers", 21.5,.95,-.71,+.5); printf("\n a real number may also be expressed in exponential notation (or scientific notation)"); printf("\n for example the value 215.65 may also be written as 2.1565e+2 where e2=100"); printf("\nexamples of legal floating point constants:"); printf("\n %f \t %f \t %f \t %f \t %f ",0.65e4,12e-2,1.5e+5,3.18E+3,-1.2E-1); getch(); } EXPERIMENT 4: DEMONSTRATING CHARACTER AND STRING CONSTANTS #include<stdio.h> #include<conio.h> void main() { int i,l; char string1[10]="MRITS"; clrscr(); printf("\n a single character constant contains a single character enclosed between a pair of single quote marks"); printf("\ncharacter constants have integer values known as ASCII values"); printf(" the ASCII (american standard code for information interchange) values of some character constants are :"); for(i=35;i<=122;i++) printf(" %c=%d\t ",i,i); printf("\n a string constant is a sequence of character enclosed in double quotes");

3

printf("\n the character may be letters,numbers,special characters and blank space"); l=printf("\n%s",string1);/* now l contains length of the string*/ puts(string1); for(i=0;i<l;i++)/* the is the another way of printing string*/ printf("%c",string1[i]); printf("\n backslash characters constants are:"); printf("\n audible alert(bell) \a"); printf("\n back space mri\bts"); printf("\n new line \n"); printf("\n carriage return \r"); printf("\n horizontal tab \t mrits"); printf("\n vertical tab \v mrits"); printf("\n single quote '\'' "); printf("\n double quote '\"' "); printf("\n quetion mark '\?' "); printf("\n backslash '\\' "); printf("\n null character '\0' "); getch(); } EXPERIMENT 5: DEMONSTRATING C OPERATORS #include<stdio.h> #include<conio.h> void main() { int choice; clrscr(); printf("\n[1] Airthmetic operators"); printf("\n[2] increment and decrement operators"); printf("\n[3] relational operators"); printf("\n[4] Logical operators");

4

printf("\n[5] Bitwise operators"); printf("\nEnter ur choice"); scanf("%d",&choice); switch(choice) { case 1: { int a=5,b=3; float x=5,y=3; clrscr(); printf("\n integer addition a+b=%d \n float addition x+y=%f ",a+b,x+y); printf("\n integer subtraction a-b=%d \n float subtraction x-y=%f",a-b,x-y); printf("\n integer multiplication a*b=%d \n float multiplication x*y=%f",a*b,x*y); printf("\n integer quetiont division a/b=%d \n integer remainder division amodb=%d",a/b,a%b); printf("\n there is no exact division in integer division \n and there is no remainder or quetiont division in float division"); printf("\n exact division or float division x/y=%f",a/b); } break; case 2: { int a=5; clrscr(); printf("\n now the a value is %d",a); printf("\n prefix increment ++a=%d",++a); printf("\n now the a value is %d",a); printf("\n prefix decrement --a=%d",--a); printf("\n now the a value is %d",a); printf("\n postfix increment a++=%d",a++); printf("\n now the a value is %d",a); printf("\n postfix decremnt a--=%d",a--);

5

printf("\n now the a value is %d",a); } break; case 3:{ clrscr(); printf("\n Not equal to operator 10!=10 =%d", 10!=10); printf("\n Equal to operator 10==10 =%d",10==10); printf("\n greater than or equal to operator 10>=10 =%d", 10>=10); printf("\n lesser than or equal to operator 10<=10 =%d",10<=10); printf("\n greater than operator 10>10 =%d and 11>10 =%d",10>10,11>10); printf("\n lesser than operator 10<11 =%d and 10<10 =%d",10<11,10<10); } break; case 4:{ clrscr(); printf(" \n logical AND: logical OR"); printf(" \n 5<3 && 5>10 =%d 5<3 || 5>10 =%d ",5<3&&5>10,5<3||5>10); printf("\n 5<3 && 5<10 =%d 5<3 || 5<10 =%d",5<3&&5<10,5<3||5<10); printf("\n 5>3 && 5>10 =%d 5>3 || 5>10 =%d",5>3&&5>10,5>3||5>10); printf("\n 5>3 && 5<10 =%d 5>3 || 5<10 =%d",5>3&&5<10,5>3||5<10); printf("\n logical NOT:"); printf("\n !(8==8) =%d \t !(8==9) =%d",!(8==8),!(8==9)); } break; case 5:{ int x=8,y=2; clrscr(); printf("\n x=%d \t y=%d",x,y); printf("\nBITWISE operators:"); printf("\n Right shift x>>2 =%d",x>>2); printf("\n Left shift y<<3 =%d",y<<3);

6

printf("\n Bitwise AND x&y =%d",x&y); printf("\n Bitwise OR x|y =%d",x|y); printf("\n Bitwise Exclusive OR x^y =%d",x^y); printf("\n ones complemnt ~x =%d \t~(~x) =%d",~x,~(~x)); } break; default: printf("\n please choose ur choice between 1 to 5"); } getch(); } EXPERIMENT 6: INTERCHANGING VALUES OF TWO VARIABLES #include<stdio.h> #include<conio.h> void main() { int a,b,temp; int option; clrscr(); printf("\n enter the values of a AND b"); scanf("%d%d",&a,&b); printf("\n a=%d \t b=%d",a,b); printf("\n[1] swaping with using temporary variable"); printf("\n[2] swaping without using temporary variable type1"); printf("\n[3] swaping without using temporary variable type2"); printf("\n enter ur otion"); scanf("%d",&option); switch(option) { case 1 : temp=a; a=b;

7

b=temp; break; case 2 : a=a+b; b=a-b; a=a-b; break; case 3 : a=a*b; b=a/b; a=a/b; break; default : clrscr(); printf("\n enter correct option"); break; } printf("\n a=%d \t b=%d",a,b); getch(); } EXPERIMENT 7: DEMONSTRATING TYPECASTING IN C #include<stdio.h> #include<conio.h> void main() { clrscr(); printf("\n when the types of the two operands in a binary expression\n are different c automatically converts one type to another called automatic promotion or IMPLICIT TYPE CONVERSION"); printf("\n the following block demonstrate implicit type conversion"); printf("\n lower type automaticaly promotes to higer type"); { /* local declarations */ char c= 'A'; float d=245.3;

8

int i=3650; short s=78; printf("\n char=%c \t float=%g \t int=%d \t short=%hd ",c,d,i,s); printf("\n float + char = float %f",d+c);/* char promoted to float */ printf("\n int * short =int %d",i*s);/* short promoted to int */ printf("\n float * char = float %f",d*c);/* char promoted to float */ printf("\n c=%d and c=%c ",c,c); } printf("\n we can convert the data from one type to another instead of complier using EXPLICIT TYPE CONVERSION"); printf("\nthe following block demonstrate explicit type conversion"); { char c='\0'; int a=100,b=45; double x=100.0,y=45.0; double z; printf("\n char=%c \t a=%d \t b=%d \t x=%f \t y=%g",c,a,b,x,y); z=(double)(a/b); printf("\n (double)(a/b)=%6.2f",z); z=(double)a/b; printf("\n (double)(a/b)=%6.2f",z); c=(char)(x/y); printf("\n (char)(x/y)=%3d",c); } getch(); } EXPERIMENT 8: MAXIMUM OF THREE NUMBERS USING IF,ELSE IF LADDER,NESTED IF AND CONDITIONAL OPERATOR #include<stdio.h> #include<conio.h> void main() { int a,b,c,choice;

9

clrscr(); printf("\n enter the values for a,b and c"); scanf("%d%d%d",&a,&b,&c); printf("\n a=%d \t b=%d \t c=%d",a,b,c); printf("\n MAXIMUM OF THREE NUMBERS USING"); printf("\n[1] simplle if"); printf("\n[2] if else if ladder"); printf("\n[3] nested if"); printf("\n[4] conditional operator"); printf("\n enter your choice"); scanf("%d",&choice); switch(choice) { case 1: if(a>b&&a>c) printf("\n a=%d is big",a); if(b>a&&b>c) printf("\n b=%d is big",b); if(c>a&&c>b) printf("\n c=%d is big",c); break; case 2: if(a>b&&b>c) printf("\n a=%d is big",a); else if(b>c) printf("\n b=%d is big",b); else printf("\n c=%d is big",c); break; case 3: if(a>b) { if(a>c) printf("\n a=%d is big",a);

10

else printf("\n c=%d is big",c); } else { if(b>c) printf("\n b=%d is big",b); else printf("\n c=%d is big",c); } break; case 4: (a>b&&b>c)?printf("\n a=%d is big",a):(b>c)?printf("\n b=%d is big",b):printf("\n c=%d is big",c); break; default : printf("\n enter correct choice"); break; } getch(); } EXPERIMENT9: Write a C program, which takes two integer operands and one operator form the user,performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement) #include<stdio.h> #include<conio.h> void main() { int a,b,res,ch;

11

clrscr(); printf("\t *********************"); printf("\n\tMENU\n"); printf("\t********************"); printf("\n\t(1)ADDITION"); printf("\n\t(2)SUBTRACTION"); printf("\n\t(3)MULTIPLICATION"); printf("\n\t(4)DIVISION"); printf("\n\t(5)REMAINDER"); printf("\n\t(0)EXIT"); printf("\n\t********************"); printf("\n\n\tEnter your choice:"); scanf("%d",&ch); if(ch<=5 & ch>0) { printf("Enter two numbers:\n"); scanf("%d%d",&a,&b); } switch(ch) { case 1: res=a+b; printf("\n Addition:%d",res); break; case 2: res=a-b; printf("\n Subtraction:%d",res); break;

12

case 3: res=a*b; printf("\n Multiplication:%d",res); break; case 4: res=a/b; printf("\n Division:%d",res); break; case 5: res=a%b; printf("\n Remainder:%d",res); break; case 0: printf("\n Choice Terminated"); exit(); break; default: printf("\n Invalid Choice"); } getch(); } EXPERIMENT 10: PROGRAM WILL EXAMINE THE VALUE OF A FLOATING POINT VARIABLE CALLED TEMP AND PRINT ICE IF TEMP<0,WATER IF 0<=TEMP<=100 AND STEAM IF TEMP>100 #include<conio.h>

13

#include<stdio.h> void main() { int n; float temp; printf("\nenter the value for temp"); scanf("%f",&temp); if(temp<0) n=1; else if(temp<=100) n=2; else n=3; switch(n) { case 1: printf("\n ICE"); break; case 2: printf("\n WATER"); break; case 3: printf("\n STEAM"); break; } getch(); } EXPERIMENT 12A: Write a C program to generate Pascal's triangle. #include<stdio.h> #include<conio.h> void main() {

14

int bin,p,q,r,x; clrscr(); bin=1; q=0; printf("Rows you want to input:"); scanf("%d",&r); printf("\nPascal's Triangle:\n"); while(q<r) { for(p=40-3*q;p>0;--p) printf(" "); for(x=0;x<=q;++x) { if((x==0)||(q==0)) bin=1; else bin=(bin*(q-x+1))/x; printf("%6d",bin); } printf("\n"); ++q; } getch(); } EXPERIMENT 12B:Write a C program to construct a pyramid of numbers. #include<stdio.h>

15

#include<conio.h> void main() { int num,i,y,x=35; clrscr(); printf("\nEnter the number to generate the pyramid:\n"); scanf("%d",&num); for(y=0;y<=num;y++) { /*(x-coordinate,y-coordinate)*/ gotoxy(x,y+1); /*for displaying digits towards the left and right of zero*/ for(i=0-y;i<=y;i++) printf("%3d",abs(i)); x=x-3; } getch(); } EXPERIMENT 12C : #include<stdio.h> #include<conio.h> void main() { int n,i,j,k; clrscr(); printf("\nEnter an integer number"); scanf("%d",&n);

16

for(i=1;i<=n;i++) { for(k=0;k<=20-i;k++) printf(" "); for(j=i;j>=1;j--) printf("%4d",i); printf("\n"); } getch(); } EXPERIMENT 12D: program to print multiplication table #include<stdio.h> #include<conio.h> void main() { int i, m, n; clrscr(); printf("\nenter no for which you want to print Multiplication table and also enter upto how many Values do you want to print it"); scanf("%d%d",&n,&m); for(i=1;i<=m;i++) { printf("%d*%d=%d",n,i,n*i); printf("\n"); } getch(); } EXPERIMENT 12E: #include<stdio.h> #include<conio.h> void main() {

17

int n,i,j; clrscr(); printf("\nEnter an integer number"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) printf("%2d",i); printf("\n"); } getch(); } EXPERIMENT 12F: #include<stdio.h> #include<conio.h> void main() { int n,i,j; clrscr(); printf("\nEnter an integer number"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=i;j>=1;j--) printf("%2d",j); printf("\n"); } getch(); }

18

EXPERIMENT 12 G: #include<stdio.h> #include<conio.h> void main() { int n,i,j; clrscr(); printf("\nEnter an integer number"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) printf("%2d",j); printf("\n"); } getch(); } EXPERIMENT 12H: #include<stdio.h> #include<conio.h> void main() { int n,i,k,j; clrscr(); printf("\nEnter an integer number"); scanf("%d",&n); for(i=1;i<=n;i++) { for(k=1;k<=20-i;k++) printf(" ");

19

for(j=1;j<=i;j++) printf("%2d",i); printf("\n"); } getch(); } EXPERIMENT 13A:GENERATING PRIME NUMBERS (USE BREAK STATEMENT) #include<stdio.h> #include<conio.h> void main() { int i,j,n,r; clrscr(); printf("\n enter any number"); scanf("%d",&n); for(i=2;i<=n;++i) { r=1; for(j=2;j<=i/2;++j) { r=i%j; if(r==0) break; } if(r!=0) printf("%d\t",i); } getch(); } EXPERIMENT 13B:PROGRAM TO PRINT A-Z AND a-z USING ASCII VALUES(USE CONTINUE STATEMENT) #include<stdio.h>

20

#include<conio.h> void main() { int i; clrscr(); printf("\n the ASCII values for A-Z and a-b are:"); for(i=65;i<=122;i++) { if(i>90&&i<97) continue; else printf("\t%c = %d",i,i); } getch(); } 14A:PROGRAM TO CONVERT DECIMAL TO BINARY #include<conio.h> #include<stdio.h> void main() { int num,rem,a=1,k; long int binaryno=0; clrscr(); printf("\nEnter the number"); scanf("%d",&num); k=num; while(k>0) { rem=k%2; binaryno=binaryno+rem*a; k=k/2;

21

a=a*10; } printf("\n Decimal number=%d-Binary number=%ld",num,binaryno); getch(); } EXPERIMENT 14B:PROGRAM TO CONVERT BINARY TO DECIMAL #include<conio.h> #include<math.h> #include<stdio.h> void main() { long binaryno,binno; int rem,decimalno=0,i=0; clrscr(); printf("\nEnter a binary no"); scanf("%ld",&binaryno); binno=binaryno; while(binaryno!=0) { rem=binaryno%10; decimalno=decimalno+rem*pow(2,i); binaryno=binaryno/10; i++; } printf("\n Equivalent decimal number for binary no %ld is %d",binno,decimalno); getch(); } EXPERIMENT 15A:PROGRAM TO ACCEPT INTEGER VALUES AND TO FIND TOTAL EVEN AND VALUES ENTERED,EVEN SUM AND ODD SUM.THE PROGRAM MUST EXIT IF NEGITIVE VALUE IS ENTERED. ( USE DO WHILE LOOP) #include<stdio.h>

22

#include<conio.h> void main() { int ecount=0,ocount=0,esum=0,osum=0,n=0; clrscr(); printf("\n enter the numbers"); scanf("%d",&n); do { if(n%2==0) { ecount++; printf("\n%d is even no",n); esum=esum+n; } else { ocount++; printf("\n %d is odd no",n); osum=osum+n; } printf("\n Enter a integer"); scanf("%d",&n); }while(n>0); printf("\n total no of even numbers is %d",ecount); printf("\n total no of odd numbers is %d",ocount); printf("\n sum of even numbers is %d",esum); printf("\n sum of odd numbers is %d",osum); getch(); }

23

EXPERIMENT 15B:PROGRAM TO ACCEPT INTEGER VALUES AND TO FIND TOTAL EVEN AND VALUES ENTERED,EVEN SUM AND ODD SUM. (USE FOR LOOP) #include<stdio.h> #include<conio.h> void main() { int ecount=0,ocount=0,esum=0,osum=0,n=0,i; clrscr(); for(i=1;i<=20;i++) { printf("\n enter an integer\n"); scanf("%d",&n); if(n%2==0) { ecount++; printf("\n%d is even no \n",n); esum=esum+n; } else { ocount++; printf("\n %d is odd no \n",n); osum=osum+n; } } printf("\n total no of even numbers is %d",ecount); printf("\n total no of odd numbers is %d",ocount); printf("\n sum of even numbers is %d",esum); printf("\n sum of odd numbers is %d",osum); getch();

24

} EXPERIMENT 16:PROGRAM TO FIND THE SERIES I) SUM OF N NATURAL NUMBERS II) SUM OF SQUARES OF N NATURAL NUMBERS III) SUM OF CUBES OF N NATURAL NUMBERS IV) 1+1/2+1/3-------1/N HARMONIC SERIES V) #include<stdio.h> #include<conio.h> #include<math.h> void main() { int n,sum=0,sqsum=0,cubsum=0; float hsum=0,hsqsum=0,i; clrscr(); printf("\n enter the value of n"); scanf("%d",&n); for(i=1;i<=n;i++) { sum=sum+i; sqsum=sqsum+i*i; cubsum=cubsum+pow(i,3); hsum=hsum+(1/i); hsqsum=hsqsum+1/pow(i,2); } printf("\n sum of %d natural numbers=%d",n,sum); printf("\n sum of squares of n natural numbers =%d",sqsum); printf("\n sum of cubes of n natural numbers=%d",cubsum); printf("\n harmonic series sum=%f",hsum);

25

printf("\n squares of harmonic serie sum=%f",hsqsum); getch(); } EXPERIMENT 17A AND 17B:PROGRAM TO FIND I) 1+X2/2!+X4/4!+X6/6!+------II) X+X2/2!+X4/4!+X6/6!+------#include<stdio.h> #include<math.h> #include<conio.h> void main() { int i,j,n,x; float sum,ssum,fact; clrscr(); printf("\n Enter the values of x and n"); scanf("%d%d",&x,&n); sum=1; ssum=x; for(i=2;i<=n;i=i+2) { fact=1; for(j=1;j<=i;j++) fact=fact*j; sum=sum+pow(x,i)/fact; ssum=ssum+pow(x,i)/fact; } printf("\n 1+x2/2!+x4/4!+_______n =%f",sum); printf("\n x+x2/2!+x4/4!+_______n =%f",ssum); getch(); } EXPERIMENT 17C:PROGRAM TO FIND THE SERIES

26

X-X3/3!+X5/5!+-----------#include<stdio.h> #include<math.h> #include<conio.h> void main() { int n,i,x,c=3,fact,j; float sum; clrscr(); printf("\n Enter the values of x and n"); scanf("%d%d",&x,&n); sum=x; for(i=3;i<=n;i=i+2) { fact=1; for(j=1;j<=i;j++) fact=fact*j; if(c%2!=0) sum=sum-pow(x,i)/fact; else sum=sum+pow(x,i)/fact; c++; } printf("\n sum of series numbers %f",sum); getch(); } EXPERIMENT 17D:Write a C program to read in two numbers, x and n, progression: 1+x+x2+x3+-----+xn For example: if n is 3 and x is 5, then the program computes 1+5+25+125.

and then compute the sum of this geometric

27

#include<stdio.h> #include<conio.h> #include<math.h> void main() { int s_sum,i,x,n; clrscr(); printf("Enter the values for x and n:"); scanf("%d %d",&x,&n); if(n<=0 || x<=0) { printf("Value is not valid\n"); } else { printf("Value is valid\n"); s_sum=1; for(i=1;i<=n;i++) { s_sum=s_sum+pow(x,i); } printf("Sum of series=%d\n",s_sum); } getch(); } EXPERIMENT 17E: PROGRAM TO FIND THE SERIES I) 1!+2!+3!+-------N!

28

II) 1/1!+1/2!+1/3!+-----1/N! #include<stdio.h> #include<conio.h> void main() { int i,j,fact,n,sum=0; float hsum=0; clrscr(); printf("\n Enter the value of n"); scanf("%d",&n); for(i=1;i<=n;i++) { fact=1; for(j=1;j<=i;j++) fact=fact*j; sum=sum+fact; hsum=hsum+(float)1/fact; } printf("\n 1!+2!+3!+----------n! =%d",sum); printf("\n 1/1!+1/2!+1/3!+-------1/n! =%f",hsum); getch(); } EXPERIMENT 17F: Write a C program to calculate the following Sum: Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! #include <stdio.h> #include <math.h> void main() {

29

int counter,f_coun; float sum=0,x,power,fact; clrscr(); printf("<-----------------------PROGRAM FOR SUM OF EQ. SERIES----------------------->"); printf("\n\n\tEQUATION SERIES : 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - X^10/10!"); printf("\n\n\n\tENTER VALUE OF X : "); scanf("%f",&x); for(counter=0, power=0; power<=10; counter++,power=power+2) { fact=1; //CALC FACTORIAL OF POWER VALUE for(f_coun=power; f_coun>=1; f_coun--) fact *= f_coun; //EQ. FOR SUM SERIES sum=sum+(pow(-1,counter)*(pow(x,power)/fact)); } printf("SUM : %f",sum); getch(); } EXPERIMENT 18: PROGRAM TO FIND I)SUM OF INDIVIDUAL DIGITS OF A NUMBER II)REVERSE OF A NUMBER III)SUM OF CUBES OF INVIDUAL DIGITS OF A NUMBERS IV)CHECKING THE NUMBER FOR PALINDROME V)CHECKING THE NUMBER FOR ARMSTRONG #include<stdio.h>

30

#include<conio.h> void main() { int num, rem, sum=0,reverse=0,t,arm=0; clrscr(); printf("Enter the number s:"); scanf("%d",&num); t=num; while(num!=0) { rem=num%10; reverse=reverse*10+rem; sum=sum+rem; num=num/10; arm=arm+rem*rem*rem; } printf("\nSum of the digits:%d",sum); printf("\nSum of the cubes of individual digits=%d",arm); printf("\nreverse of %d is %d",t,reverse); if(t==reverse) printf("\n%d is palindrome",t); else printf("\n%d is not palindrome",t); if(t==arm) printf("\n%d is armstrong",t); else printf("\n%d is not armstrong",t); getch(); }

31

EXPERIMENT 19A:STORAGE CLASSES DEMONSTRATING THE WORKING OF AUTO/LOCAL AND EXTERN/GLOBAL VARIABLES #include<stdio.h> #include<conio.h> int a=10; /* external or global variable*/ void main() { void call1(void); void call2(void); clrscr(); call1(); call2(); printf("\n in main a=%d",a); getch(); } void call1() { int a=20; /* auto are local variable */ printf("\n in call1() a=%d",a); } void call2() { printf("\n in call2() a=%d",a); } EXPERIMENT 19B:STORAGE CLASSES DEMONSTRATING REGISTER AND STATIC VARIABLES #include<stdio.h> #include<conio.h> void main() { register int i; /* register variables are mostly used for loop control variables

32

for fast access*/ void display(void); clrscr(); for(i=0;i<5;i++) display(); printf("\n static variables values perists at each call"); getch(); } void display() { static int a; /* the default value of static int is zero */ printf("\n a is now %d",a++); } EXPERIMENT 20A:DEMONSTRATING PASS BY VALUE #include<stdio.h> #include<conio.h> void main() { int swap(int,int); int a=10,b=20; clrscr(); printf("\n before swaping a=%d AND b=%d",a,b); swap(a,b); printf("\n After swaping a=%d AND b=%d",a,b); getch(); } int swap(int a,int b) { int temp; temp=a;

33

a=b; b=temp; printf("\n here in function swap a=%d AND b=%d",a,b); } EXPERIMENT 20B:DEMONSTRATING PASS BY REFERENCE #include<stdio.h> #include<conio.h> void main() { int swap(int*,int*); int a=10,b=20; clrscr(); printf("\n before swaping a=%d AND b=%d",a,b); swap(&a,&b); printf("\n After swaping a=%d AND b=%d",a,b); getch(); } int swap(int *a,int *b) { int temp; temp=*a; *a=*b; *b=temp; printf("\n here in function swap a=%d AND b=%d",*a,*b); } EXPERIMENT 21 I: PROGRAM TO FIND FACTORIAL OF A NUMBER USING ITERATIVE LOOPS #include<stdio.h> #include<conio.h> void main() {

34

int i,n; long fact; clrscr(); printf("\n Enter the number to find factorial"); scanf("%d",&n); fact=1; for(i=1;i<=n;i++) fact=fact*i; printf("\n factorial of %d is %ld",n,fact); getch(); } EXPERIMENT 21 II: PROGRAM TO FIND FACTORIAL OF A NUMBER USING NON RECURSIVE FUNCTION #include<stdio.h> #include<conio.h> void main() { int i,n; long fact(int); long l; clrscr(); printf("\n Enter the number to find factorial"); scanf("%d",&n); l=fact(n); printf("\n factorial of %d is %ld",n,l); getch(); } long fact(int n) { int i,f=1; for(i=1;i<=n;i++)

35

f=f*i; return f; } EXPERIMENT 21 III: PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION #include<stdio.h> #include<conio.h> void main() { int i,n; long fact(int); long l; clrscr(); printf("\n Enter the number to find factorial"); scanf("%d",&n); l=fact(n); printf("\n factorial of %d is %ld",n,l); getch(); } long fact(int n) { if(n==0) return 1; else return n*fact(n-1); } EXPERMENT 22 I: PROGRAM TO PRINT FIBNOCCISERIES USING ITERATIVE LOOPS #include<stdio.h> #include<conio.h> void main()

36

{ int a=1,b=1,c,n,i; clrscr(); printf("\nEnter the value of n"); scanf("%d",&n); printf("%d\n",a); printf("%d\n",b); c=a+b; i=0; while(i<=n) { printf("%d\n",c); a=b; b=c; c=a+b; i++; } getch(); } EXPERIMENT 22 II: PROGRAM TO PRINT FIBNOCCISERIES USING NON RECURSIVE FUNCTION #include<stdio.h> #include<conio.h> void main() { void fib(int,int,int); int a,b,n; clrscr(); printf("\n Enter the values of and b"); scanf("%d%d",&a,&b); printf("\nEnter the value of n");

37

scanf("%d",&n); printf("\n%d",a); printf("\n%d",b); fib(a,b,n); getch(); } void fib(int a,int b,int n) { int c,i; c=a+b; i=3; while(i<=n) { printf("%d\n",c); a=b; b=c; c=a+b; i++; } } EXPERIMENT 22 III:PROGRAM TO PRINT FIBNOCCI SERIES USING RECURSION #include<stdio.h> #include<conio.h> void main() { int fib(int); int i,n; clrscr(); printf("\n Enter the number of terms");

38

scanf("%d",&n); printf("\nthe fibnocci series is"); for(i=1;i<=n;++i) printf("\n %d",fib(i)); getch(); } int fib(int m) { if(m<0) exit(1); else if(m<=2) return 1; else return (fib(m-1)+fib(m-2)); } EXPERIMENT 23A I:PROGRAM TO FIND GCD OF TWO NUMBERS USING NON RECURSIVE FUNCTION #include<stdio.h> #include<conio.h> void main() { int gcd(int,int); int x,y; clrscr(); printf("\n Enter two numbers"); scanf("%d%d",&x,&y); if(x<y) printf("\nGCD of %d and %d is %d",x,y,gcd(y,x)); else

39

printf("\n Gcd of %d and %d is %d",x,y,gcd(x,y)); getch(); } int gcd(int a,int b) { int rem; rem=a%b; while(rem!=0) { a=b; b=rem; rem=a%b; } return b; } EXPERIMENT 23A II:PROGRAM TO FIND GCD OF TWO NUMBERS USING RECURSION #include<stdio.h> #include<conio.h> void main() { int gcd(int,int); int x,y; clrscr(); printf("\n Enter two numbers"); scanf("%d%d",&x,&y); if(x<y) printf("\nGCD of %d and %d is %d",x,y,gcd(y,x)); else printf("\n Gcd of %d and %d is %d",x,y,gcd(x,y)); getch();

40

} int gcd(int a,int b) { int rem; rem=a%b; if(rem==0) return b; else return gcd(b,rem); } EXPERIMENT 23B I: PROGRAM TO FIND BASE POWER EXPONENT (X POWER Y) USING NON RECURSIVE FUNCTION #include<stdio.h> int power(int base,int exp) { int p = 1; while(exp>0) { p = p*base; exp--; } return(p); } void main() { int b,e,l; clrscr(); printf("\nEnter base & exponent"); scanf("%d%d",&b,&e); l=power(b,e); printf("%d^%d = %d",b,e,l);

41

getch(); } EXPERIMENT 23B II:PROGRAM TO FIND X POWER Y USING RECURSION #include<stdio.h> int power(int base,int exp) { if(exp==0) return 1; else return base*power(base,exp-1); } void main() { int b,e,l; clrscr(); printf("\nEnter base & exponent"); scanf("%d%d",&b,&e); l=power(b,e); printf("%d^%d = %d",b,e,l); getch(); } EXPERIMENT 24: Write a C program to find both the largest and smallest number in a list of integers void main( ) { float largest(float a[ ], int n); float smallest(float a[],int n); float value[40]; int i,n; printf("\n enter how many elements u want");

42

scanf("%d",&n); printf("\n enter the elements"); for(i=0;i<n;i++) scanf("%f",&value[i]); printf("largest element=%f\n", largest(value,n)); printf("smallest element=%f\n",smallest(value,n)); } float largest(float a[], int n) { int i; float max; max = a[0]; for(i = 1; i < n; i++) if(max < a[i]) max = a[i]; return(max); } float smallest(float a[], int n) { int i; float min; min = a[0]; for(i = 1; i < n; i++) if(min > a[i]) min = a[i]; return(min); } EXPERIMENT 25:PROGRAM TO SORT LIST OF INTEGERS USING BUBBLE SORT #include <stdlib.h>

43

#include <stdio.h> #define MAXSIZE 500 void bubblesort(int arr[], int maxsize); int arr[100],n; void main() { int i; printf("\nHow many arr you want to sort: "); scanf("%d",&n); printf("\nEnter the values one by one: "); for (i = 0; i < n; i++) { printf ("\nEnter element %i :",i); scanf("%d",&arr[i]); } printf("\nArray before sorting:\n"); for (i = 0; i < n; i++) printf("[%i], ",arr[i]); printf ("\n"); bubblesort(arr, n); printf("\nArray after sorting:\n"); for (i = 0; i < n; i++) printf("[%i], ", arr[i]); getch(); } void bubblesort(int arr[], int n) { int i, j, temp; for (i = 0; i < n - 1; i++) { for (j = i; j < n; j++) /* for(j=i+1;j<n;j++) */

44

{ if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } EXPERIMENT 26:PROGRAM TO SORT LIST OF INTEGERS USING SELECTION SORT #include <stdlib.h> #include <stdio.h> #define MAXSIZE 500 void selection(int elements[], int maxsize); void main() { int i,n,elements[100]; printf("\nHow many elements you want to sort: "); scanf("%d",&n); printf("\nEnter the values one by one: "); for (i = 0; i < n; i++) { printf("\nEnter element %i :",i); scanf("%d",&elements[i]); } printf("\nArray before sorting:\n"); for (i = 0; i < n; i++) printf("[%i], ",elements[i]); printf ("\n");

45

selection(elements, n); printf("\nArray after sorting:\n"); for (i = 0; i < n; i++) printf("[%i], ", elements[i]); getch(); } void selection(int elements[], int n) { int i, j, k; int min, temp; for (i = 0; i < n-1; i++) { min = i; for (j = i+1; j < n; j++) { if (elements[j] < elements[min]) min = j; } temp = elements[i]; elements[i] = elements[min]; elements[min] = temp; } } EXPERIMENT 27: program To sort a given list of integers in ascending order USING Insertion sort #include<stdio.h> #include<conio.h> void inst_sort(int []);

46

void main() { int num[5],count; clrscr(); printf("\nEnter the Five Elements to sort:\n"); for (count=0;count<5;count++) scanf("%d",&num[count]); inst_sort(num); printf("\n\nElements after sorting: \n"); for(count=0;count<5;count++) printf("%d\n",num[count]); getch(); } // Function for Insertion Sorting void inst_sort(int num[]) { int i,j,k; for(j=1;j<5;j++) { k=num[j]; for(i=j-1;i>=0 && k<num[i];i--) num[i+1]=num[i]; num[i+1]=k; } } EXPERIMENT 28:PROGRAM TO SORT LIST OF INTEGERS USING QUICK SORT #include <stdlib.h>

47

#include <stdio.h> #define MAXSIZE 500 void quickSort(int elements[], int maxsize); void sort(int elements[], int left, int right); int elements[MAXSIZE]; void main() { int i, maxsize; printf("\nHow many elements you want to sort: "); scanf("%d",&maxsize); printf("\nEnter the values one by one: "); for (i = 0; i < maxsize; i++) { printf ("\nEnter element %i :",i); scanf("%d",&elements[i]); getch(); } printf("\nArray before sorting:\n"); for (i = 0; i < maxsize; i++) printf("[%i], ",elements[i]); printf ("\n"); quickSort(elements, maxsize); printf("\nArray after sorting:\n"); for (i = 0; i < maxsize; i++) printf("[%i], ", elements[i]); } void quickSort(int elements[], int maxsize) { sort(elements, 0, maxsize - 1); } void sort(int elements[], int left, int right)

48

{ int pivot, l, r; l = left; r = right; pivot = elements[left]; while (left < right) { while ((elements[right] >= pivot) && (left < right)) right--; if (left != right) { elements[left] = elements[right]; left++; } while ((elements[left] <= pivot) && (left < right)) left++; if (left != right) { elements[right] = elements[left]; right--; } } elements[left] = pivot; pivot = left; left = l; right = r; if (left < pivot) sort(elements, left, pivot - 1); if (right > pivot) sort(elements, pivot + 1, right); }

49

EXPERIMENT 29:PROGRAM TO SORT LIST OF INTEGERS USING MERGE SORT #include<stdio.h> #include<conio.h> void main() { void mergesort(int *a,int *b,int *c,int n1,int n2,int *n); int n1=0,n2=0,n=0,i=0; int a[30],b[30],c[60]; clrscr(); printf("\n Enter two sorted arrays\n"); printf("\n Details of first array"); printf("\n Enter the number of elemnts in 1st array"); scanf("%d",&n1); printf("\nEnter elements"); for(i=0;i<n1;i++) scanf("%d",&a[i]); printf("\n Details of second array"); printf("\n Enter the number of elemnts in 1st array"); scanf("%d",&n2); printf("\nEnter elements"); for(i=0;i<n2;i++) scanf("%d",&b[i]); mergesort(a,b,c,n1,n2,&n); printf("\n Resultant array after merge sort\n"); for(i=0;i<n;i++) printf("%d\t",c[i]); getch(); } void mergesort(int *a,int *b,int *c,int n1,int n2,int *n) {

50

int i=0,j=0,k=0; while(i<n1 && j<n2) { if(a[i]<b[j]) { c[k]=a[i]; i++; } else { c[k]=a[i]; j++; } k++; } while(i<n1) { c[k]=a[i]; i++; k++; } while(j<n2) { c[k]=b[j]; j++; k++; } *n=k; } EXPERIMENT 30: SEARCHING AN ELEMENT IN A LIST OF

51

INTEGERS USING LINEAR SEARCH #include<stdio.h> #include<conio.h> void main() { int i,a[100],n,key; clrscr(); printf("\n Enter the size of the array"); scanf("%d",&n); printf("\nEnter the elements to sort"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter an element to search"); scanf("%d",&key); for(i=0;i<n;i++) if(key==a[i]) printf("\n%d is found at %d location",key,(i+1)); getch(); } EXPERIMENT 31:SEARCHING AN ELEMENT IN A LIST OF INTEGERS USING BINARY SEARCH int binsearch(int a[100],int n,int key); void bubblesort(int a[100],int n); void main() { int i,pos,n,key,a[100]; clrscr(); printf("\n how many elements you want"); scanf("%d",&n); printf("\nEnter elements");

52

for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter the elemnts to be searched"); scanf("%d",&key); bubblesort(a,n); printf("\nElemnts after sorting"); for(i=0;i<n;i++) printf("%d\n",a[i]); pos=-1; pos=binsearch(a,n,key); if(pos!=-1) printf("\n the key %d is found at %d",key,pos+1); else printf("\n the key %d is not found",key); getch(); } int binsearch(int a[],int n,int key) { int i,first=0,middle,last=n-1; while(last>=first) { middle=(last+first)/2; if(key>a[middle]) first=middle+1; else if(key<a[middle]) last=middle-1; else return middle; } return -1; }

53

void bubblesort(int arr[], int n) { int i, j, temp; for (i = 0; i < n - 1; i++) { for (j = i; j < n; j++) /* for(j=i+1;j<n;j++) */ { if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } EXPERIMENT 32: .#include<stdio.h> #include<conio.h> void main() { int i,ncon,nvow,nblank,nwords,ndigits,nsp; char str[100]; clrscr(); printf("\nEnter any string"); gets(str); i=ncon=nvow=nblank=ndigits=nsp=0; nwords=1; while(str[i]!='\0')

54

{ if((str[i]>64&&str[i]<91)||(str[i]>96&&str[i]<123)) { if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U') nvow++; else ncon++; } else if(str[i]==32) { nblank++; nwords++; } else if(str[i]>46&&str[i]<57) ndigits++; else nsp++; i++; } printf("\n number of characters = length of the string=%d",i); printf("\n number of vowels=%d",nvow); printf("\n number of consonats=%d",ncon); printf("\n no of digits=%d",ndigits); printf("\n number of special characters=%d",nsp); printf("\n number of blank spaces=%d",nblank); printf("\n no of words=%d",nwords); getch(); } EXPERIMENT 33: #include<stdio.h> #include<conio.h>

55

void main() { char str[100]; int i; clrscr(); printf("\nEnter string"); gets(str); for(i=0;str[i]!='\0';i++) { if(str[i]<91) printf("%c",str[i]+32); if(str[i]>96) printf("%c",str[i]-32); } getch(); } EXPERIMENT 34: #include<stdio.h> #include<conio.h> void main() { int i,j,k; char firstname[15]; char secondname[15]; char lastname[15]; char name[40]; clrscr(); printf("\n enter first name"); gets(firstname); printf("\n enter second name"); gets(secondname);

56

printf("\n enter last name"); gets(lastname); for(i=0;firstname[i]!='\0';i++) /* copy first name into name */ name[i]=firstname[i]; name[i]=' '; /* end first name with a space */ for(j=0;secondname[j]!='\0';j++) /*copy second name into name */ name[i+j+1]=secondname[j]; name[i+j+1]=' '; /* end second name with a space */ for(k=0;lastname[k]!='\0';k++) name[i+j+k+2]=lastname[k]; name[i+j+k+2]='\0'; /* end string name with a null character */ printf("\n%s",name); /* we can also use printf("\n%s",&name[0]); */ getch(); } EXPERIMENT 35: #include<stdio.h> #include<conio.h> void main() { char s1[15]; char s2[15]; int pos1,pos2,i,j=0; clrscr(); printf("\n Enter a string"); gets(s1); printf("\n Enter position 1"); scanf("%d",&pos1); printf("\n Enter pos2"); scanf("%d",&pos2); for(i=pos1;i<=pos2;i++) {

57

s2[j]=s1[i];; j++; } s2[j]='\0'; printf("\n extracted substring from %s is %s:",s1,s2); getch(); } EXPERIMENT 36: /*accessing variables using pointers*/ #include<stdio.h> void main( ) { int x,y; int *ptr; clrscr(); x=10 ; ptr=&x ; y=*ptr ; printf("\n value of x is %d\n\n",x); printf("\n%d is stored at address %u\n",x,&x); printf("\n%d is stored at address %u\n",*&x,&x); printf("%d is stored at address %u\n",*ptr,ptr); printf("%d is stored at address %u\n",y,&*ptr); printf("%u is stored at address %u\n",ptr,&ptr); printf("%d is stored at address %u\n",y,&y); *ptr=25; printf("\n now x=%d\n",x); getch(); } EXPERIMENT 37: #include<stdio.h>

58

#include<conio.h> void main() { int n, factorial; clrscr(); printf("\nEnter a number"); scanf("%d", &n); factcompute(n,&factorial); printf("\nThe factorial of %d is %d",n,factorial); getch(); } factcompute(int a,int *b) { int m; *b = 1; for(m = 1; m <= a; m++) *b = *b * m; } EXPERIMENT 38: #include<stdio.h> #include<conio.h> void main() { int i,ncon,nvow,nblank,nwords,ndigits,nsp; char *str; clrscr(); printf("\nEnter any string"); gets(str); i=ncon=nvow=nblank=ndigits=nsp=0; nwords=1; while(*str!='\0')

59

{ if((*str>64&&*str<91)||(*str>96&&*str<123)) { if(*str=='a'||*str=='e'||*str=='i'||*str=='o'||*str=='u'||*str=='A'||*str=='E'||*str=='I'||*str=='O'||*str=='U') nvow++; else ncon++; } else if(*str==32) { nblank++; nwords++; } else if(*str>46&&*str<57) ndigits++; else nsp++; str++; i++; } printf("\n number of characters = length of the string=%d",i); printf("\n number of vowels=%d",nvow); printf("\n number of consonats=%d",ncon); printf("\n no of digits=%d",ndigits); printf("\n number of special characters=%d",nsp); printf("\n number of blank spaces=%d",nblank); printf("\n no of words=%d",nwords); getch(); } EXPERIMENT 39: #include <stdlib.h>

60

#include <stdio.h> #define MAXSIZE 500 void main() { int i,j,temp,n,*arr; printf("\nHow many arr you want to sort: "); scanf("%d",&n); printf("\nEnter the values one by one: "); for (i = 0; i < n; i++) { printf ("\nEnter element %i :",i); scanf("%d",&arr[i]); } printf("\nArray before sorting:\n"); for (i = 0; i < n; i++) printf("[%i], ",arr[i]); printf ("\n"); printf("\nArray elements in reverse order"); for(i=n;i>=0;i--) printf("%d\t",arr[i]); for (i = 0; i < n - 1; i++) { for (j = i; j < n; j++) /* for(j=i+1;j<n;j++) */ { if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }

61

} printf("\nArray after sorting:\n"); for (i = 0; i < n; i++) printf("[%i], ", arr[i]); printf("\n largest element in the array is %d",arr[i-1]); printf("\n smallest element in the array is %d",arr[0]); getch(); } EXPERIMENT 40: /*program to find the length of a char string */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20],*p; int i=0; printf("enter a string \n"); scanf ("%s",str); p=str; while(*p!='\0') { i++; p++; } printf("\nthe length of the given string is %d",i); getch(); } EXPERIMENT 41: #define COUNT 5 void main()

62

{ char *names[5], temp[20]; int m, n; clrscr(); for (m = 0; m < COUNT; m++) { printf("\nEnter name[%d] : ",m); names[m] = (char *) malloc (20); scanf("%s",names[m]); } for (m = 1; m <= COUNT; m++) for (n = 1; n <= COUNT - m; n++) if (strcmp(names[n - 1], names[n]) > 0) { strcpy(temp, names[n - 1]); strcpy(names[n - 1], names[n]); strcpy(names[n], temp); temp[0] = '\0'; } printf("\nSorted list is "); for (m = 0; m < COUNT; m++) printf("\n%s", names[m]); getch(); } EXPERIMENT 42: #include<stdio.h> #include<conio.h> void main() { int strcopy(char *s2,char *s1); char s1[20],s2[20];

63

clrscr(); printf("\nEnter the string"); gets(s1); strcopy(s2,s1); puts("\n after copying"); printf("\nthe string s1 is"); puts(s1); printf("\nthe string s2 is"); puts(s2); getch(); } int strcopy(char *s2,char *s1) { while(*s1!=0) { *s2=*s1; s1++; s2++; } *s2='\0'; } EXPERIMENT 43: #include<conio.h> #include<stdio.h> void main() { int compare_function(); int result; char inputA[20],inputB[20]; printf("\nEnter string A : "); gets(inputA);

64

printf("\nEnter string B : "); gets(inputB); result = compare_function(inputA,inputB); if (result == 0) printf("\nStrings are same"); else printf("\nStrings are different"); getch(); } int compare_function(char *a,char *b) { while (*a != '\0') { if (*a == *b) { a++; b++; } else return 1; } return 0; } EXPERIMENT 44: /* Write a C program that uses functions to perform the following: i) Addition of Two Matrices ii) Multiplication of Two Matrices */ #include<stdio.h>

65

void main() { int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10]; clrscr(); printf("************************************"); printf("\n\t\tMENU"); printf("\n**********************************"); printf("\n[1]ADDITION OF TWO MATRICES"); printf("\n[2]MULTIPLICATION OF TWO MATRICES"); printf("\n[0]EXIT"); printf("\n**********************************"); printf("\n\tEnter your choice:\n"); scanf("%d",&ch); if(ch<=2 & ch>0) { printf("Valid Choice\n"); } switch(ch) { case 1: printf("Input rows and columns of A & B Matrix:"); scanf("%d%d",&r1,&c1); printf("Enter elements of matrix A:\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&a[i][j]); } printf("Enter elements of matrix B:\n");

66

for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&b[i][j]); } printf("\n =====Matrix Addition=====\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%5d",a[i][j]+b[i][j]); printf("\n"); } break; case 2: printf("Input rows and columns of A matrix:"); scanf("%d%d",&m,&n); printf("Input rows and columns of B matrix:"); scanf("%d%d",&p,&q); if(n==p) { printf("matrices can be multiplied\n"); printf("resultant matrix is %d*%d\n",m,q); printf("Input A matrix\n"); read_matrix(a,m,n); printf("Input B matrix\n"); /*Function call to read the matrix*/ read_matrix(b,p,q); /*Function for Multiplication of two matrices*/ printf("\n =====Matrix Multiplication=====\n"); for(i=0;i<m;++i)

67

for(j=0;j<q;++j) { c[i][j]=0; for(k=0;k<n;++k) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf("Resultant of two matrices:\n"); write_matrix(c,m,q); } /*end if*/ else { printf("Matrices cannot be multiplied."); } /*end else*/ break; case 0: printf("\n Choice Terminated"); exit(); break; default: printf("\n Invalid Choice"); } getch(); } /*Function read matrix*/ int read_matrix(int a[10][10],int m,int n)

68

{ int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); return 0; } /*Function to write the matrix*/ int write_matrix(int a[10][10],int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%5d",a[i][j]); printf("\n"); } return 0; }

69