# Exercise 1 Solving problems such as temperature conversion, student grading, income tax calculation, etc which expose the

students to use basic C operators. a) Temperature Conversion from Fahrenheit to Celsius. Program #include<stdio.h> #include<conio.h> void main() { float f,c; clrscr(); printf("\n Enter Temperature in F: "); scanf("%f",&f); c=5.0/9*(f-32); printf(" Temperature in C: %f",c); getch(); } Output Enter Temperature in F: 98 Temperature in C: 36.666668 b) Student grading based on his percentage. Program #include<stdio.h> #include<conio.h> void main() { int p; clrscr(); printf("\n Enter Percentage: "); scanf("%d",&p); if(p>=70) printf(" Distinction"); else if(p>=60&&p<70) printf(" First class"); else if(p>=50&&p<60) printf(" Second class");

1

else if(p>=40&&p<50) printf(" Third class"); else printf(" Fail"); getch(); } Output Enter Percentage: 73 Distinction

c) Average of three numbers Program #include<stdio.h> #include<conio.h> void main() { int a,b,c; float avg; clrscr(); printf("\n Enter a,b,c: "); scanf("%d%d%d",&a,&b,&c); avg=(float)(a+b+c)/3; printf(" Avg: %f",avg); getch(); } Output Enter a,b,c: 62 45 90 Avg: 65.666664

2

d) Largest among three numbers Program #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("\n Enter a,b,c: "); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) printf(" a is large"); else printf(" c is large"); } else if(b>c) printf(" b is large"); else printf(" c is large"); getch(); } Output Enter a,b,c: 5 9 3 b is large

3

else if(a>200000&&a<=300000) tax=(a-200000)*10/100. } Output Enter amount: 600000 Tax: 80000 200000 300000 500000 0% 10% 20% 30% 4 . printf("\n Enter amount: ").&a). getch(). printf(" Tax: %ld". clrscr().tax). scanf("%ld".tax=0. if(a<=200000) tax=0.h> #include<conio. else if(a>300000&&a<=500000) tax=10000+(a-300000)*20/100.h> void main() { long int a.e) Income Tax calculation 0 – 200000 – 300000 – Above 500000 Program #include<stdio. else if(a>500000) tax=50000+(a-500000)*30/100.

} } for(i=len-1. } printf(" 2's complement: %s". Thus 2’s complement of 11100 is 00100.len. gets(a). clrscr(). exit(0).h> #include<string. Program #include<stdio.k++) { if(a[k]!='0'&&a[k]!='1') { printf(" Incorrect Binary number.j>=0.a[k]!='\0'.. for(j=i-1.h> #include<conio. getch().j--) { if(a[j]=='1') a[j]='0'.k.i--).a[i]!='1'. int i. Write a C program to find the 2’s complement of a binary number.a). 5 . len=strlen(a)."). else a[j]='1'.h> void main() { char a[20].j..Exercise 2 2’s complement of a number is obtained by scanning it from right to left and complementing all the bits after the first appearance of a 1. for(k=0. printf("\n Enter Binary string: ").h> #include<stdlib.

} else { while(n!=0) { sum=sum+n%10.").getch(). printf("\n Enter n: "). } Output Enter Binary string: 110101010 2's complement: 001010110 Exercise 3 a) Write a C Program to find the sum of individual digits of a positive integer Program #include<stdio.h> #include<conio. } Output Enter n: 2346 Sum of individual digits: 15 6 .h> void main() { int n.. n=n/10. } getch().&n). clrscr().sum=0. scanf("%d". if(n<0) { printf("Enter positive integer.sum). } printf(" Sum of individual digits: %d".

h> void main() { int a=0.\n")..h> #include<conio.n.i++) { c=a+b. scanf("%d". printf("\n The Fibonacci sequence. printf(" %d\n %d". b=c. Write a C program to generate the first n terms of the sequence.b). Subsequent terms are found by adding the preceding two terms in the sequence.i. a=b. } Output Enter n: 10 The Fibonacci sequence… 0 1 1 2 3 5 8 7 .b=1.c. printf("\n Enter n: "). clrscr().&n).a.. Program #include<stdio. printf("\n %d". } getch(). for(i=3.i<=n.c).b) A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and 1.

Program #include<stdio..i).i++) { count=0.&n).. } Output Enter n: 45 Prime numbers between 1 and 45 are. } if(count==0) printf("\n %d"..count=0. } getch().i<=n.n. printf("\n Enter n: ").j<=i-1. for(j=2.". scanf("%d".h> void main() { int i.n).j. for(i=2.. where n is a value supplied by the user. printf(" Prime numbers between 1 and %d are. 2 3 5 7 11 13 17 8 .j++) { if(i%j==0) count++.h> #include<conio.13 21 34 c) Write a C program to generate all the prime numbers between 1 and n. clrscr().

for(i=2.i<20. 9 ."). } Output Enter n: 144 Fibonacci number.count=0."). clrscr(). } printf("\n Enter n: "). getch()..&n).a[20]. break..i++) { if(n==a[i]) { count++.. scanf("%d".i<20.i.19 23 29 31 37 41 43 d) Write a program which checks a given integer is Fibonacci number or not Program #include<stdio. a[1]=1. a[0]=0.h> void main() { int n.h> #include<conio.i++) { a[i]=a[i-1]+a[i-2].. } } if(count>0) printf(" Fibonacci number.. else printf(" Not a fibonacci number.. for(i=0.

t.x. scanf("%f". } printf("\n The Sum of Series: %f". } Output Enter x: 2 The Sum of Series: -0.k)*pow(x. while(t!=0) { f=f*t.&x).416155 10 .i<=10.i))/(float)f.Exercise 4 a) Write a C program to calculate the following Sum: Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! Program #include<stdio. for(i=2.h> void main() { long int i. } sum=sum+(pow(-1. float sum=1. t--.h> #include<conio. k++.k=1.i=i+2) { t=i.h> #include<math.f=1. clrscr().0. f=1.sum). getch(). printf("\n Enter x: ").

h> #include<conio. else printf("\n x1=%f+i%f x2=%f-i%f".d.&c).p. clrscr(). q=sqrt(-d)/(2*a).h> void main() { float a. scanf("%f%f%f".b) Write a C program toe find the roots of a quadratic equation.b..c.. x1=(-b+sqrt(d))/(2*a). printf("\n x1=x2=%f"..h> #include<math. printf("\n x1=%f \n x2=%f".q.&a.p.c: "). printf("\n Enter a.. } getch(). 11 ..x2."). } else if(d>0) { printf(" Roots are real. Program #include<stdio. if(q<0) printf("\n x1=%f+i%f x2=%f-i%f". } else //if(d<0) { printf(" Roots are imaginary.").-q).b.x1.. if(d==0) { printf(" Roots are real and equal. p=-b/(2*a).q).-b/(2*a)).q.p.&b.x2).x1.p. d=b*b-4*a*c.").p. x2=(-b-sqrt(d))/(2*a).-q.

h> void main() { float s.000000 Enter u and a: 3 4 12 .&a.t++) { printf(" Enter u and a: ").&u)..a. } getch().&n).) and acceleration (m/sec2).u. for(t=0. printf(" Distance traveled at %d time interval: %f \n". } Output Enter Time Intervals: 2 Enter u and a: 2 7 Distance traveled at 0 time interval: 0.000000 Enter u and a: 4 9 Distance traveled at 1 time interval: 11.t.n. s=u*t+0.h> #include<conio..} Output Enter a. scanf("%d". x1=x2=-0. Program #include<stdio. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of ‘u’ and ‘a’. int t. Write C program to find the distance traveled at regular intervals of time given the values of ‘u’ and ‘a’. printf("\n Enter Time Intervals: ").t<=n.s).500000 Exercise 5 a) The total distance traveled by vehicle in ‘t’ seconds is given by distance = ut+1/2at2 where ‘u’ and ‘a’ are the initial velocity (m/sec. scanf("%f%f". clrscr().5*a*t*t.c: 4 4 1 Roots are real and equal.b.

/. case '*': c=a*b.h> #include<conio.b: "). case '-': c=a-b.&b).-. char op./. (Consider the operators +.c. printf("\n Enter a.%): % 13 .h> void main() { int a.*.*. break. which takes two integer operands and one operator form the user. getch(). switch(op) { case '+': c=a+b. performs the operation and then prints the result. scanf("%d%d". default : printf("\n Enter correct operator. /.%): ").. break.b: 87 34 Enter Operator (+. } Output Enter a.Distance traveled at 2 time interval: 14. break.000000 b) Write a C program. } printf("\n Result: %d". getch(). break. exit(0).-. printf(" Enter Operator (+."). break. op=getche(). % and use Switch Statement) Program #include<stdio.b.&a.c). case '%': c=a%b. clrscr(). case '/': c=a/b.-.*.

else printf("\n Two strings are not equal"). strrev(d).b[10]="KRISHNA".d[10]. len=strlen(a). strcpy(e.e[20]. if(strcmp(a.e).a). printf("\n %s". strcpy(d. } 14 .a).b). int len.len). printf("\n %s". getch().Result: 19 Exercise 6 a) Simple programming examples to manipulate strings.d). strlwr(b). clrscr(). strupr(a). Program #include<string. else printf("\n Two strings are not equal").h> void main() { char a[10]="srinivas". printf("\n %s".c)==0) //compares with case sensitivity printf("\n Two strings are equal ").c)==0) //compares without case sensitivity printf("\n Two strings are equal").c[10]="srinivas".d).a). strcat(e. printf("\n Length=%d". printf("\n %s". printf("\n %s". if(stricmp(a.b).

printf("\n Enter String: ").Output Length=8 srinivas srinivasKRISHNA savinirs krishna SRINIVAS Two strings are not equal Two strings are equal b) Verifying a string for its palindrome property Program #include<stdio. getch(). gets(a).i<len/2. getch(). for(i=0.. clrscr()."). int len. } } printf("\n Palindrome…").h> void main() { char a[20].i++) { if(a[i]==a[len-1]) len--.h> #include<string.i. else { printf("\n Not Palindrome. len=strlen(a). } Output Enter String: srinirs Palindrome… 15 .. exit().h> #include<conio.

i++) c[i]=a[i].b[10]. getch().c[30]. for(i=0.Exercise 7 Write a C program that uses functions to perform the following operations i) To insert a sub-string in to given main string from a given position. printf("\n Enter the Main String: ").h> #include<conio. char a[20]. r=strlen(b).j=0.k. gets(a). } 16 .r. printf(" Enter the String to insert: "). k=i.c).i<p+r-1. clrscr(). Program #include<stdio.h> #include<string. printf(" Resultant String: %s".i++) c[i]=a[k++].h> void main() { int i. printf(" Enter position to insert: ").n.&p). c[i]='\0'. n=strlen(a). for(i=p-1.i<n+r.i++) c[i]=b[j++]. gets(b).p. scanf("%d". for(i=p+r-1.i<p-1.

h> #include<string.&p). Program #include<stdio.i++) a[i]=a[k++]. printf(" Enter Position: "). scanf("%d".h> #include<conio. k=p+n-1. printf(" Enter Number of chars to delete: ").len. int i. scanf("%d".a).Output Enter the Main String: srinivas Enter the String to insert: Hari Enter position to insert: 4 Resultant String: sriHarinivas ii) To delete n Characters from a given position in a given string.n.p.k. getch(). a[i]='\0'. clrscr(). gets(a). printf("\n Enter String: "). for(i=p-1.h> void main() { char a[20]. printf(" Resultant String: %s".i<len-n.&n). } Output Enter String: srinivas Enter Position: 4 Enter Number of chars to delete: 2 Resultant String: srivas 17 . len=strlen(a).

Program #include<stdio.a). printf("\n Enter string: "). printf("\n New string: %s".len. printf("\n New string: %s".&pos).h> #include<conio. gets(a). a[0]=ch.pos. scanf("%d".At position \n"). 18 . printf("\n Enter position: "). ch=getche().h> #include<string. ch=getche(). case 3: printf(" Enter character to replace at given position: "). printf(" 1. len=strlen(a). case 2: printf(" Enter character to replace at end: "). int choice. printf(" New string: %s".At Begin \n 2.iii) To replace a character of a string either from beginning or ending or at a specified location.a). break.a).&choice). ch=getche(). scanf("%d". a[len-1]=ch. a[pos-1]=ch.At End \n 3. clrscr(). printf(" Enter choice: ").h> void main() { char a[20]. break.ch. switch(choice) { case 1: printf(" Enter character to replace at begin: ").

At Begin 2. default:printf(" Enter correct choice").At End 3.At Begin 2. break.break.At End 3. } getch().At position Enter choice: 1 Enter character to replace at begin: Q New string: Qri nivas Enter string: sri nivas 1. } Output Enter string: sri nivas 1.At position Enter choice: 3 Enter character to replace at given position: Z Enter position: 4 New string: sriZnivas 19 .At position Enter choice: 2 Enter character to replace at end: W New string: sri nivaW Enter string: sri nivas 1.At Begin 2.At End 3.

struct complex b). a=read(). scanf("%d". }. void main() { struct complex a. printf("\n 1. printf("\n Enter Real and Imag for 1st Complex Number: ").b.struct complex b).h> struct complex add(struct complex a. void write(struct complex s).c. b=read(). struct complex { float r.i. struct complex mul(struct complex a.Add \n 2. switch(ch) 20 . printf("\n Enter Real and Imag for 2nd Complex Number: "). struct complex read().Exercise 8 Write a C program that uses functions to perform the following operations: i) Reading a complex number ii) Writing a complex number iii) Addition of two complex numbers iv) Multiplication of two complex numbers Program #include<stdio. printf("\n Enter choice: "). clrscr().&ch).h> #include<conio.Multiplication "). int ch.

i=a.{ case 1 : c=add(a.r-a.r. s.s.i*b. } struct complex mul(struct complex a.b).r=a. } void write(struct complex s) { if(s.s.&s.struct complex b) { struct complex s. break.i*b.i+b. s. return s.&s.").i).-1*s.r*b. else printf("\n Result=%f-i%f".r. } struct complex add(struct complex a.i). s. return s. default: printf("\n Enter Correct choice.i.r.i=a. break. case 2 : c=mul(a.i>0) printf("\n Result=%f+i%f". return s.r=a.r+b. break. } struct complex read() { struct complex s.r.i+a.struct complex b) { struct complex s.r. } getch(). write(c). s. write(c).r*b. } Output Enter Real and Imag for 1st Complex Number: 3 -2 Enter Real and Imag for 2nd Complex Number: -5 21 ..i.b).i). scanf("%f%f".s.

&n). for(i=0.j<n. scanf("%d%d". for(i=0. } } printf("\n Addition of two matrices: \n").&b[i][j]). scanf("%d%d". printf("\n Enter Order for 1st Matrix: ").i<m.000000+i28.Add 2.i.Multiplication Enter choice: 2 Result=-3. } } printf("\n Enter 2nd Matrix elements: ").n.000000 Exercise 9 a) Addition of two matrices Program #include<stdio.i<p.b[4][4].i++) { for(j=0.h> #include<conio.j++) { scanf("%d".j.i<m.m. for(i=0.q.i++) { for(j=0.h> void main() { int a[4][4].c[4][4].&p. clrscr().6 1.j++) { scanf("%d".p. if((m==p)&&(n==q)) { printf("\n Enter 1st Matrix elements: ").j<q. printf("\n Enter Order for 2nd Matrix: ").&m.&q).i++) 22 .&a[i][j]).

j<n.").j++) { c[i][j]=a[i][j]+b[i][j]. } getch().{ for(j=0. } } else { printf("\n Addition not possible. } printf("\n").. } Output Enter Order for 1st Matrix: 3 2 Enter Order for 2nd Matrix: 3 2 Enter 1st Matrix elements: 1 2 3 4 5 6 Enter 2nd Matrix elements: 6 5 4 3 2 1 Addition of two matrices: 7 7 7 7 7 7 23 .. printf("%d \t".c[i][j]).

} Output Enter Order for matrix: 3 4 24 . printf(" Enter Matrix elements: ").j++) { printf("%d \t". printf("\n Enter Order for matrix: ").m.&a[i][j]). for(i=0.i<n.n.i++) { for(j=0.&m. for(j=0. for(i=0.i++) { printf("\n").h> #include<conio.&n).i<m.j<m.b) Calculating transpose of matrix in-place manner Program #include<stdio.j<n. } } printf(" Transpose of Matrix: "). scanf("%d%d".j++) { scanf("%d". b[j][i]=a[i][j]. clrscr().i.b[4][4].j. } } getch().b[i][j]).h> void main() { int a[4][4].

q.j++) { scanf("%d".j++) 25 .j<q. scanf("%d%d". printf("\n Enter Order for 1st Matrix: ").&q).j++) { scanf("%d".n.h> void main() { int a[4][4].&p.i++) { for(j=0. for(i=0.&a[i][j]). for(i=0. scanf("%d%d".i++) { for(j=0.i<m.&m. for(i=0. } } printf("\n Multiplication of two matrices: \n").i.i<p.i<m. if(n==p) { printf("\n Enter 1st Matrix elements: "). printf("\n Enter Order for 2nd Matrix: ").j<q.k.j.c[4][4].&n).h> #include<conio.b[4][4]. } } printf("\n Enter 2nd Matrix elements: ").i++) { for(j=0.p.m. clrscr().Enter Matrix elements: 1 2 3 4 5 6 7 8 9 10 11 12 Transpose of Matrix: 1 5 9 2 6 10 3 7 11 4 8 12 c) Matrix Multiplication by checking compatibility Program #include<stdio.&b[i][j]).j<n.

} Output Enter Order for 1st Matrix: 3 3 Enter Order for 2nd Matrix: 3 2 Enter 1st Matrix elements: 1 2 3 4 5 6 7 8 9 Enter 2nd Matrix elements: 6 5 4 3 2 1 Multiplication of two matrices: 20 14 56 41 92 68 26 . for(k=0.k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]. } } else { printf("\n Multiplication not possible.k<n. } printf("\n").c[i][j])."). printf("%d \t"... } getch().{ c[i][j]=0.

n.i.h> #include<conio. i) To find the factorial of a given integer.n.i++) { f=f*i. getch().f). } Output Enter n: 7 Factorial of 7 is: 5040 Program using Recursive #include<stdio. printf("\n Enter n: ").h> 27 .i<=n.h> #include<conio.h> void main() { int f=1. for(i=1.&n). scanf("%d".Exercise 10 a) Write C programs that use both recursive and non-recursive functions. } printf("\n Factorial of %d is: %d". clrscr(). Program using Non-Recursive #include<stdio.

getch(). clrscr(). printf("\n Factorial of %d is: %d".gcd.h> #include<conio.i. for(i=1.n. } int fact(int n) { int p. else p=n*fact(n-1).i<=m.i.m. return p.b.&n). scanf("%d%d". printf("\n Enter a.i++) { if(a%i==0&&b%i==0) 28 .b: ").fact(n)). GCD using Non-Recursive #include<stdio. if(n==1) return 1. printf("\n Enter n: "). else m=a. clrscr(). } Output Enter n: 7 Factorial of 7 is: 5040 ii) To find the GCD (greatest common divisor) of two given integers.void main() { int f.&b).&a. if(a>b) m=b.n.h> void main() { int a. scanf("%d".

h> void main() { int a.gcd).gcd(a.h> #include<conio.h> void towers(int n.a.b.b.&b). 29 . scanf("%d%d".gcd=i.char src.a). printf("\n GCD of %d and %d is: %d". getch().&a. } int gcd(int a.a%b). } Output Enter a.a. } Output Enter a. Program using Recursive #include<stdio. getch().int b) { if(b>a) return gcd(b. char target.b.b: 36 24 GCD of 36 and 24 is: 12 iii) To solve Towers of Hanoi problem. clrscr(). if(b==0) return a. else return gcd(b. printf("\n Enter a.b)). } printf("\n GCD of %d and %d is: %d".h> #include<conio. char temp).b: 36 24 GCD of 36 and 24 is: 12 GCD using Recursive #include<stdio.b: ").

target). towers(n.src. getch(). towers(n-1.temp.'A'. clrscr().temp. } void towers(int n. char temp) { if(n==1) { printf("\n Move top disk from %c to %c". return. char src. scanf("%d".src.target).void main() { int n.src. printf("\n How Many disks: ").'B').'C'. } Output How Many disks: 4 Move top disk from A to B Move top disk from A to C Move top disk from B to C Move top disk from A to B Move top disk from C to A Move top disk from C to B Move top disk from A to B Move top disk from A to C Move top disk from B to C Move top disk from B to A Move top disk from C to A Move top disk from B to C Move top disk from A to B Move top disk from A to C Move top disk from B to C 30 .target). char target.&n).src). } towers(n-1.target. printf( "\n Move top disk from %c to %c".

i.min.&a[i]). min=max=a[0].Exercise 11 a) Write a C Function to find both largest and smallest numbers of an array of integers.i<n. scanf("%d".i++) scanf("%d". clrscr().h> #include<conio. for(i=0.i++) { if(a[i]<min) min=a[i]. if(a[i]>max) max=a[i].i<n. getch(). for(i=0.max. } Output Enter n: 6 Enter elements: 1 5 -65 8 31 .h> void main() { int a[10].n. printf("\n Enter n: "). printf(" Enter elements: ").&n). } printf(" Largest=%d \n Smallest=%d ". Program #include<stdio.max.min).

found=strstr(s. words and characters in a given text.".936 421 Largest=936 Smallest=-65 b) Write a C function that uses functions to perform the following..found-s+1)..h> #include<string. } Output Enter String: srinivas Enter Substring to find: iva Found at 6 position ii) Write a C program to count the lines..t[10].*found. if(found) printf(" Found at %d position. Program 32 . clrscr(). else printf(" Substring not found. getch(). gets(s).h> #include<conio. Program #include<stdio. i) Write a C program that displays the position or index in the string S where the string T begins.")..h> void main() { char s[20]. printf("\n Enter String: "). or – 1 if S doesn’t contain T. gets(t). printf(" Enter Substring to find: ").t).

int i.nc).#include<stdio.ch. printf("\n Number of Characters: %d".h> void main() { char a[80].nl).\n"). else { nw++. } printf("\n Number of Lines : %d".. clrscr(). nc=nc+strlen(a). printf("\n Enter the Text and press ENTER at end.nw). if(a[0]=='\0') break.h> #include<conio. a[k]='\0'.h> #include<string.end=0. for(i=0. while(end==0) { k=0. getch(). } Output Enter the Text and press ENTER at end. printf("\n Number of Words : %d".nw=0..nl=0. } } nl=nl+1.nc=0. Hello World How are you !!!! 33 .i++) { if(a[i]==' '||a[i]=='\t') nw++. while((ch=getchar())!='\n') a[k++]=ch.a[i]!='\0'.k.

x. printf("\n Rows you want to input: "). printf("%6d". while(y<r) { for(i=40-3*y. printf("\n Pascal's Triangle:\n"). } printf("\n"). scanf("%d".Number of Lines :2 Number of Words :6 Number of Characters: 27 Exercise 12 a) Write a C function to generate Pascal’s triangle. } getch().h> #include<conio.r. clrscr().y=0. for(x=0.i--) printf(" ").&r).x++) { if((x==0)||(y==0)) p=1.i>0. else p=(p*(y-x+1))/x.p).h> void main() { int p=1.i. } Output 34 . Program #include<stdio.x<=y. y++.

&n). } Output Enter n: 4 Pyramid Triangle 0 101 21012 3210123 432101234 35 . Program #include<stdio. for(i=-y.Rows you want to input: 6 Pascal's Triangle: 1 1 1 1 1 1 5 4 10 3 6 2 3 4 10 1 1 1 1 5 1 b) Write a C function to construct a pyramid of numbers.x=35.h> void main() { int n. x=x-2.y+4).i++) printf("%2d". for(y=0. printf("\n Pyramid Triangle \n").h> #include<conio. scanf("%d".i. } getch().y<=n.y++) { gotoxy(x.y. clrscr().abs(i)).i<=y. printf("\n Enter n: ").

} else { sum=1. main(). } printf("\n Sum of series: %ld".h> #include<math.&x.x.&n).Exercise 13 Write a C program to read in two numbers.").+xn Program #include<stdio.. if(n<=0||x<=0) { printf("\n Value is not valid. printf("\n Enter the values for x and n: "). for(i=1.sum). x and n.h> void main() { int i. and then compute the sum of this geometric progression: 1+x+x2+x3+…………. } 36 . } getch(). getch(). scanf("%d%d". clrscr().i).i++) { sum=sum+pow(x. long int sum.n.h> #include<conio.i<=n.

cos(x)=1-x2/2!+x4/4!-x6/6!+x8/8!-……. while(t!=0) { f=f*t.h> #include<conio. getch().k)*pow(x.i=i+2) { t=i. scanf("%ld".i))/f.sum). for(i=2. float sum=1.Output Enter the values for x and n: 5 3 Sum of series: 156 Write a C function to read in two numbers. k++. f=1. } 37 .h> #include<math. printf("\n Enter x: ").h> void main() { long int i.&n). x and n (number of terns) and then compute sin(x) and cos(x).n.f=1. } printf("\n cos(x)= %f". t--. printf(" Enter n: "). scanf("%f".x.k=1.i<n*2.&x). } sum=sum+(pow(-1.t. clrscr(). Program #include<stdio.

h> void main() { long int i.i))/f.i<n*2. scanf("%ld". clrscr().x. scanf("%f".k)*pow(x.&n). } Output Enter x: 2 38 . t--. sum=x.sum). } sum=sum+(pow(-1.h> #include<conio.&x).Output Enter x: 2 Enter n: 6 cos(x)= -0.t.f=1. float sum.k=1.i=i+2) { t=i. while(t!=0) { f=f*t. printf("\n Enter x: ").h> #include<math. } printf("\n sin(x)= %f". for(i=3.416155 sin(x)=x-x3/3!+x5/5!-x7/7!+x9/9!-……. printf(" Enter n: "). getch(). f=1. k++. Program #include<stdio.

907937 39 .Enter n: 4 sin(x)= 0.