0% found this document useful (0 votes)
63 views

Index: S.N O. Experiment Pag Eno. Dat e Rem Arks

This document contains code snippets and explanations for 12 programming problems in C language. The problems include: 1) Finding roots of a quadratic equation 2) Raising a number to a power 3) Generating a histogram 4) Adding digits of a number 5) Checking if a number is prime 6) Printing a number in reverse order 7) Finding greatest common divisor of two numbers 8) Generating Fibonacci series 9) Reversing an array 10) Calculating factorial of a number 11) Adding terms of a series 12) Performing linear search For each problem, the code, flowchart and algorithm are provided.

Uploaded by

Mohd Shahid
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Index: S.N O. Experiment Pag Eno. Dat e Rem Arks

This document contains code snippets and explanations for 12 programming problems in C language. The problems include: 1) Finding roots of a quadratic equation 2) Raising a number to a power 3) Generating a histogram 4) Adding digits of a number 5) Checking if a number is prime 6) Printing a number in reverse order 7) Finding greatest common divisor of two numbers 8) Generating Fibonacci series 9) Reversing an array 10) Calculating factorial of a number 11) Adding terms of a series 12) Performing linear search For each problem, the code, flowchart and algorithm are provided.

Uploaded by

Mohd Shahid
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 33

1

INDEX
S.n Experiment o. Roots of quadratic 1. equation. Pag Dat Rem eno. e arks 2-3

2. 3. 4. 5. 6. 7. 8. 9. 10 . 11 .

Raise x to the power n. 4 - 5 Generating Histogram. 6 - 8 911 Whether a no. is prime 12 or not. 14 Print no. in reverse 15 order. 16 Find G.C.D. of given 17 no. 18 19 Fibonacci series. 21 22 Reversing an array. 23 Add digits of a no. Factorial. Add series 1+x+x2+x3+..+xn 24 25 26 27

12 .

Linear search.

28 29

PRACTICAL - 1
WAP to find roots of quadratic equation

#include<stdio.h> #include<conio.h> #include<math.h> void main() { clrscr(); float a,b,c,r1,r2; printf("Enter the value of a.\n"); scanf("%f",&a); printf("Enter the value of b.\n"); scanf("%f",&b); printf("Enter the value of c.\n"); scanf("%f",&c); r1=(-b+sqrt((b*b)-(4*a*c)))/(2*a); r2=(-b-sqrt((b*b)-(4*a*c)))/(2*a); printf("The required roots are %f & %f .",r1,r2); getch(); } Enter the value of a. 1 Enter the value of b. 5 Enter the value of c. 6 The square root are -2, -3

Flowchart
Start A,b,c,r1,r 2

R1=(b+sqrt((b*b)(4*a*c)))/(2*a)

Print value of R1

R2=(-bsqrt((b*b)(4*a*c)))/(2*a)

Print value of R2

Stop

Algorithm
Step1:Step2:Step3:Step4:Step5:Step6:Start. Initialize a,b,c,r1,r2. Calculate R1=(-b+sqrt((b*b)-(4*a*c)))/(2*a)R2=(-b-sqrt((b*b)(4*a*c)))/(2*a) Assign the result in R1 and R2. Print the value of R1 and R2. Stop

PRACTICAL - 2
WAP to raise X to power N

#include<stdio.h> #include<conio.h> void main() { clrscr(); int x,n,i,a=1; printf("Enter the base: "); scanf("%d",& x); printf("Enter the power: "); scanf("%d",& n); for(i=1;i<=n;i++) a=a*x; printf("The result is %d",& a); getch(); }
Enter the base:Enter the power:The result is:25 5 2

Flowchart
Start

x,n,i,a=1

If I< =1 Pow=pow* x Display power I=I+1 Stop

Algorithm
Step1:Step2:Step3:Step4:Step5:Step6:Step7:Start. Initialize x,n,i,a=1. if (If I < =) then continue otherwise go to step 7. pow=pow*x i=i+1 repeat step 4 display pow

PRACTICAL - 3
WAP for generating Histogram

#include<stdio.h> #include<conio.h> void main() { clrscr(); int I,j; for(int i=9;i>=1;i--) { for(int j=i;j<9;j++) printf(" "); for(j=i;j>=1;j--) printf("%d",j); printf("\n"); } getch(); } 987654321 87654321 7654321 654321 54321 4321 321
21 1

Flowchart
Start

i,j

I=9

If i=>1

J=i

J=i

If j<=i J=i

Print j

Stop

10

Algorithm
Step1:- Start. Step2:- Initialize i,j. Step3:- check the condition of I is less then 9 continue otherwise go to step 1. Step4:- print space . Step5:- check the condition of j is greater then or equal to 1 continue otherwise go to step 12. Step6:- print the value of j and entered new line. Step7:- decrement of j and go to step 8 Step8:- increment of j and go to step 5 Step9:- increment of i and go to step 3 Step10:- Stop.

PRACTICAL - 4
WAP to add digits of a number

11

#include<stdio.h> #include<conio.h> void main() { clrscr(); int sum=0,c,d,a; printf("Enter the number"); scanf("%d",&a); do { c=a/10; d=a%10; sum=sum+d; a=c; } while(c!=0); printf("The sum is: %d",sum); getch(); }
Enter the no. 456 The sum is:- 15

12

Flowchart
Start

sum=0,c,d,a

yes If n = 0 no Print value of sum Rem = n%10

Display rem

Sum=sum=rem N=n/10

Stop

13

Algorithm
Step1:Step2:Step3:Step4:Start. Initialize sum=0,c,d,a if (n!=0) then continue otherwise go to step 5 d=a%10, sum=sum+d c=a/10 repeat step 3 Print a. Print sum. Stop

Step5:Step6:Step7:Step8:-

PRACTICAL - 5

14

WAP to check whether a number is prime

#include<stdio.h> #include<conio.h> #include<math.h> void main() { clrscr(); int n,i,a,count=0; printf("Enter the number: \n"); scanf("%d",&n); for(i=1;i<=n;i++) { if(n%i==0) { count++; } if(count==3) { printf("%d is not a prime number.",n); break; } } if(count==1) { printf("%d is not a prime number.",n); } if(count==2) { printf("%d is a prime number.",n); } getch();
Enter the no. 13 13 is a prime no. Or Enter the no. 10 10 is not a prime no.

Flowchart
Start

15

sum=0,c,d,a

While n > 0

I=n%10 Sum=sum+i*i*i N=n/10

Print value of sum

If sum = = n Print Print not

Stop

16

Algorithm

Step1:Step2:Step3:Step4:Step6:Step7:Step8:Step9:-

Start. Initialize n,i,a,count=0 compute m=square root of n then add 1 check the condition x = m then continue otherwise goto step 9. then break print message n is a prime no. otherwise print message n is not a prime no. Stop

17

PRACTICAL - 6
WAP to print a given number in reverse order

#include<stdio.h> #include<conio.h> void main() { clrscr(); int c,d,a; printf("Enter the number"); scanf("%d",&a); do { c=a/10; d=a%10; printf("%d",d); a=c; } while(c!=0); getch(); }
Enter a no. 654 Reverse is 456

18

Flowchart
Start

c,d,a

c=a/10; d=a%10; a=c; while(c! =0);

Print the reverse order

Stop

Algorithm
Step1:Step2:Step3:Start. Initialize c,d,a do { c=a/10; d=a%10; printf("%d",d); a=c; } while(c!=0); Print the reverse order. Stop

Step4:Step5:-

PRACTICAL - 7
WAP for finding GCD of two numbers

19

#include<stdio.h> #include<conio.h> void main() { clrscr(); int a,b,gcd,i,n; printf("Enter the first number.\n"); scanf("%d",&a); printf("Enter the second number.\n"); scanf("\n%d",&b); if(a>b) { n=b; } else if(a<b) { n=a; } else { i=0; gcd=a; } for(i=1;i<=n;i++) { if((a%i)==0 && (b%i)==0) { gcd=i; } } printf("gcd = %d",gcd); getch(); } Enter the first number 25 Enter the first number 75 The G.C.D is 25456

Flowchart
Start

20

a,b,gcd,i,n

If a ! = b yes If a > b Print value of gcd B=b-a Stop A=a-b no

Algorithm
Step1:Step2:Step3:Step4:Step4:Step5:Step6:Start. Initialize a,b,gcd,i,n while a!= sub of b then continue otherwise go to step 5 if (a > b) compute a = sub of b and a Print the value of gcd. Stop

21

PRACTICAL 8
WAP to generate Fibonacci series

#include<stdio.h> #include<conio.h> void main() { clrscr(); int fib[25],I,n; printf(Enter the number of terms: ); scanf(%d,&n ); fib[0]=0; fib[1]=1; for(i=2;i<n;i++) { fib[i]=fib[i-1]+fib[i-2]; } printf(The required fibonacci series is:\n); for(i=0;i<n;i++) { printf(%d\t,fib[i]); } getch(); } Enter the no. of terms 8 The required fibonacci series is: 0 1 1 2 3 5 8 13

Flowchart
Start

22

fib[25],i,n

Compute a = 0, H = 1, I = 3

If I < = n

Compute c = a + b

print c

Compute a = b

Assign b = c

Initialize I = I + 1

Stop

23

Algorithm
Step1:Step2:Step3:Step4:Step5:Step6:Step7:Step8:Start. Initialize fib[25],i,n check the condition I < = n then continue otherwise got to step 8 compute c = add of a and b Print c assign a = b and b = c increase I go to step 3 stop

WAP for reversing an array

PRACTICAL - 9

24

#include<stdio.h> #include<conio.h> void main() { clrscr(); int a[10], b[10],i,j=0; printf("Enter the elements in array.\n"); for(i=0;i<10;i++) { printf("Enter element no. %d\t",i+1); scanf("%d",&a[i]); } for(i=9;i>=0;i--) { b[j]=a[i]; j++; } printf("\nThe array you entered is: \n\n"); for(i=0;i<10;i++) { printf("%d\t",a[i]); } printf("\nThe aray after reversal is: \n\n"); for(i=0;i<10;i++) { printf("%d\t",b[i]); } getch(); }

Enter the elements in Array


Enter the elements in array 1 Enter the elements in array 2 Enter the elements in array 3 Enter the elements in array 4 Enter the elements in array 5 Enter the elements in array 6 Enter the elements in array 7 Enter the elements in array 8 Enter the elements in array 9 Enter the elements in array 10 The array you entered is 5 1 2 8 9
7

6 2 3 5

5 5

1 3

2 2

8 6

9 7

7 9

6 8

2 2

3 1

5 5

The aray after reversal is:

Flowchart
Start

25

i\p str

Compute n=string

Initialize I = n - 1

If I>=0

Print value of str[i]

Decrease i

Stop

Algorithm
Step1:Step2:Step3:Step4:Step5:Step6:Start. Initialize a[10], b[10],i,j=0 check the condition I is > = 0 then continue otherwise goto step 6 display str [i] decrease I go to step 3. Stop

PRACTICAL - 10
WAP to calculate factorial of a number

26

#include<stdio.h> #include<conio.h> void main() { clrscr(); int x,i,fact=1; printf("Enter the number.\n"); scanf("%d",&x); if(x==0) { printf("Enter the number. 1.\n"); } else { for(i=x;i>=1;i--) { fact=fact*i; } printf("The factorial is %d",fact); } getch(); }
Enter the number. The factorial is 5 120

Flowchart
Start

27

x,i,fact=1

If x = 1 Return (1) Compute F=x*rec(x-1)

Return (f)

Stop

Algorithm
Step1:Step2:Step3:Step4:Step5:Start. Initialize x,i,fact=1 print the value of fact calling the recursion stop.

PRACTICAL - 11
WAP to add a series 1+x+x2+x3.+xn

28

#include<stdio.h> #include<conio.h> void main() { clrscr(); int x,i,n,sum=1,mul=1; printf("Enter the number: "); scanf("%d",&x); printf("Enter the degree: "); scanf("%d",&n); for(i=0;i<=n;i++) { mul=mul*x; sum=sum+mul; } printf("The sum upto n terms is %d",sum); getch(); }
Enter the number: Enter the degree: The sum upto n terms is 2 2 7

Flowchart
Start

29

x,i,n,sum=1,mul=1

ifi=0;i<=n; i+ +mul=mul*
x; sum=sum+ mul;

Print value of sum Stop

Algorithm
Step1:Step2:Step3:Step4:Step5:Step6:Start. Initialize x,i,n,sum=1,mul=1 check if(i=0;i<=n;i++) Assign the result in sum. Print the value of sum. Stop

PRACTICAL - 12
WAP for linear search

30

#include<stdio.h> #include<conio.h> void main() { clrscr(); int a[10]={10,20,30,40,50,60,70,80,90,100},n,i; printf("Enter the number to be searched.\n"); scanf("%d",&n); for(i=0;i<10;i++) { if(a[i]==n) { printf("The number found at index %d",i); break; } } if(i==10) { printf("Number not found"); } getch(); }
Enter the number to be searched. Number not found at index Enter the number to be searched. The number found at index 5 10 0

Flowchart
Start

31

a[10]={10,20,30,40,50 ,60,70,80,90,100},n,i yes

ifa[i]==n
no

if(i==10)

Print true block

Print false block

Stop

Algorithm
Step1:Step2:Step3:Step4:Step5:Step6:Start. Initialize a[10]={10,20,30,40,50,60,70,80,90,100},n,i check ifa[i]==n print true block otherwise to continue to step 6 check if(i==10) print true block otherwise to continue to step 6 Print the value of a. Stop

32

33

C Practical File

You might also like