+ All Categories
Home > Documents > C manual vivek

C manual vivek

Date post: 24-Nov-2014
Category:
Upload: vivekeng537
View: 109 times
Download: 4 times
Share this document with a friend
108
Week-1(a) Conversion of temperature from Celsius to Fahrenheit Algorithm Step-1 : Start Step-2 : Declare variables C,f Step-3 : Read Input value for celsius temperature Step-4 : Compute Farenheit temperature using f = (C*(9/5))+32 Step-5 : Display farenheit temperature Step-6 : Stop Flowchart 1
Transcript
Page 1: C manual vivek

Week-1(a) Conversion of temperature from Celsius to Fahrenheit

Algorithm

Step-1 : StartStep-2 : Declare variables C,fStep-3 : Read Input value for celsius temperatureStep-4 : Compute Farenheit temperature using f = (C*(9/5))+32Step-5 : Display farenheit temperatureStep-6 : Stop

Flowchart

Start

Declare variables c and f

Read input value C

Compute f = (C*(9/5))+32

Dsiplay Fahrenheit temperature

Stop

1

Page 2: C manual vivek

Program 1(a)

#include<stdio.h>#include<conio.h>void main(){ float c,f; clrscr(); printf("Enter the temperature in celsius:"); scanf("%f",&c); f=c*9/5+32; printf("Temperature in Fahrenheit=%.2f",f); getch();}

2

Page 3: C manual vivek

Week-1(b) Student GradingAlgorithmStep-1 : StartStep-2 : Declare variables marks1,marks2,marks3,total,average;Step-3 : Read maths,physics,chemistry;Step-4 : Compute total=marks1+marks2+marks3;Step-5 : Compute average=total/3;Step-6 : Find grading based on average

6.1 : if(maths<35) Display Student failed

6.2 : else if(physics<35)Display student failed

6.3 : else if(chemistry<35)Display student failed

6.4 : else if(average>75)Display grade is A+

6.5 : else if(average>70)Display grade is A

6.6 : else if(average>60)Display grade is B

6.7 : else if(average<60)Display grade is C

Step-7 : StopFlowchart

Declare variables Maths,physics,chemistry

Read maths, physics,chemistry

Total=maths+physics+chemistry

Start

3

Page 4: C manual vivek

Average=total/3

If(maths<35

If(physics<35)

If(chemistry<35)

Student failed

If(average>75)

If(average>70)

If(average>60)

Grade is A+

Grade is A

Grade is B

4

Page 5: C manual vivek

Program 1(b)

#include<stdio.h>#include<conio.h>void main(){ int m,p,c,sum; float avg; clrscr(); x: printf("Enter the marks Maths Physics Chemistry:"); scanf("%d%d%d",&m,&p,&c); if(m<0||m>100||p<0||p>100||c<0||c>100) goto x; sum=m+p+c; avg=(float)sum/3;printf("Maths=%d\nPhysics=%d\nChemistry=%d\nSum=%d\nAverage=%.2f\n",m,p,c,sum,avg);m<35||p<35||c<35?printf("Fail"):avg>75?printf("Grade=A+"):avg>70?printf("Grade=A"): avg>60?printf("Grade=B"):printf("Grade=C"); getch();}

Week-1(c) Income Tax

If(average<60)

Grade is C

stop

5

Page 6: C manual vivek

Algorithm

Step-1 : StartStep-2 : Declare variables salary,tax,ITStep-3 : Read Input value salaryStep-4 : Calculate tax

4.1. : if(salary>500000) tax is 10%

4.2. : if(salary>300000) tax is 7%

4.3. : if(salary>100000) tax is 5%

4.4. : if(salary>50000) tax is 3%

4.5. : if(salary<50000) tax is 0%

Step-5 : Calculate IT=Salary*tax/100Step-6 : Display Income taxStep-7 : Stop

Flowchart

Start

Declare variables salary,tax,IT

Read Input value salary

if(salary>500000)

Tax is 10%

6

Page 7: C manual vivek

Program 1(c)

if(salary>300000)

if(salary>100000)

if(salary>50000)

Tax is 7%

Tax is 5%

Tax is 3%

Tax is 0%

Calculate IT=Salary*tax/100

Display Income tax

Stop

7

Page 8: C manual vivek

#include<stdio.h>#include<conio.h>void main(){ float inc,t,sal; clrscr(); printf("Enter the salary:"); scanf("%f",&sal); t=sal>500000?10:sal>300000?7:sal>100000?5:sal>50000?3:0; inc=sal*t/100; printf("Tax=%.2f\nIncome tax=%.2f",t,inc); getch();}

Week-2 2’s complement of a binary number

8

Page 9: C manual vivek

AlgorithmStep-1 : StartStep-2 : Declare variables bin,flStep-3 : Initialize fl=0Step-4 : Read the binary numberStep-5 : Initialise i=0Step-6 : if(i<strlen(bin)) 6.1 : if(bin[i]!=0&&bin[i]1=1) 6.2 : goto Step-4 6.3 : Increment value of i 6.4 : Go to Step-6Step-7 : Initialise i=strlen(bin)-1Step-8 : if(i>=0) 8.1 : if((fl==0)&&bin[i]= =’1’) 8.1.1 : fl=1 8.2 : if(fl= =1) 8.2.1 : if(bin[i]= =’0’) 8.2.1.1 : bin[i]=’1’ 8.2.2 : if(bin[i]= =’1’) 8.2.2.1 : bin[i]=’0’Step-9 : Decrement value of i 9.1 : Go to Step-7Step-10 : Display the resultStep-11 : Stop

9

Page 10: C manual vivek

Flowchart

Start

Declare variables bin,fl

Read the binary number

Initialize fl=0

Initialise i=0

if(i<strlen(bin))

if(bin[i]!=0&&bin[i]1=1)

Initialise i=strlen(bin)-1

Increment value of i

10

Page 11: C manual vivek

if(i>=0)

if((fl==0)&&bin[i]= =’1’)

if(bin[i]= =’0’)

if(bin[i]= =’1’)

Decrement value of i

Display the result

bin[i]=’1’

bin[i]=’0’

fl=1

if (f1==1)

Stop

11

Page 12: C manual vivek

Program 2

#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char bin[50]; int i,n,flag=0; clrscr(); a: printf("Enter the binary number:"); gets(bin); n=strlen(bin); for(i=0;i<n;i++) { if(bin[i]!='0'&&bin[i]!='1') { printf("Binary number must consists of only 0's and 1's\n"); goto a; } } for(i=n-1;i>=0;i--) { if(flag==0 && bin[i]=='1') { flag=1; continue; } if(flag==1) { if(bin[i]=='0') bin[i]='1'; else if(bin[i]=='1') bin[i]='0'; } } printf("2's compliment of the given binary number=%s",bin); getch();}

12

Page 13: C manual vivek

Week-3(a) Sum of individual digits of a positive integerAlgorithm

Step-1 : StartStep-2 : Declare variables sum, numberStep-3 : Initialize sum=0 and read the numberStep-4 : perform sum of individual digits

4.1 : compute sum=sum+number%104.2 : compute number=number/104.3 : if(number!=0) 4.3.1: Go to step 4.1

Step-5 : Display sumStep-6 : StopFlowchart

Start

Declare variables sum, number

Initialize sum=0 and read the number

compute sum=sum+number%10

compute number=number/10

if(number!=0)

Stop

Display sum

13

Page 14: C manual vivek

Program 3(a)

#include<stdio.h>#include<conio.h>void main(){ int n,sum=0,m; clrscr(); printf("Enter the number:"); scanf("%d",&n); m=n; while(n>0) { sum=sum+n%10; n=n/10; } printf("Sum of digits=%d",sum); getch();}

14

Page 15: C manual vivek

Week-3(b) Generate first n terms of the Fibonacci sequenceAlgorithmStep-1 : StartStep-2 : Declare variables f,f1,f2,i,nStep-3 : Initialize f1=0,f2=1 Step-4 : Read the value of nStep-5 : perform Fibonacci seriesStep-6 : Display values of f1 and f2

6.1 : Initialise i=16.2 : if(i<=n-2)

6.2.1 : f=f1+f2 6.2.2 : Display value of f 6.2.3 : f1=f2 6.2.4 : f2=f 6.2.5 : Increment value of i 6.2.6 : Go to Step-5.2Step-7 : Stop

15

Page 16: C manual vivek

Flowchart

Declare variables f,f1,f2,i,n

Initialize f1=0,f2=1

Start

Read the value of n

if(i<=n-2)

Initialise i=1

f=f1+f2

Display value of f

f1=f2, f2=f

Increment value of i

16

Stop

Page 17: C manual vivek

Program 3(b)

#include<stdio.h>#include<conio.h>void main(){int n,i,t1=0,t2=1,t;clrscr();printf("Enter the number :");scanf("%d",&n);printf("Fibonacci series :");printf("%d\t%d",t1,t2);for(i=1;i<=n-2;i++){t=t1+t2;printf("\t%d",t);t1=t2;t2=t;}getch();}

17

Page 18: C manual vivek

Week-3(c) Generate prime numbers between 1 and nAlgorithmStep-1 : StartStep-2 : Declare variables i,j,n,c;Step-3 : Initialize c=0 and read the numberStep-4 : perform generation of prime numbers 4.1 : Initialise i=2 4.2 : if(i<=n) 4.2.1 : initialize j=1; 4.2.2 : if(j<=i) 4.2.2.1 : if(i%j= =0) 4.2.2.1.1 : Increment the value of c 4.2.3 : Increment value of j to 1 4.2.4 : go to step 4.2.2 4.3 : if(c==2) 4.3.1 : Display value of i 4.3.2 : Initialise c=0; 4.4 : Increment value of i to 1 4.5 : Go to step 4.2Step-5 : StopFlow chart

Start

Declare variables i,j,n,c;

Initialize c=0 and read the number

Initialise i=2

if(i<=n)

18

Page 19: C manual vivek

initialize j=1;

if(j<=i)

if(i%j= =0)

Increment value of j to 1

if(c==2)

Display value of i

Increment value of i to 1

Stop

Initialize C=0

19

Increment the value of c

Page 20: C manual vivek

Program 3(c)

#include<stdio.h>#include<conio.h>void main(){ int n,i,j,c=0; clrscr(); printf("Enter the number upto which prime number should be printed:"); scanf("%d",&n); printf("Prime numbers \n"); for(i=2;i<=n;i++) { for(j=1;j<=i;j++) { if(i%j==0) c++; } if(c==2) printf("%6d",i); c=0; } getch();}

20

Page 21: C manual vivek

Week-3(d) Check a given integer is Fibonacci or notAlgorithmStep-1 : StartStep-2 : Declare variables f,f1,f2,c,nStep-3 : Initialize f=1,f1=0,f2=1,c=0 Step-4 : Read the value of nStep-5 : if(f<=n) 5.1 : f=f1+f2 5.2 : f1=f2 5.3 : f2=f 5.4 : if(f==n) 5.4.1 : Display given number is Fibonacci 5.4.2 : c=1 5.5 : Go to step-5Step-6 : if(c==0) 6.1 : Display given number is not FibonacciStep-7 : Stop

21

Page 22: C manual vivek

Flowchart

Start

Declare variables f,f1,f2,c,n

Initialize f=1,f1=0, f2=1 ,c=0

Read the value of n

if(f<=n)

f=f1+f2, f1=f2, f2=f

if(f==n)

Display given number is Fibonacci c=1

if(c==0)

Display given number is not Fibonacci

22

Stop

Page 23: C manual vivek

Program 3(d)

#include<stdio.h>#include<conio.h>void main(){int n,i,t1=0,t2=1,t=0,c=0;clrscr();printf("Enter the number :");scanf("%d",&n);printf("Fibonacci series :");printf("%d\t%d",t1,t2);while(t<=n){t=t1+t2;printf("\t%d",t);if(t==n) { c=1; break; }t1=t2;t2=t;}if(c==0) printf("\nNumber is not fibonacci");else printf("\nNumber is fibonacci"); getch();}

23

Page 24: C manual vivek

Week-4(a) Sum of series 1-x2/2!+x4/4!-x6/6!+x8/8!-x10/10!

AlgorithmStep-1 : StartStep-2 : Declare variables x,i,j,f,sumStep-3 : Initialise sum=1,f=1Step-4 : Read value of xStep-5 : Initialize i=2 5.1 : if(i<=10) 5.1.1 : initialize f=1 5.1.2 : initialize j=1 5.1.2.1 : if(j<=i) 5.1.2.1.1 : f=f*j 5.1.2.1.2 : Increment value of j 5.1.2.1.3 : Go to Step-5.1.2.1 5.1.2.1.4 : if(i%4= =0) 5.1.2.1.4.1: sum=sum+pow(x,i)/f 5.1.2.1.5 : if(i%4!=0) 5.1.2.1.5.1: sum=sum-pow(x,i)/f 5.1.3 : increment value of i 5.1.4 : Go to Step-5.1Step-6 : Stop

Flow chart:

Declare variables x,I,j,f,sum

Read the value of x

Initialize i=2

Initialize sum=1, f=1

Start

24

Page 25: C manual vivek

if i<=10 then

if j<=i then

f=f*j,j=j+1

if i%4==0

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

if i%4!=0

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

Print sum

i=i+1,f=1

Y

N

N

Y

N

Y

N

Y

Stop

25

Page 26: C manual vivek

Program 4(a)

#include<stdio.h>#include<conio.h>#include<math.h>void main(){ int x,i,j; long f=1; double sum=1; clrscr(); printf("Enter value of x:"); scanf("%d",&x); for(i=2;i<=10;i+=2) { for(j=1;j<=i;j++) f*=j; if(i%4==0) sum=sum+(pow(x,i)/f); else sum=sum-(pow(x,i)/f); f=1; } printf("1-%d/2!+%d/4!-%d/6!+%d/8!-%d/10!=%.3lf",x,x,x,x,x,sum); getch();}

26

Page 27: C manual vivek

Week-4(b) Roots of Quadratic equationAlgorithmStep-1 : StartStep-2 : Declare variables a,b,c,d,x1,x2;Step-3 : Read values of a,b,cStep-4 : compute discriminant=((b*b)-(4*a*c))Step-5 : Find Roots of quadratic equation

5.1 : if(d<0) Display roots are imaginary

5.2 : if(d=0) Display Roots are equal

5.3 : if(d>0) compute x1=-b+sqrt(d)/2*a compute x2=-b-sqrt(d)/2*a Display values of x1 and x2

Step-6 : StopFlowchart

Start

Declare variables a,b,c,d,x1,x2

Read values of a,b,c

Compute discriminant =((b*b)-(4*a*c))

Display roots are imaginary

If(d<0)

27

Page 28: C manual vivek

If(d=0)

If(d>0)

stop

Display roots are equal

X1=-b+sqrt(d)/2*aX2=-b-sqrt(d)/2*aDisplay x1 and x2

28

Page 29: C manual vivek

Program 4(b)

#include<stdio.h>#include<conio.h>#include<math.h>void main(){ int a,b,c,t; float r1,r2; //clrscr(); printf("Enter the coefficients a,b,c:"); scanf("%d%d%d",&a,&b,&c); t=pow(b,2)-4*a*c; if(t<0) printf("Roots are (-%d+sqrt(%d)i)/%d and (-%d-sqrt(%d)i)/%d",b,-t,2*a,b,-t,2*a); else { r1=(-b+sqrt(t))/(2*a); r2=(-b-sqrt(t))/(2*a); printf("Roots of quadratic equation are %.2f %.2f",r1,r2); } getch();}

29

Page 30: C manual vivek

Week -5 a) Distance Travelled in Time interval using ut+0.5at2

Algorithm

Step 1: StartStep 2: Declare u, a, t, sStep 3: Initialize t=10Step 4: Read u,aStep 5: if(t<=60) 5.1: Calculate s=ut+1/2at2

5.2: Print s 5.3: t=t+10, goto step 5Step 6: Stop

Flow Chart

30

Page 31: C manual vivek

Program 5(a)

#include<stdio.h>#include<conio.h>void main(){ int u,t,a; float s; clrscr(); printf("Enter the initial velocity :"); scanf("%d",&u); printf("Enter the acceleration :"); scanf("%d",&a); for(t=10;t<=60;t+=10) { s=(float)u*t+0.5*a*t*t;

Start

Declare u,a,t,s

Read u,a

Initialize t=10

if t<=60

s=(u*t)+(1/2 *a *t * t)

t=t+10

Print s

Stop

31

Page 32: C manual vivek

printf("Distance travelled in %d seconds=%.2f\n",t,s); } getch();}

Week-5(b) Arithmetic Operations

Algorithm

Step-1 : StartStep-2 : Declare variables a,b,c,choiceStep-3 : If choice is trueStep-4 : Read Input values a and bStep-5 : Read choiceStep-6 : perform arithmetic operations

6.1 : if choice = ‘+’ perform addition and display result

6.2 : if choice= ‘-‘ perform Subtraction and display result

6.3 : if choice= ‘*’ perform multiplication and display result

32

Page 33: C manual vivek

6.4 : if choice= ‘/’ perform Division and display result

6.5 : if choice = ‘%’ perform Modulus and display result

6.6 : Display wrong choiceStep-7 : Go to step-3Step-8 : Stop

Flow Chart

Start

Declare variables a,b,c,choice

Read Input values a and b

Read choice

33

Page 34: C manual vivek

Program 5(b)

if choice=’+’

if choice=’-‘

if choice=’/’

if choice =’%’

if choice=’*’

Stop

perform addition and display result

perform Subtraction and display result

perform multiplicati on and display result

perform Division and display result

perform Modulus and display result

34

Display wrong choice

Page 35: C manual vivek

#include<stdio.h>#include<conio.h>void main(){ int a,b; char ch; clrscr(); printf("Enter the values of a,b :"); scanf("%d%d",&a,&b); printf("Enter choice:"); fflush(stdin); scanf("%c",&ch); switch(ch) { case '+': printf("Addition=%d",a+b); break; case '-': printf("Subraction=%d",a-b); break; case '*': printf("Multiplication=%ld",a*b); break; case '/': printf("Division=%.2f",(float)a/b); break; case '%': printf("Modulus=%d",a%b); break; default: printf("Invalid option"); } getch();}

Week-6(a)Addition of two matrices

35

Page 36: C manual vivek

AlgorithmStep-1 : StartStep-2 : Declare variables row1,column1,row2,column2,a,b,c,i,jStep-3 : Read rows and columns of first matrixStep-4 : Read values of first matrixStep-5 : Print values of first matrixStep-6 : Read rows and columns of second matrixStep-7 : Read values of second matrixStep-8 : Print values of second matrixStep-9 : if((row1= =row2)&&(column1= =column2)) 9.1 : loop from 0 to row1 9.1.1 : loop from 0 to column1 9.1.1.1 : Calculate addition of corresponding values of two matrices 9.1.1.2 : Print corresponding resultant values of matrixStep-10 : StopFlowchart

Start

Declare variables row1,column1,row2,column2,a,b,c,i,j

Read rows and columns of first matrix

Read values of first matrix

Print values of first matrix

36

Page 37: C manual vivek

Read rows and columns of second matrix

Read and Print values of second matrix

if((row1= =row2)&&(column1= = column2))

Initialize I to 0

If(i<row1)

Initialise j to 0

If(j<column1)

Matrix addition is not possible

37

Page 38: C manual vivek

Program 6(a)

#include<stdio.h>#include<conio.h>void main(){ int a[5][5],b[5][5],i,j; clrscr(); printf("Enter First matrix (2x2):"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&a[i][j]); printf("Enter Second matrix (2x2):"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&b[i][j]); printf("Addition of two matrices:\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) printf("%6d",a[i][j]+b[i][j]); printf("\n"); } getch();}

Week-6(b)Transpose of a matrix in-place mannerAlgorithm

C[i][j]=a[i][j]+b[i][j]

Increment value of j

Increment value of i

Print the matrix c[i][j]

Stop

38

Page 39: C manual vivek

Step-1 : StartStep-2 : Declare variables row,column,a,i,j.tStep-3 : Read rows and columns of matrixStep-4 : Read values of matrixStep-5 : Print values of first matrixStep-6 : initialize i to 0 6.1 : if(i<row) 6.1.1 : Initialise j to 0 6.1.2 : if(j<column) 6.1.2.1 : if(i<j) 6.1.2.1.1: t=a[i][j] 6.1.2.1.2: a[i][j]=a[j][i] 6.1.2..1.3: a[j][i]=t 6.1.2.2 : Transpose is not possible 6.1.3 : Increment value of j 6.1.4 : Go to step-6.1.2 6.2 : Increment value of i 6.3 : Go to step6.1Step-7 : Display transpose of the matrixStep-8 : Stop

Flowchart

Start

Declare variables row,column,a,i,j.t

Read rows and columns of matrix

Read values of matrix

Print values of first matrix

39

Page 40: C manual vivek

Initialize i to 0

if(i<row)

Initialise j to 0

if(j<column)

if(i<j)

Transpose is not possible

Increment value of j

Increment value of i

Stop

t=a[i][j] a[i][j]=a[j][i] a[j][i]=t

Display transpose of the matrix

40

Page 41: C manual vivek

Program 6(b)

#include<stdio.h>#include<conio.h>void main(){ int a[5][5],t,i,j; clrscr(); printf("Enter matrix (2x2):"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&a[i][j]); printf("Transpose of matrix:\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { if(i<j) { t=a[i][j]; a[i][j]=a[j][i]; a[j][i]=t; } printf("%-6d",a[i][j]); } printf("\n"); } getch();}

41

Page 42: C manual vivek

Week-6(c) Matrix multiplication by checking compatibilityAlgorithm

Step-1 : StartStep-2 : Declare variables row1,column1,row2,column2,a,b,c,i,j,kStep-3 : Read rows and columns of first matrixStep-4 : Read values of first matrixStep-5 : Print values of first matrixStep-6 : Read rows and columns of second matrixStep-7 : Read values of second matrixStep-8 : Print values of second matrixStep-9.0 : if(column1= =row2) 9.1 : Initialise i to 0 9.2 : if(i<row1) 9.2.1 : Initialise j to 0 9.2.2 : if(j<column1) 9.2.2.1 : c[i][j]=0 9.2.2.2 : initialize k to 0 9.2.2.2.1 : if(k<row2) 9.2.2.2.1.1 : c[i][j]=c[i][j]+a[i][k]*b[k][j]

9.2.2.2.1.2 : Increment k by 1 9.2.2.2.1.3 : Go to step-9.2.2.2.1 9.2.2.3 : increment j by 1 9.2.2.3 : Go to step-9.2.2 9.3 : Increment value of i 9.4 : Go to Step-9.2Step-9.1 : Display matrix multiplication is not possibleStep-10 : Print Resultant matrix CStep-11 : Stop

42

Page 43: C manual vivek

Flowchart

Print values of first matrix

Start

Declare variables row1,column1,row2,column2,a,b,c,i,j,k

Read rows and columns of first matrix

Read values of first matrix

Read rows and columns of second matrix

Read values of second matrix

Print values of second matrix

if(column1= =row2)

43

Page 44: C manual vivek

Initialise i to 0

if(i<row1)

Initialise j to 0

if(j<column1)

c[i][j]=0

initialize k to 0

if(k<row2)

c[i][j]=c[i][j]+a[i][k]*b[k][j]

Increment k by 1

Increment j by 1

Increment 1 by 1

Display matrix multiplication is not possible

44

Page 45: C manual vivek

Print resultant matrix

Stop

45

Page 46: C manual vivek

Program 6(c)

#include<stdio.h>#include<conio.h>void main(){ int a[5][5],b[5][5],c[5][5],m,n,o,p,i,j,k; clrscr(); x: printf("Enter the size of first matrix:"); scanf("%d%d",&m,&n); printf("Enter the size of second matrix:"); scanf("%d%d",&o,&p); if(n!=o) goto x; printf("Enter first matrix elements:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } printf("Enter second matrix elements:\n"); for(i=0;i<o;i++) { for(j=0;j<p;j++) scanf("%d",&b[i][j]); } printf("Multiplication of two matrices:\n"); for(i=0;i<m;i++) { for(j=0;j<p;j++) { c[i][j]=0; for(k=0;k<n;k++) c[i][j]=c[i][j]+(a[i][k]*b[k][j]); printf("%d\t",c[i][j]); } printf("\n"); } getch();}

46

Page 47: C manual vivek

Week-7 a). String manipulation

Algorithm:

Step 1: StartStep 2: Declare Character array st, st1Step 3: Read strings st, st1Step 4: Print length of st, st1Step 5: if st==st1 then print both strings are equalStep 6: else if st<st1 then print first string is less than second stringStep 7: else print first string is greater than second string.

Flow Chart

Start

Declare strings st,st1

Read st,st1

Calculate length of st,st1

Print length of st,st1

if( st = = st1)

Print Two strings are equal

Y

N

47

Page 48: C manual vivek

Program 7(a)

48

if( st < st1)

Print first string is less than second string

if( st > st1)

Y

N

Print first string is greater than second string

Y

N

Stop

Page 49: C manual vivek

#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char str[30],str1[30]; clrscr(); printf("Enter string:"); gets(str); printf("Enter another string:"); gets(str1); printf("Length of first string=%d",strlen(str)); printf("\nLength of second string=%d",strlen(str)); if(strcmp(str,str1)==0) printf("\nTwo strings are equal"); else if(strcmp(str,str1)<0) printf("\n%s < %s",str,str1); else printf("\n%s > %s",str,str1); getche();}

Week-7(b) Program to check given string is palindrome or not

49

Page 50: C manual vivek

Algorithm:

Step 1: StartStep 2: Declare s, s1, i, jStep 3: Read string sStep 4: initialize i= length of (s) -1 , j=0Step 5: if(i>=0) 5.1: s1[j]=s[i] 5.2: i=i-1, j=j+1, goto step 5Step 6: s1[j]= end of stringStep 7: if(s = = s1) 7.1: print given string is palindromeStep 8: if(s ! = s1) 8.1: print given string is not palindromeStep 9: Stop

Flow chart

Start

Declare s, s1, i, j

Read s

initialize i= length of (s) -1 , j=0

if(i>=0)

s1[j]=s[i]

i=i-1, j=j+1

s1[j]=’\0’

50

Page 51: C manual vivek

Program 7(b)

#include<stdio.h>#include<conio.h>#include<string.h>main(){ char st[30],st1[30]; int n,i; clrscr(); printf("Enter the string:"); gets(st); n=strlen(st)-1; for(i=0;i<=n;i++) st1[i]=st[n-i]; st1[i]='\0'; if(stricmp(st,st1)==0) printf("Given String is palindrome"); else printf("Given String is not palindrome"); getch();}

Week-8(a)-i Factorial using non recursion

if (s = = s1)

Print Given string is palindrome

Print Given string is not palindrome

Stop

51

Page 52: C manual vivek

Algorithm

Step-1 : StartStep-2 : Declare variables n,factStep-3 : Read vaue for nStep-4 : Call the function with argument n and result is in fact 4.1 : Declare variables fact with in function 4.2 : initialize fact value to 1 4.3 : Initialise i value to 0 4.4 : if(i<=n) 4.4.1 : fact=fact*i; 4.5 : Increment value of i and go to step-4.4 4.6 : return fact and value is returned to called functionStep-5 : Display factorial valueStep-6 : Stop

Flowchart

52

Start

Declare variables n,fact

Read vaue for n

Call the function with argument n and result is in fact

Declare variables fact with in function

initialize fact value to 1 Initialise i value to 0

if(i<=n)

fact=fact*i;

Page 53: C manual vivek

Program 8(a)-i

#include<stdio.h>#include<conio.h>long fact(int n);main(){ int n; clrscr(); printf("Enter value of x:"); scanf("%d",&n); printf("Factorial=%ld",fact(n)); getch();}long fact(int n){ int i; long f=1; for(i=1;i<=n;i++) f=f*i; return f;}

Week-8(a)-ii Factorial using recursion

return fact and value is returned to called function

Display factorial value

Stop

Increment value of i

53

Page 54: C manual vivek

AlgorithmStep-1 : StartStep-2 : Declare variables n,factStep-3 : Read vaue for nStep-4 : Call the function factorial with argument n and result is in fact 4.1 : if(n= =0) 4.1.1 : return 1 to called function 4.2 : if(n!=0) 4.2.1 : return(factorial(n*(n-1)) to called functionStep-5 : Display factorial valueStep-6 : StopFlowchart

Program 8(a)-ii

Start

Declare variables n,fact

Read vaue for n

Call the function factorial with argument n and result is in fact

if(n= =0)

return 1 to called function return(factorial(n

*(n-1)) to called function

Display factorial value

Stop

54

Page 55: C manual vivek

#include<stdio.h>#include<conio.h>long fact(int n);main(){ int n; clrscr(); printf("Enter value of x:"); scanf("%d",&n); printf("Factorial=%ld",fact(n)); getch();}long fact(int n){ if(n==0) return 1; return n*fact(n-1);}

Week-8(b)-i GCD using non-recursion

55

Page 56: C manual vivek

AlgorithmStep-1 : StartStep-2 : Declare variables a,b,c,min,maxStep-3 : Read values of a and bStep-4 : find the minimum number min=a<b?a:bStep-5 : find the maximum number max=a>b?a:bStep-6 : Call the function GCD with two arguments min,max 6.1 : Declare variable c 6.2 : if((min<max)&&(c!=0)) 6.2.1 : c=max%min 6.2.2 : max=min 6.2.3 : min=c 6.2.4 : Go to step-6.2 6.3 : Return max value to called functionStep-7 : Display value of GCDStep-8 : StopFlowchart

Declare variables a,b,c,min,max

Read values of a and b

find the minimum number min=a<b?a:bfind the maximum number max=a>b?a:b

Call the function GCD with two arguments min,max

Start

56

Page 57: C manual vivek

Program 8(b)-i

Declare variable c

if((min<max)&&(c!=0))

c=max%min max=min min=c

Return max value to called function

Display value of GCD

Stop

57

Page 58: C manual vivek

#include<stdio.h>#include<conio.h>int gcd(int x,int y);main(){ int a,b,max,min; clrscr(); printf("Enter value of a:"); scanf("%d",&a); printf("Enter value of b:"); scanf("%d",&b); max=a>b?a:b; min=a<b?a:b; printf("GCD=%d",gcd(max,min)); getch();}int gcd(int x,int y){ int c=-1; while(c!=0) { c=x%y; x=y; y=c; } return x;}

Week-8(b)-ii GCD using recursion

58

Page 59: C manual vivek

AlgorithmStep-1 : StartStep-2 : Declare variables a,b,c,min,maxStep-3 : Read values of a and bStep-4 : find the minimum number min=a<b?a:bStep-5 : find the maximum number max=a>b?a:bStep-6 : Call the function GCD with two arguments min,max 6.1 : if(c= =0) 6.1.1 : return min TO called function 6.2 : if(c!=0) 6.2.1 : c=max%min 6.2.2 : return GCD(min,c) to called functionStep-7 : Display GCD valueStep-8 : Stop

Flowchart

Start

Declare variables a,b,c,min,max

Read values of a and b

find the minimum number min=a<b?a:b

find the maximum number max=a>b?a:b

Call the function GCD with two arguments min,max

59

Page 60: C manual vivek

Program 8(b)-ii

if(c= =0)

return min TO called function

return GCD(min,c) to called function

Display GCD value

Stop

60

Page 61: C manual vivek

#include<stdio.h>#include<conio.h>int gcd(int x,int y);int c=-1;main(){ int a,b,max,min; clrscr(); printf("Enter value of a:"); scanf("%d",&a); printf("Enter value of b:"); scanf("%d",&b); max=a>b?a:b; min=a<b?a:b; printf("GCD=%d",gcd(max,min)); getch();}int gcd(int x,int y){ if(c==0) return x; c=x%y; return gcd(y,c);}

61

Page 62: C manual vivek

Week-9(a) Program to insert a substring to main string from given position

Algorithm:

Step 1 : StartStep 2 : Declare s, s1, s2, i, posStep 3 : Read String sStep 4 : Read String s1Step 5 : Read posStep 6 : initialize i=pos, j=0Step 7 : if i<length of string s 7.1 : s2[j] = s[i] 7.2 : s[i]=s1[j] 7.3 : j=j+1, i=i+1 7.4 : goto step 7Step 8 : concat s, s2Step 9 : Print sStep 10 : Stop

Flow Chart:

Start

Declare s, s1, s2, i, j, pos

Read Strings s, s1

Read pos

initialize i=pos, j=0

if i<length of string s

62

Page 63: C manual vivek

Program 9(a)

s2[j] = s[i]s[i]=s1[j]

j=j+1, i=i+1

Concat s, s2

Print s

Stop

63

Page 64: C manual vivek

#include<stdio.h>#include<conio.h>#include<string.h>

insert(char *s,char *s1,int p){ char s2[30]; int i,j,n; n=strlen(s); for(i=p,j=0;i<strlen(s);i++,j++) s2[j]=s[i]; for(i=p,j=0;j<strlen(s1);i++,j++) s[i]=s1[j]; s[p+j]='\0'; s2[n-p]='\0'; strcat(s,s2); printf("String=%s",s);} main(){ char s[30],s1[30]; int pos; clrscr(); printf("Enter the string:"); gets(s); printf("Enter string to insert:"); gets(s1); printf("Enter the position:"); scanf("%d",&pos); insert(&s,&s1,pos); getch();}

64

Page 65: C manual vivek

Week-9(b) Program to delete ‘n’ characters from the given position of string

Algorithm:

Step 1 : StartStep 2 : Declare s, s1, i, j, pos, nStep 3 : Read String sStep 4 : Read pos, nStep 5 : initialize i=pos, j= pos + nStep 6 : if j< length of string s 6.1 : s1[i] = s[j], s[i]=s1[i] 6.2 : j=j+1, i=i+1 6.3 : goto step 6Step 7 : s[length-n]=end of stringStep 8 : Print sStep 9 : Stop

Flow Chart:

Start

Declare s, s1, i, j, pos, n

Read Strings s

Read pos, n

initialize i=pos, j=pos+n

if j< length of string s

65

Page 66: C manual vivek

s1[i] = s[j], s[i]=s1[i]

j=j+1, i=i+1

s[length-n]=’\0’

Print s

Stop

Concat(s,s1)

66

Page 67: C manual vivek

Program 9(b)

#include<stdio.h>#include<conio.h>#include<string.h>void deletion(char *s,int p,int n){char s1[50];int i,j,k;k=p+n; if(k<strlen(s)) { for(i=p,j=n+p;i<strlen(s);i++,j++) { s1[i]=s[j]; s[i]=s1[i]; } } else s[p]='\0'; s[k-n]='\0'; printf("%s",s);}main(){ char s[20]; int p,n,i,j; clrscr(); printf("Enter string:"); gets(s); printf("enter position"); scanf("%d",&p); printf("Enter n:"); scanf("%d",&n); deletion(s,p,n); getch(); }

67

Page 68: C manual vivek

Week-9(c) Program to replace a substring to main string from given position

Algorithm:

Step 1 : StartStep 2 : Declare s, s1, i, j, posStep 3 : Read String sStep 4 : Read String s1Step 5 : Read posStep 6 : initialize i=pos, j=0Step 7 : if j<length of s1 7.1 : s[i] = s1[j] 7.2 : j=j+1, i=i+1 7.3 : goto step 7Step 8 : if(j>length of string s) 8.1 : s[pos+j]=end of stringStep 9 : Print sStep 10 : Stop

Flow Chart:

Start

Declare s, s1, i, j, pos

Read Strings s, s1

Read pos

initialize i=pos, j=0

if j<length of string s1

68

Page 69: C manual vivek

s[i]=s1[j]

j=j+1, i=i+1

s[p+j]=’\0’

Print s

Stop

If(j> length of string s

69

Page 70: C manual vivek

Program 9(c)

#include<stdio.h>#include<conio.h>void strreplace(char *s,char *s1,int pos);void main(){ char str[30],str1[30]; int pos; clrscr(); printf("Enter string:"); gets(str); printf("Enter string to replace:"); gets(str1); printf("Enter position to replace:"); scanf("%d",&pos); strreplace(str,str1,pos); getch();}void strreplace(char *s,char *s1,int pos){ char s2[30]; int i,j; for(i=pos,j=0;i<pos+strlen(s1);i++,j++) s[i]=s1[j]; if(i>strlen(s)) s[i]='\0'; printf("After replacement=%s",s);}

70

Page 71: C manual vivek

Week-10(a) Program for finding maximum and minimum values of arrays

Algorithm:

Step 1: StartStep 2: Declare array a[50], nStep 3: Read nStep 4: Read array a from a[0] to a[n-1] Step 5: Call function maxmin(a,n)Step 6: In maxmin function declare i, max, minStep 7: initialize max=a[0], min=a[0], i=0Step 8: if i<n then goto step- else goto step- 8.1: if(max<a[i]) then max=a[i] 8.2: if(min>a[i]) then min=a[i] 8.3: i=i+1 and goto step 8Step 9: Print max, minStep 10: Stop

Flow Chart:

Start

Declare a[50], n

Read n

Read array a from a[0] to a[n-1]

Call function maxmin(a,n)

Declare i, max, min

initialize max=a[0], min=a[0], i=0

71

Page 72: C manual vivek

max=a[i]

if min>a[i]

if i<n

if max<a[i]

min=a[i]

i=i+1

Print max, min

Stop

72

Page 73: C manual vivek

Program 10(a)

#include<stdio.h>#include<conio.h>int maxmin (int *p,int n);main(){ int a[50],i,n; clrscr(); printf("Enter number of elements:"); scanf("%d",&n); printf("Enter elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); maxmin(a,n); getch();}int maxmin (int *p,int n){ int max,min,i; max=p[0]; min=p[0]; for(i=1;i<n;i++) { if(max<p[i]) max=p[i]; if(min>p[i]) min=p[i]; } printf("Maximum=%d\nMinimum=%d",max,min);}

73

Page 74: C manual vivek

Week 10(b) Program to print Pyramid of numbers

Algorithm

Step 1: StartStep 2: Declare nStep 3: Read nStep 4: call function pyramid(n)Step 5: In pyramid function declare i, j, kStep 6: initialize i=1Step 7: if(i<=n) then goto step 7.1 else goto step-8 7.1: initialize k=1 7.2: if(k<=n-i) then goto step 7.2.1 else goto step-7.3 7.2.1: print 2 spaces 7.3: initialize j=1 7.4: if(j<=i) then goto step 7.4.1 else goto step-7.5 7.4.1: print i with 4 spaces 7.5: print next lineStep 8: Stop

Flow Chart

Start

Declare n

Read n

Call function pyramid(n)

Declare i, j, k

Initialize i=1

74

Page 75: C manual vivek

if(k<n-i)

Print 2 spaces

if(j<=i)

Print I with 4 spaces

Initialize j=1

Print next line

Stop

if i<=n

Initialize k=1

75

Page 76: C manual vivek

Program 10(b)

#include<stdio.h>#include<conio.h>void main(){ void pyramid(int); int n; clrscr(); printf("Enter the number of lines:"); scanf("%d",&n); pyramid(n); getch();}void pyramid(int n){ int i,j,k; for(i=1;i<=n;i++) { for(k=1;k<=n-i;k++) printf("%2c",32); for(j=1;j<=i;j++) printf("%-4d",i); printf("\n"); }}

76

Page 77: C manual vivek

Week-11(a) Program to print geometric series 1+x2+x3+x4+………..+xn

Algorithm:

Step 1: StartStep 2: Declare x, nStep 3: Read x, nStep 4: call the function geometric(x,n)Step 5: Declare i, j, sumStep 6: initialize i=0, sum=0Step 7: if(i<=n) then goto step 7.1 else goto step 8 7.1: sum= sum+xi

7.2: i=i+1 goto step-7Step 8: print sumStep 3: Stop

Flow chart

77

Page 78: C manual vivek

Program 11(a)

#include<stdio.h>#include<conio.h>long geometric(int x,int n);main(){ int x,n; clrscr(); printf("Enter value of x:"); scanf("%d",&x); printf("Enter value of n:"); scanf("%d",&n); printf("Geometric series=%ld",geometric(x,n)); getch();}long geometric(int x,int n)

start

Declare x, n

Read x, n

call the function geometric(x,n)

Declare i, j, sum

if i<=n

Initialize i=0, sum=0

sum= sum+xi

i=i+1

Print sum

stop

78

Page 79: C manual vivek

{ int i,j; long sum=0; for(i=0;i<=n;i++) { sum=sum+(long)(pow(x,i)); } return sum;}

Week-11(b) Program to find sin(x) and cos(x) for accuracy of n terms

Algorithm:

Step 1 : StartStep 2 : Declare x, nStep 3 : Read x, nStep 4 : Call function sine(x,n) and print resultStep 5 : Declare i, j, sum, fStep 6 : initialize i=1, sum=0Step 7 : if(i<=n) 7.1 : initialize j=1, f=1 7.2 : if(j<=(2*n)-1) 7.2.1 : f=f*j, j=j+1, goto step 7.2 7.3 : sum= sum+ (-1)i-1 * x(2*i)-1)/f), goto step 7Step 8 : return sumStep 9 : Call function cosine(x,n) and print result

79

Page 80: C manual vivek

Step 10 : Declare i, j, sum, fStep 11 : initialize i=1, sum=0Step 12 : if(i<=n) 12.1 : initialize j=1, f=1 12.2 : if(j<=(2*n)-2) 12.2.1 : f=f*j, goto step 12.2 12.3 : sum= sum+ (-1)i-1 * x(2*i)-2)/f), i=i+1, goto step 12Step 13 : return sum

Flow Chart

Call function sine(x,n)

Call function cosine(x,n)

80

Declare variables x, n

Start

Read the value of x, n

Page 81: C manual vivek

initialize i=1, sum=0

if i<=n

initialize j=1, f=1

Declare variables i, j, sum, f

if(j<=2*i-1)

f=f*j, j=j+1

sum= sum+ (-1)i-1 * x(2*i)-1)/f)i=i+1

Return sum

initialize i=2, sum=1

Declare variables i, j, sum, f

if i<=n

initialize j=1, f=1

81

Page 82: C manual vivek

Program 11(b)

if(j<=2*i-2)

f=f*j, j=j+1

sum= sum+ (-1)i-1 * x(2*i)-2)/f)i=i+1

Return sum

Stop

82

Page 83: C manual vivek

#include<stdio.h>#include<conio.h>float sine(int x,int n);float cosine(int x,int n);main(){ int x,n; clrscr(); printf("Enter value of x:"); scanf("%d",&x); printf("Enter value of n:"); scanf("%d",&n); printf("Sin(x)=%.2f",sine(x,n)); printf("\nCos(x)=%.2f",cosine(x,n)); getch();}float sine(int x,int n){ int i,j; long f=1; float sum=0; for(i=1;i<=n;i++) { f=1; for(j=1;j<=(2*i)-1;j++) f=f*j; sum=sum+(float)(pow(-1,i-1)*pow(x,(2*i)-1)/f); } return sum;} float cosine(int x,int n){ int i,j; long f=1; float sum=0; for(i=1;i<=n;i++) { f=1; for(j=1;j<=(2*i)-2;j++) f=f*j; if(i==0) f=1; sum=sum+(float)(pow(-1,i-1)*pow(x,(2*i)-2)/f); } return sum;}

83

Page 84: C manual vivek

Week-12(a) Program to interchange two numbers by using call by reference

Algorithm:

Step 1 : StartStep 2 : Declare a, bStep 3 : Read a, bStep 4 : Call function swap(&a,&b) 4.1 : In function swap(*x,*y) 4.2 : *x=*x+*y; 4.3 : *y=*x-*y; 4.4 : *x=*x-*y;Step 5 : Print a, bStep 6 : Stop

Flowchart:

Declare variables a, b

Start

Read the value of a, b

Call function swap(&a,&b)

In function swap(*x,*y)

*x=*x+*y;*y=*x-*y;*x=*x-*y;

Print a, b

Stop

84

Page 85: C manual vivek

Program 12(a)

#include<stdio.h>#include<conio.h>swap(int*,int*);main(){ int a,b; clrscr(); printf("Enter 2 numbers:"); scanf("%d%d",&a,&b); printf("%u,%u",&a,&b); swap(&a,&b); printf("After swaping a=%d,b=%d",a,b); getch();}swap(int *x,int *y){ *x=*x+*y; *y=*x-*y; *x=*x-*y;}

85

Page 86: C manual vivek

Week-12(b) Program to read and print elements in dynamic array

Algorithm:

Step 1 : StartStep 2 : Declare *a, n, i, t, *sStep 3 : Read nStep 4 : Allocate dynamic memory for a ‘n’ elementsStep 5 : s=aStep 6 : initialize i=0Step 7 : if(i<n) 7.1 : Read t 7.2 : *a=t 7.3 : a=a+1 7.4 : i=i+1, goto step-7Step 8 : a=sStep 9 : initialize i=0Step 10 : if(i<n) 10.1 : print *(a+i) 10.2 : i=i+1, goto step 10Step 11 : Stop

Flow Chart:

Declare variables *a, n, i, t, *s

Start

Read the value of n

Allocate dynamic memory for a ‘n’ elements

i=0

if(i<n)

86

Page 87: C manual vivek

Read the value of t

*a=ta=a+1i=i+1

a=s

if(i<n)

Print *(a+i)

i=i+1

Stop

87

Page 88: C manual vivek

Program 12(b)

#include<stdio.h>#include<conio.h>void main(){ int *a,n,i,t,*s; clrscr(); printf("Enter number of elements:"); scanf("%d",&n); a=calloc(n,sizeof(int)); printf("Enter elements:"); s=a; for(i=0;i<n;i++) { scanf("%d",&t); *a=t; a=a+1; } a=s; printf("Elements in array are:"); for(i=0;i<n;i++) printf("%d\t",*(a+i)); getch();}

88


Recommended