Sie sind auf Seite 1von 13

Week-2: Write a C program to calculate the sum: Sum=1-x2/2!+x4/4!-x6/6!+x8/8!-x10/10! AIM: To calculate sum = 1-x2/2!+x4/4!-x6/6!+x8/8!-x10/10!

ALGORITHM: Step 1: Read x Step 2: Initialize n 10, c 2 Step 3: For i is 2 to n by step 2 Begin Step 4: f 1 Step 5: if (c%2=0) Begin Step 6: For j is 1 to i by step 1 Step 7: calculate f = f*j Step 8: calculate sum=sum-pow(x,i)/f End Step 9: else Begin Step 10: For j is 1 to i by step 1 Step 11: calculate f = f*j Step 12: calculate sum=sum+pow(x,i)/f End Step 13: c c+1 End Step 14: Print sum

FLOWCHART:
Start

Read x

n=10 c=2 For i= 2 to n Step 2

f=1

c%2=0

For j is 1 to i Step 1 f=f*j

For j is 1 to i Step 1 f=f*j

Sum=sum-pow(x,i)/f

Sum=sum+pow(x,i)/f

c=c+1

Print sum

Stop

PROGRAM: /* Program purpose is to calculate sum = 1-x2/2!+x4/4!-x6/6!+x8/8!-x10/10! #include<stdio.h> #include<math.h> int main() {int x,i,j,n=10,f=1,c=2; float sum=1; printf("ENTER VALUE FOR x:\n"); scanf("%d",&x); for(i=2;i<=n;i+=2) {f=1; if(c%2==0) { for(j=1;j<=i;j++) {f=f*j; }sum=sum-pow(x,i)/f; } else { for(j=1;j<=i;j++) f=f*j; sum=sum+pow(x,i)/f; } c++; } printf("SUM OF GIVEN SERIES:%f",sum); return 0; }

INPUT:
ENTER VALUE FOR x 5

OUTPUT:
SUM OF GIVEN SERIES:-424.197968

5. Write a C program to find the roots of a quadratic equation. AIM: To find the roots of a quadratic equation. ALGORITHM: Step 1: Read a,b,c Step 2: calculate disc = b*b-4*a*c Step 3: if disc>0 Begin Step 4: root1=(-b+sqrt(disc))/(2*a) Step 5: root2=(-b-sqrt(disc))/(2*a) Step 6: Print Root1 , Root2 End Step 7: else if(disc=0) Begin Step 8: root1=-b/(2*a) Step 9: root2=root1; Goto step 6 End Step 10: else Begin Step 11: disc=-(disc); Step 12: root1=-b/(2*a); Step 13:root2=sqrt(disc)/(2*a); Goto step 6 End

FLOWCHART:
Start

disc = b*b-4*a*c

disc>0

No

disc=0

No

Yes
root1=(-b+sqrt(disc))/(2*a) root2=(-b-sqrt(disc))/(2*a)

Yes
root1=-b/(2*a) root2= root1 disc=-(disc) root1=-b/(2*a) root2=sqrt(disc)/(2*a)

Print root1 Print root2

Start

PROGRAM: /* Program purpose is find the nature and roots of a quadratic equation #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a,b,c,disc; float root1,root2; clrscr(); printf("ENTER VALUES FOR a,b,c:\n"); scanf("%d %d %d",&a,&b,&c); disc=b*b-4*a*c; if(disc>0) { printf("THE ROOTS ARE REAL & THEY ARE..\n"); root1=(-b+sqrt(disc))/(2*a); root2=(-b-sqrt(disc))/(2*a); printf("Root1=%f",root1); printf("Root2=%f",root2); } else if(disc==0) { printf("THE ROOTS ARE EQUAL & THEY ARE..\n"); root1=-b/(2*a); root2=root1; printf("Root1=%f",root1); printf("Root2=%f",root2); } else { printf("THE ROOTS ARE IMAGINARY & THEY ARE..\n"); disc=-(disc); root1=-b/(2*a); root2=sqrt(disc)/(2*a); printf("Root1=%f",root1); printf("Root2=i%f",root2); } getch(); } INPUT: ENTER VALUES FOR a,b,c 1 4 4 OUTPUT:

THE ROOTS ARE EQUAL AND THEY ARE.. Root1=-2 Root2=-2 Exercise Programs 1. Write a C program to find the factorial of a given number 2. Write a C program to find whether given number is perfect or no. 3. Write a C program to print days in a week using switch 4. Write a c program to find factors of a given number. 5. Write a C program to print the even numbers from 1 to n and its count. 6. Write a C program to find the sum of n natural numbers 7. Write a C program to find the sum of following series 1*1+2*2+3*3+4*4.n*n 8. Write a C program to find the sum of following series 1+1/2+1/3+1/4..1/n. 9. Write a c program to print the following pattern 1 1 2 1 2 3 10. Write a c program to print the following pattern 1 2 3 4 5 6 WEEK-4 1.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) and acceleration(m/sec2). Write C program to find the distance traveled at regular intervals of time given the values of u and 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. AIM: To calculate s=u*t+1/2*a*t2 ALGORITHM: Step 1: read p,q,a,u Step 2: for t is p to q by step 1 Step 3: calculate s = u*t+1/2*a*t2 Step 4: Print t,s

FLOWCHART:
Start

Read p,q,a,u

For t is p to q by Step 1 S=u*t+1/2*a*t2

Stop

PROGRAM: /* Program purpose is to calculate s=u*t+1/2*a*t2 */ #include<stdio.h> #include<conio.h> void main() { int u,a,t; float s,p,q; printf("ENTER TIME INTERVALS\n"); scanf("%f %f",&p,&q); printf("ENTER INITIAL VELOCITY AND ACCELERATION:\n"); scanf("%d %d",&u,&a); printf("Time \t Distance\n"); for(t=p;t<=q;t++) { s=(u*t)+(0.5*a*t*t); printf("%d\t %f\n",t,s); } return 0;

INPUT: ENTER TIME INTERVALS: 1.0 5.0 ENTER INITIAL VELOCITY AND ACCELERATION: 80 2.5 OUTPUT: Time Distance 1.0 81.25 2.0 165.00 3.0 251.25 4.0 340.00 5.0 431.25

2. Write a C program which takes two integer operands and one operator from the user, performs the operation and then prints the result.(Consider the operators +,-,*,/,% and use Switch Statement.) AIM: To perform arithmetic operations using switch statement. ALGORITHM: Step 1: Read a,b Step 2: Print Menu Options Step 3: do Begin Step 4: Read ch Step 5: switch(ch) Begin Step 6: case 1: Begin Calculate c = a+b Print c break; End case 2: Begin Calculate c = a-b Print c break; End case 3:

Begin Calculate c = a*b Print c break; End case 4: Begin Calculate c = a/b Print c break; End case 5: Begin Calculate c = a%b Print c break; End default:Print Invalid choice End FLOWCHART:
Start

Read a,b,ch

switch(ch)

c=a+b

c=a-b

c=a*b

c=a/b

c=a%b

Print c

Stop

PROGRAM: /* Program purpose is to perform arithmetic operations using switch statement.*/ #include<stdio.h> #include<conio.h> int main() { int a,b,c,ch; printf("ENTER TWO VALUES FOR a & b\n"); scanf("%d %d",&a,&b); printf("MENU OPTIONS \n"); printf("************\n"); printf("1.Addition\n"); printf("2.Subtraction\n"); printf("3.Multiplication\n"); printf("4.Division\n"); printf("5.Modulus\n"); printf("\n"); do { printf("ENTER UR CHOICE\n"); scanf("%d",&ch); switch(ch) { case 1: { c=a+b; printf("The addition of %d and %d is..%d\n",a,b,c); break; } case 2: { c=a-b; printf("The subtraction of %d and %d is..%d\n",a,b,c); break; } case 3: { c=a*b; printf("The multiplication of %d and %d is..%d\n",a,b,c); break; } case 4:

{ c=a/b; printf("The division of %d and %d is..%d\n",a,b,c); break; } case 5: { c=a%b; printf("The modulus of %d and %d is..%d\n",a,b,c); break; } default:printf("INVALID CHOICE\n"); } }while(ch<=5); return 0; }

INPUT: ENTER TWO VALUES FOR a & b: 20 16 OUTPUT: MENU OPTIONS *************** 1.Addition 2.Subtraction 3.Multiplication 4.Division 5.Modulus ENTER UR CHOICE 1 The addition of 20 and 16 is..36 ENTER UR CHOICE 2 The subtraction of 20 and 16 is..4 ENTER UR CHOICE 3 The multiplication of 20 and 16 is..320 ENTER UR CHOICE 4 The division of 20 and 16 is..1 ENTER UR CHOICE

6 INVALID CHOICE. Exercise Programs 1. Write a C program to find whether given number is Strong number or not 2. Write a C program to find the frequency of individual digits in a given number 3. Write a c program to print the following pattern 2 4 6 8 10 12 4. Write a C program to find whether given number is positive or negative or zero 5. Write a C program to find the sum of squares of n numbers 6. Write a C program to find the largest among three numbers

NOTE: For Exercise programs Question AIM Program /* program purpose..*/ code

Das könnte Ihnen auch gefallen