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

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

a[i]!='1'. printf("\n Enter Binary string: "). getch(). for(j=i-1.k++) { if(a[k]!='0'&&a[k]!='1') { printf(" Incorrect Binary number.a[k]!='\0'. int i.h> void main() { char a[20].j--) { if(a[j]=='1') a[j]='0'.h> #include<stdlib.h> #include<conio.").k.j>=0. clrscr().. len=strlen(a). exit(0).h> #include<string.len. else a[j]='1'. Write a C program to find the 2’s complement of a binary number.i--). } } for(i=len-1. for(k=0.. 5 . Program #include<stdio. Thus 2’s complement of 11100 is 00100. } printf(" 2's complement: %s".j.a).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. gets(a).

sum).&n). } printf(" Sum of individual digits: %d".h> void main() { int n.sum=0.. printf("\n Enter n: "). } getch(). } Output Enter n: 2346 Sum of individual digits: 15 6 . } else { while(n!=0) { sum=sum+n%10. n=n/10. if(n<0) { printf("Enter positive integer. clrscr().h> #include<conio. scanf("%d". } 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.").getch().

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

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

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

getch(). } sum=sum+(pow(-1.i))/(float)f.h> #include<conio. f=1. printf("\n Enter x: "). } Output Enter x: 2 The Sum of Series: -0.i=i+2) { t=i. for(i=2. clrscr().h> void main() { long int i. } printf("\n The Sum of Series: %f".0. float sum=1.&x).k)*pow(x.x. while(t!=0) { f=f*t. t--.h> #include<math.416155 10 .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.k=1. k++.t.i<=10.sum). scanf("%f".f=1.

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

h> #include<conio.u. Program #include<stdio.t<=n.s).&a. int t. } Output Enter Time Intervals: 2 Enter u and a: 2 7 Distance traveled at 0 time interval: 0.t++) { printf(" Enter u and a: "). Write C program to find the distance traveled at regular intervals of time given the values of ‘u’ and ‘a’.b.a.) and acceleration (m/sec2).} Output Enter a. 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’..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. } getch().. scanf("%f%f".&n). for(t=0.h> void main() { float s. s=u*t+0.c: 4 4 1 Roots are real and equal. clrscr().5*a*t*t. scanf("%d". printf(" Distance traveled at %d time interval: %f \n".000000 Enter u and a: 3 4 12 .t. printf("\n Enter Time Intervals: ").&u).n.000000 Enter u and a: 4 9 Distance traveled at 1 time interval: 11. x1=x2=-0.

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

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

getch().h> #include<string. gets(a).").. } Output Enter String: srinirs Palindrome… 15 . getch().h> #include<conio.i<len/2. for(i=0.i.i++) { if(a[i]==a[len-1]) len--. exit(). else { printf("\n Not Palindrome. } } printf("\n Palindrome…"). len=strlen(a).. int len.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. printf("\n Enter String: ").h> void main() { char a[20]. clrscr().

} Output 16 .i<p-1.c[30].i++) c[i]=a[k++]. printf(" Enter position to insert: ").c). printf(" Resultant String: %s". getch(). n=strlen(a).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.p.i++) c[i]=a[i].k. gets(a).i<n+r.j=0.h> #include<string. printf("\n Enter the Main String: "). for(i=p+r-1.i++) c[i]=b[j++].r. k=i. c[i]='\0'. for(i=p-1. char a[20]. for(i=0.h> #include<conio. r=strlen(b).h> void main() { int i. printf(" Enter the String to insert: ").i<p+r-1. scanf("%d".n.b[10]. clrscr().&p). gets(b). Program #include<stdio.

clrscr(). k=p+n-1.h> #include<string.p. printf(" Enter Position: ").&n). for(i=p-1. printf(" Enter Number of chars to delete: ").i<len-n. len=strlen(a).&p).n. } Output Enter String: srinivas Enter Position: 4 Enter Number of chars to delete: 2 Resultant String: srivas 17 . gets(a).len. getch(). Program #include<stdio.a). a[i]='\0'. scanf("%d". scanf("%d".k.i++) a[i]=a[k++].h> void main() { char a[20].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.h> #include<conio. printf("\n Enter String: "). printf(" Resultant String: %s". int i.

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

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

printf("\n Enter Real and Imag for 2nd Complex Number: "). printf("\n 1. void main() { struct complex a.struct complex b). clrscr(). void write(struct complex s).Add \n 2.i.struct complex b). scanf("%d". b=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.h> struct complex add(struct complex a.h> #include<conio. switch(ch) { 20 .c. struct complex { float r.&ch). a=read(). printf("\n Enter Real and Imag for 1st Complex Number: "). printf("\n Enter choice: ").b. int ch.Multiplication "). struct complex read(). struct complex mul(struct complex a.

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

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

."). } printf("\n")..c[i][j]). } getch().for(j=0. } } else { printf("\n Addition not possible. printf("%d \t". } 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 .j<n.j++) { c[i][j]=a[i][j]+b[i][j].

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

i<m. printf("\n Enter Order for 2nd Matrix: "). scanf("%d%d".p. printf("\n Enter Order for 1st Matrix: ").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. clrscr().j.j++) { 25 .j<q. scanf("%d%d".j<q.h> void main() { int a[4][4].i.&m.i<p.&a[i][j]). } } printf("\n Enter 2nd Matrix elements: ").j++) { scanf("%d".k.c[4][4].&n).h> #include<conio. for(i=0. for(i=0.q.j++) { scanf("%d".i++) { for(j=0.j<n. for(i=0.i++) { for(j=0.&q).&b[i][j]).m. } } printf("\n Multiplication of two matrices: \n").&p.i<m.n. if(n==p) { printf("\n Enter 1st Matrix elements: ").b[4][4].i++) { for(j=0.

} printf("\n").k<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 .. printf("%d \t".c[i][j]). for(k=0.. } getch(). } } else { printf("\n Multiplication not possible.k++) c[i][j]=c[i][j]+a[i][k]*b[k][j].c[i][j]=0.").

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

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

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

src.{ int n. char target.target).target).'C'. getch(). char temp) { if(n==1) { printf("\n Move top disk from %c to %c".src. clrscr().src). towers(n-1.&n).src.'B'). scanf("%d". towers(n.target. } towers(n-1. } void towers(int n. printf( "\n Move top disk from %c to %c".'A'.temp. return. printf("\n How Many disks: ").target). char src.temp. } 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 .

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

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

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

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

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

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

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

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

sin(x)= 0.907937 39 .

Sign up to vote on this title
UsefulNot useful