Professional Documents
Culture Documents
C Programming Lab
C Programming Lab
net
C Programming Lab
I B.Tech
EXERCISE: 1
a) Convert temperature from centigrade to Fahrenheit and Fahrenheit to centigrade.
DESCRIPTION:
To convert Fahrenheit to centigrade, subtract 32 and multiply by 0.56.
To convert centigrade to Fahrenheit, multiply by 1.8 and add 32 degrees.
ALGORITHM:
Step 1: start
Step2: read temperature in Fahrenheit
Step 3: calculate centigrade = (Fahrenheit -32)*0.56
Step 4: display centigrade value
Step 5: read temperature in centigrade
Step 6: calculate Fahrenheit = 32 + (centigrade * 1.8)
Step 7: display Fahrenheit value
Step 8: stop
Page |1
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
Read f
c=(fa-32)*0.56
Display c
Read c
fa = 32+(c*1.8)
Display fa
stop
OUTPUT:
Enter Temperature in Fahrenheit:52
Temperature in Celsius is: 11.200000
Page |2
www.jntuworld.com || www.jwjobs.net
GEC
Page |3
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
start
Read marks
total=m1+m2+m3+m4
+m5+m6;
avg=total/6;
avg >80
yes
Display
grade =A
no
avg >=60
and <=80
no
avg >=40
and <=60
yes
no
Display fail
yes
Display
grade =B
Display
grade = C
stop
Page |4
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter your marks 36
36
36
36
36
36
You Failed in this exam
Page |5
www.jntuworld.com || www.jwjobs.net
GEC
Page |6
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
start
Read income
income >
0 and <
200000
income >
200001 and
<500000
no
yes
Display
no
yes
income >
500001 and
< 1000000
no
yes
tax = income*0.2
tax = income*0.1
tax = income *
0.3
No tax
Display tax
Display tax
Display tax
OUTPUT:
Enter Income: 500000
stop
Page |7
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE: 2
To convert the given binary number to 2s complement.
DESCRIPTION:
In this program the given binary number is first covert the numbers 0 to1 and 1 to 0.
And finally add the 1 to the converted number. Then we will get the 2s complement
number.
ALGORITHM:
Step1: Start
Step2: Read a binary number b
Step3: store length of the binary number-1 value in n
Step4: initialize i=n
Step5: if i>=0 otherwise go to step7
(i) if b[i]==1 is true, otherwise go to step6
(A) initialize j=i-1
(B) if j>=0 otherwise break
(a) if b[j]==1 otherwise b[j]=1 go to step5(i)(B)(b)
b[j]=0
(b) decrement j go to step5(i)(B)
Step6: decrement i, go to step5
Step7: print b
Step8: stop
Page |8
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Read a binary
number
n= strlen(b)-1
false
i=n
i-i >=0
true
If
b[i]==1
false
true
false
j = i-1
j>=0
j-true
if
b[j]==1
false
true
b[j] = 0
b[j] = 1
Display 2s
complement
of number
Stop
OUTPUT:
Enter Binary Number: 101010
Its 2's Complement Is: 010110
Enter Binary Number: 11111
Its 2's Complement Is: 00001
Page |9
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE 3
a) To find the sum of individual digits of a given number
DESCRIPTION:
Sum of the individual digits means adding all the digits of a number
Ex: 123 sum of digits is 1+2+3=6
ALGORITHM:
Step 1: start
Step 2: initialize the s=0
Step 3: read n
Step 4: if n<0 go to Step 7
Step 5: if n!=0 go to Step 6 else go to step 7
Step 6: store n%10 value in p
Add p value to s
Assign n/10 value to n
Go to Step 5
Step 7: print s
Step 8: stop
P a g e | 10
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
START
s=0
Read N
IF N < 0
TRUE
FALSE
IF
N! =0
Print S
TRUE
STOP
P=N%10
S=S+P
N=N/10
OUTPUT:
enter the value for n:
123
sum of individual digits is 6
enter the value for n:
-11
The given number is not valid
P a g e | 11
www.jntuworld.com || www.jwjobs.net
GEC
i)
Step 7: stop
P a g e | 12
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Initialize
a=0,b=1
Read n
True
False
If n==1
True
False
If n ==2
Output a
Output a,b
Output a,b
i++
i=3
False
i<=n
true
c =a + b
Output c
a=b
b=c
Stop
OUTPUT:
enter n value5
0
enter n value10
0
P a g e | 13
13
21
34
www.jntuworld.com || www.jwjobs.net
GEC
P a g e | 14
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
Read n
i=1
false
i++
i<=n
true
c=0
j=1
false
false
j++
j<=i
true
if c==2
if i % j == 0
false
true
true
Display i
c ++
stop
OUTPUT:
Enter Value of n:5
Prime Numbers upto 5 are:
235
P a g e | 15
www.jntuworld.com || www.jwjobs.net
GEC
P a g e | 16
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
START
a=0,b=1
c = a+b
Read n
no
if c< n
if n ==0 or
n==1 or
n ==c
no
yes
c= a+b
a=b, b=c
yes
Display n is not
a Fibonacci
number
Display n is
Fibonacci
number
Stop
OUTPUT:
Enter no to find: 21
21 is Fibonacci
Enter no to find: 25
25 is not Fibonacci
P a g e | 17
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE: 4
a) To calculate the sum. Sum=1-x2/2! + x4/4! - x6/6! + x8/8! - x10/10!
ALGORITHM:
Step 1: start
Step 2: declare sum=0.0,p=0
Step 3: read x,n value
Step 4: initialize i=1
Step5: if i<=n initialize f=1 otherwise go to step 11
Step6: initialize j=1
Step7: if j<=p
assign f*j to f
increment j
otherwise go to step 8
Step8: if i%2==0 sum=sum-(pow(x,p)/f) otherwise sum=sum+(pow(x,p)/f)
Step9: compute p=p+2
Step 10: increment i
Step 11: print sum value
Step 12: stop
P a g e | 18
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
Read x,n
false
i=1,
i<=n
i++
true
f= 1
true
j=1,
j<=p
f= f*j
j++
false
if i%2
== 0
false
true
sum=sum(pow(x,p)/f)
Display
sum
sum=sum+
(pow(x,p)/f)
p = p+2
Stop
P a g e | 19
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter Number of Terms in the Series:3
Enter x value:1
The sum of the Series upto 3 terms: 0.541667
P a g e | 20
www.jntuworld.com || www.jwjobs.net
GEC
go to step 7
Step7: print r1, r2
Step8: stop
P a g e | 21
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Read a,b,c
d = pow((b*b-4*a*c),0.5)
true
false
If d < 0
If d== 0
false
true
r1=-b / (2 * a )
r2= -b / (2 * a)
r1 = ((-b+d) / (2*a))
r2 = ((-b-d) / (2*a))
Display
roots are
imaginary
Display
r1, r2
Stop
P a g e | 22
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter the values of a,b,c:1 6 9
Roots are Equal:-3.000000,-3.000000
P a g e | 23
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE: 5
a) The total distance travelled 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 travelled 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'.
DESCRIPTION:
The total distance travelled 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).
ALGORITHM:
Step 1:Start
Step2 : Read u,a,t
Step 3: compute s=(u*t)+(0.5*a*t*t)
Step 4:print d value
Step 5: read ch
Step 6: if ch==1 go to step 2 otherwise go to step 7
Step 7: stop
P a g e | 24
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
start
Read u, a, t
d= (u*t)+(0.5*a*t*t)
Display
distance as d
Read ch
true
If
ch==1
false
stop
P a g e | 25
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter the value of initial velocity:12
Enter the value of acceleration:14
Enter time interval:2
The distance is: 52.000000
Press 0 to quit ---- 1 to continue
1
Enter the value of initial velocity:16
Enter the value of acceleration:15
Enter time interval:4
The distance is: 184.000000
Press 0 to quit ---- 1 to continue
0
P a g e | 26
www.jntuworld.com || www.jwjobs.net
GEC
b) Two integer operands and one operator form user, performs the operation and then prints the
result. (Consider the operators +,-,*, /, % and use Switch Statement)
Description:
To take the two integer operands and one operator from user to perform the some arithmetic
operations by using the following operators like +,-,*, /, %
Ex: 2+3=5
Algorithm:
Step 1: Start
Step 2: Read the values of op1,op2 and operator op
Step 3: if the operator is + then
R=op1+op2
Go to step 8
Break
Step 4: Else if the operator is - then
R=op1-op2
Go to step 8
Step 5: Else if the operator is * then
R=op1*op2
Go to step 8
Step 6: Else if the operator is / then
R=op1/op2
Go to step 8
Step 7: Else if the operator is % then
R=op1%op2
Go to step 8
Step 8: print R
Step 9: stop
P a g e | 27
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
start
Read
op1,op2, op
If op
==+
true
false
If op
==-
false
r=op1+op2
true
If op
==*
false
r=op1-op2
true
If op
==/
false
r=op1*op2
true
If op
==%
false
r=op1/op2
true
r=op1%op2
Display r
Display
invalid
operation
stop
P a g e | 28
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Operations Performed are: + - * / %
Enter 2 Integers & an Operator:2
4
*
MULTIPLICATION -- Product is 8
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE: 6
a) String functions (predefined) examples.
DESCRIPTION:
Apply some of the predefined functions on given strings. These are included in string.h header file.
Algorithm
Step 1 : start
Step 2: read s1,s2,s3
Step 3: compute l1= strlen(s1)
Step 4: print l1
Step 5: compute s=strcpy(s3,s1)
Step 6: print s
Step7: compute e= strcmp(s1,s2)
Step 8: if e==0 go to step 9 otherwise go to step 10
Step 9: display strings are equal
Step 10: display Strings are not equal
Step 11: display reverse of 1 st string is , strrev(s1)
Step 12: display after concatenation of two strings s1 and s2 is strcat(s1,s2)
Step 13: stop
P a g e | 30
www.jntuworld.com || www.jwjobs.net
GEC
Flowchart:
start
Read strings
s1,s2,s3
l1=strlen(s1)
Display l1
s=strcpy(s3,s1)
Display s
e=strcmp(s1,s2)
If
e==0
false
r=strrev(s1)
c=strcat(s1,s2)
Display r
Display c
true
Display s1,s2
are equal
Display s1,s2
are not equal
stop
P a g e | 31
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter three strings sagf
gec
gudlavalleru
length of s1 is 4
After copying 1st string into 3rd string is sagf
The two Strings s1 and s2 are not equal
Reverse of first string is fgas
After adding first two strings fgasgec
P a g e | 32
www.jntuworld.com || www.jwjobs.net
GEC
start
FLOWCHART:
Read string s1
strcpy(s2,s1)
strrev(s2)
p=strcmp(s1,s2)
false
if p
== 0
true
The given
string s1 is
palindrome
The given
string s1 is not
palindrome
stop
P a g e | 33
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter a string: madam
The given string is palindrome
P a g e | 34
www.jntuworld.com || www.jwjobs.net
GEC
Read a string
s1
false
i=0
s1[i]!=\0
i++
true
s2[i]=s1[i]
s2[i]=\0
Display s2
stop
P a g e | 35
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter a string : malayalam
The copied string is malayalam
Enter a string : haihello
The copied string is haihello
P a g e | 36
www.jntuworld.com || www.jwjobs.net
GEC
Read
string s1
false
s1[i]!=\0
i=0
i++
true
sum ++
Display
sum
stop
P a g e | 37
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter a string : gudlavalleruengineeringcollege
30
Enter a string : string
6
P a g e | 38
www.jntuworld.com || www.jwjobs.net
GEC
P a g e | 39
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
start
Initialize sum = 0
Read
string s1
false
s1[i]!=\0
i=0
i++
true
sum ++
false
i=0, j=sum-1
s1[i]!=\0, j>=0
i++,j-true
rev[j]=s[i]
rev[j]=\0
Display
rev
stop
P a g e | 40
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter a string : sai
Reverse of the given string is ias
P a g e | 41
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE: 7
a)
Functions to insert a sub string into given main string from a given position
DESCRIPTION:
In this program we need to insert a string into another string from a specified position.
ALGORITHM:
Step 1: start
Step 2: read main string and sub string
Step 3: find the length of main string(r)
Step 4: find length of sub string (n)
Step 5: copy main string into sub string
Step 6: read the position to insert the sub string (p)
Step 7: copy sub string into main string from position p-1
Step 8: copy temporary string into main string from position p+n-1
Step 9: print the strings
Step 10: stop
FLOWCHART:
Start
Insert (s1,s2,i)
Start
P a g e | 42
www.jntuworld.com || www.jwjobs.net
GEC
i=0
No
i++,c++
s1[ i]!=\0
Yes
No
if i==x
Yes
No
j=0
j++,c++
s2 [j]! =\0
Yes
s[c] =s2[j]
s[c]=s1[i]
s[c]='\0'
Print
string s
Return to main()
P a g e | 43
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter the Original String: computer
Enter the subString: gec
Enter the index where you want to insert the subString: 3
The String now is:
-----------------comgecputer
P a g e | 44
www.jntuworld.com || www.jwjobs.net
GEC
P a g e | 45
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
Read string
Read position, no of
characters
Stop
delchar( )
if ((a+b-1)<=
strlen(x))
Strcpy(&x[b-1],&x[a+b-1])
Print x
Return to
main()
OUTPUT:
1. enter the string
nagaraju
Enter the position from where to delete:4
Enter the number of charcters to be deleted3
nagju
2. enter the string
kaliraju
Enter the position from where to delete:0
Enter the number of charcters to be deleted4
Raju
P a g e | 46
www.jntuworld.com || www.jwjobs.net
GEC
DESCRIPTION:
Replace a character of string either from beginning or ending or at a specified location.
ALGORITHM:
Step 1: start
Step 2: read string
Step 3: find the length of the string
Step 4: read the replace characters
Step 5: read the position to be placed like end,begin,pos
Step 6: char placed in a part of string from position to end, and (position+number
of characters to end)
Step 7: print string
Step 8: stop
start
FLOW CHART:
Read string
Read replace
character
chara
switch
opt
case p
false
case b
case e
pos >=0
and pos
<=l-1
true
s1[0] = ch;
s1[l-1] = ch;
s1[pos]= ch;
Not match
Print string
P a g e | 47
stop
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter string :cplab
Enter character to replace :n
Enter b-begin, e-ending , p-position b
after replacing string is nplab
P a g e | 48
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE : 8
Write a C program that uses functions to perform the following operations using Structure:
i) Reading a complex number
P a g e | 49
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
Declare structure
Read choice
switch
choice
case add
c3.real=c1.real+c2.real
c4.img=c1.img+c2.img
case read
Read complex
numbers
case mul
c4.real=(c1.real*c2.real)-(c1.img*c2.img)
c4.img=(c1.real*c2.img)+(c1.img*c2.real)
case write
Display the
result
case exit
Stop
P a g e | 50
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
1:Reading
2:Writing
3:Addition
4:Multiplication
5:Exit
1:Reading
2:Writing
3:Addition
4:Multiplication
5:Exit
4:Multiplication
5:Exit
4:Multiplication
5:Exit
4:Multiplication
5:Exit
1:Reading
2:Writing
3:Addition
1:Reading
2:Writing
3:Addition
1:Reading
2:Writing
3:Addition
P a g e | 51
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE : 9
a) To perform the addition of two matrices
Description:
The program takes the two matrixes of same size and performs the addition
Algorithm:
Step 1: start
Step 2: read the size of matrices A,B m,n
Step 3: read the elements of matrix A
Step 4: read the elements of matrix B
Step 5: perform the addition operation
Step 6: print sum of matrices A and B
Step 7: Stop
P a g e | 52
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
start
If r1!=r2
and c1!=c2
true
false
Display
invalid order
of matrices
false
i=0
i++
i<r1
true
j=0
j<c1
false
j++
true
c[i][j]=a[i][j]+b[i][j];
stop
P a g e | 53
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter Number of rows & columns for Matrix1:2
2
Enter Number of rows & columns for Matrix2:2
2
Enter elements for Mat1:2
2
2
2
Enter elements for Mat2:2
2
2
2
Matrix Addition Result:
4
P a g e | 54
www.jntuworld.com || www.jwjobs.net
GEC
P a g e | 55
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
start
Read elements of
matrix
false
i=0
i++
i<c
true
j=0
j<r
false
j++
true
at[i][j]=a[j][i]
Print resultant
transpose matrix
stop
OUTPUT:
Enter Number of Rows: 2
Enter Number of Columns: 3
Enter 6 Elements: 2
3
4
5
6
7
P a g e | 56
www.jntuworld.com || www.jwjobs.net
GEC
After Transpose:
2 5
3 6
4 7
Enter Number of Rows: 3
Enter Number of Columns: 4
Enter 12 Elements: 1
2
3
4
5
6
7
8
9
10
11
12
After Transpose:
1 5 9
2 6 10
3 7 11
4 8 12
P a g e | 57
www.jntuworld.com || www.jwjobs.net
GEC
P a g e | 58
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
start
if c1!=r2
true
false
Read matrices A and B
i=0
Display
invalid order
of matrices
i++
i<r1
false
true
j=0
j<c2
false
j++
true
c[i][j]=0
k=0
k<c1
false
k++
true
c[i][j]=c[i][j]+(a[i][k]*b[k
][j])
stop
P a g e | 59
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter Number of rows & columns for Matrix1:2
2
Enter Number of rows & columns for Matrix2:2
2
Enter elements for Mat1:2
2
2
2
Enter elements for Mat2:2
2
2
2
Matrix Multiplication Result:
P a g e | 60
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE : 10
a) (i) Programs that use recursive function to find the factorial of a given integer.
DESCRIPTION:
Factorial of a number is nothing but the multiplication of numbers from a given number to 1
ALGORITHM: main program
Step 1: start
Step 2: read n
Step 3: call sub program
Step4: f=fact(n)
Step 5: print f value
Step 6: stop
Sub program:
Step 1: initialize the f
Step 2: if n= = 0 or n == 1 return 1 to main program if not goto step 3
Step 3: return n*fact(n-1) go to main program
P a g e | 61
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Read n
Call subprogram
f = fact(n)
print f
Stop
Sub program
fact()
false
If n=0 || n=1
true
Return 1
Return
n*fact(n-1)
Return to main
program
OUTPUT:
enter the number :7
factorial of number 7 is 5040
enter the number :5
factorial of number 5 is 120
P a g e | 62
www.jntuworld.com || www.jwjobs.net
GEC
a) ii) Program that use non recursive function to find the factorial of a given integer.
DESCRIPTION:
Factorial of a number is nothing but the multiplication of numbers from a given number to 1 Ex: 5!
=5*4*3*2*1= 120
ALGORITHM:
main program
Step 1: start
Step 2: read n
Step 3: call the sub program fact(n)
Step 4: print the f value
Step 5: stop
Sub program:
Step 1: initialize the f=1
Step 2: if n==0 or n=1 return 1 to main program. If not goto step 3
Step 3: perform the looping operation as follows
For i=1 i<=n; i++
Step 4: f=f*i
Step 5: return f value to the main program
P a g e | 63
www.jntuworld.com || www.jwjobs.net
GEC
start
FLOWCHART:
Read n
Call subprogram
Fact(n)
Print F value
Stop
Subprogram Flowchart
Fact (n)
Initialize F = 1
if n == 0 ||
n == 1
i= 1
Return 1
i++
i<=n
false
true
F=F*i
OUTPUT:
Return to main
program
Return F
P a g e | 64
www.jntuworld.com || www.jwjobs.net
GEC
b) To find the GCD of two given integers by using the recursive function
Description:
GCD means Greatest Common Divisor. i.e the highest number which divides the given number
Ex: GCD (12,24) is 12
Formula: GCD= product of numbers/ LCM of numbers
ALGORITHM:
main program
Step 1: start
Step 2: read a,b
Step 3: call the sub program GCD(a,b) and then print the value
Step 4: stop
Sub program:
Step 1: if n==0 return m else goto step 2
Step 2: return GCD (n,m%n)
Step 3: return to main program
P a g e | 65
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
Read a,b
Stop
Subprogram Flowchart
gcdrec( )
false
true
If n==0
Return m
Return to main()
OUTPUT:
enter the two numbers whose gcd is to be found:36
63
GCD of a,b is 9
enter the two numbers whose gcd is to be found:15
25
GCD of a,b is 5
P a g e | 66
www.jntuworld.com || www.jwjobs.net
GEC
b) ii)To find the GCD of two given integers by using the non recursive function
Description:
GCD means Greatest Common Divisor. i.e the highest number which divides the given number
Ex: GCD(12,24) is 12
Formula: GCD= product of numbers/ LCM of numbers
ALGORITHM:
Main program
Step 1: start
Step 2: read a,b
Step 3: call sub program g=GCD(a,b)
Step 4: print the g value
Step 5: stop
Sub program:
Step 1: initialize the p=1, q, remainder
Step 2: remainder=p-(p/q*q)
Step 3: remainder=0 return q else goto step 4
Step 4: GCD(q,remainder) return to main program
P a g e | 67
www.jntuworld.com || www.jwjobs.net
GEC
start
FLOWCHART:
Read a, b
Call subprogram
gcdnonrec(a,b)
Output g
stop
Subprogram Flowchart
gcdnonrec(m,n)
r=m%n
true
false
if r!=0
m=n,
n=r,
r=m%n
Return n
OUTPUT:
enter the two numbers whose gcd is to be found:36
63
GCD of a,b is 9
enter the two numbers whose gcd is to be found:15
25
GCD of a,b is 5
enter the two numbers whose gcd is to be found:36
54
GCD of a,b is 18
P a g e | 68
www.jntuworld.com || www.jwjobs.net
GEC
c).To solve the towers of Hanoi problem by using the recursive function
DESCRIPTION:
Towers of Hanoi problem means we have three towers
Source
intermediate
destination
Here source ,intermediate and destination are the three towers. We have to transfer all the disks
from source to destination towers. Here the restriction is not to place a big disk on smaller one . for
this we use intermediate tower. Finally the arrangements in the destination tower must be as same
as the disks in the source tower at first.
ALGORITHM:
main program
Step 1: start
Step 2: initialize the source=a, intermediate=c, destination = d
Step 3: read n
Step 4: call the sub program Hanoi recursion (n value,a ,b, c)
Step 5: stop
Sub program:
Step 1: if n== 1 call the sub program Hanoi recursion (num-1, a, c, b)
Step 2: print the output from a to b
Step 3: call the sub program Hanoi recursion(num-1, b, c, a)
Step 4: return to main program
P a g e | 69
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Assume Source = A
Intermediate = C
Destination = B
READ
no
Call subprogram
Hanoirec(no,source,intermediate,destination)
Stop
Sub program
Hanoirec(num,n1,n2,n3)
false
if num==1
true
Print A,B
Call sbgroram
Hanoirec(num-1,n1,n3,n2)
Print A,C
P a g e | 70
Call subprogram
Hanoirec(num-1,n3,n2,n1)
Return to main
program
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter the no. of disks to be transferred :3
recursive transfer with source needle A and destination needle B
Move top disk from needle A to needle B
Move top disk from needle A to needle C
Move top disk from needle B to needle C
Move top disk from needle A to needle B
Move top disk from needle C to needle A
Move top disk from needle C to needle B
Move top disk from needle A to needle B
P a g e | 71
www.jntuworld.com || www.jwjobs.net
GEC
ii)To solve the towers of Hanoi problem by using the non recursive function
DESCRIPTION:
Towers of Hanoi problem means we have three towers
Source
intermediate
destination
Here source ,intermediate and destination are the three towers. We have to transfer all the disks
from source to destination towers. Here the restriction is not to place a big disk on smaller one . for
this we use intermediate tower. Finally the arrangements in the destination tower must be as same
as the disks in the source tower at first.
ALGORITHM:
Step 1: start
Step 2: read no of disks
Step 3: initialize x=1
Step 4: if x < (1 << n) goto step 5 otherwise step 6
Step 5: print (x&x-1)%3, ((x|x-1)+1)%3 value
Step 6: stop
Start
FLOW CHART:
Read no of
disks
false
x=1
x++
x < (1 << n)
true
Print (x&x-1)%3,
((x|x-1)+1)%3
Stop
P a g e | 72
www.jntuworld.com || www.jwjobs.net
GEC
Output:
1.Enter the no. of disks to be transferred:3
nonrecursive
Move top disk from tower A to tower C
Move top disk from tower A to tower B
Move top disk from tower C to tower B
Move top disk from tower A to tower C
Move top disk from tower B to tower A
Move top disk from tower B to tower C
Move top disk from tower A to tower C
P a g e | 73
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE: 11
a) To find both the largest and smallest number in a list of integers
DESCRIPTION:
This program contains n number of elements, in these elements we can find the largest and smallest
numbers and display these two numbers
ALGORITHM:
Step 1: start
Step 2: read n
Step 3: initialize i=0
Step 4: if i<n do as follows. If not goto step 5
Read a[i]
Increment i
Goto step 4
Step 5: min=a[0], max=a[0]
Step 6: initialize i=0
Step 7: if i<n do as follows. If not goto step 8
If a[i]<min
Assign min=a[i]
Increment i goto Step 7
Step 8: print min,max
Step 9: stop
P a g e | 74
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Read n
false
i= 0
i<n
i++
true
Read a[i]
min = a[0]
max = a[0]
i=0
i++
false
i< n
true
false
true
if a[i]< min
min=a[i]
a[i]>max
true
max=a[i]
Print min,
max
stop
P a g e | 75
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
enter the array size:5
Enter the elements of array50
40
30
20
10
maximum value is:50
minimum value is:10
P a g e | 76
www.jntuworld.com || www.jwjobs.net
GEC
b) Program that displays the position or index in the string S where the string T begins , or 1 if S doesnt contain T
ALGORITHM:
Step 1: start
Step 2: read the string and then displayed
Step 3: read the string to be searched and then displayed
Step 4: searching the string T in string S and then perform the following steps
i. found=strstr(S,T)
ii. if found print the second string is found in the first string at the position.
otherwise goto step 5
Step 5: print -1
Step 6: stop
P a g e | 77
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
Read string t to be
searched
Found=strstr(s,t)
yes
no
If found
Print -1
Print string is
found
Stop
P a g e | 78
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter the first string:
gudlavalleru
Enter the string to be searched:
lava
Second String is found in the First String at 3 position.
P a g e | 79
www.jntuworld.com || www.jwjobs.net
GEC
c)
DESCRIPTION:
In this program we have to count the no of lines, no of words and no of characters in a given
program or given text by using the string function
ALGORITHM:
Step 1: Start
Step 2: Read the text until an empty line
Step 3: Compare each character with newline char \n to count no of lines
Step 4: Compare each character with tab char \t\ or space char to count no
of words
Step 5: Compare first character with null char \0 to find the end of text
Step 6: No of characters = length of each line of text
Step 7: Print no of lines, no of words, no of chars
Step 8: Stop
P a g e | 80
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
Initialize end=0,chars=0,words=0,lines=0
true
if
end!=0
true
c= 0
false
if
(ctr=getchar())==\n
true
line[c]=\0
line[c++]=ctr
if
line[0]=\0
false
Words ++
i=0
i ++
line[i]!=\0
true
Print lines,
Words, chars
false
If line[i]== ||
line[i]==\t
false
lines++
chars+=strlen(line)
true
stop
P a g e | 81
Words ++
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
hai
hello
how r u
Number of lines = 3
Number of words = 5
Number of characters = 15
Admiration is a very short-lived passion.
Admiration involves a glorious obliquity of vision.
Always we like those who admire us but we do not
like those whom we admire.
Fools admire, but men of sense approve.
Number of lines = 5
Number of words = 36
Number of characters = 205
P a g e | 82
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE : 12
a) To generate Pascals triangle
DESCRIPTION:
Pascals triangle which is used for a coefficient in the equation in polynomials.
ALOGRITHM:
Step 1: Start
Step 2: Read n
Step3: initialize i=0
Step4: if i<n otherwise go to step 11
Step5: initialize j=0
Step6: if j<=i otherwise go to step10
Step7: if j==0 or i==j otherwise p[i][j]=p[i-1][j-1] + p[i-1][j] go to step 9
Step8: p[i][j]=1
Step9: increment j go to step6
Step10: increment i go to step 4
Step11: initialize i=0
Step12: if i<n otherwise go to step19
Step13: initialize k=0
Step14: if k<=n-i otherwise go to step16
Step15: print space ,Increment k, go to step14
Step 16: initialize j=0
Step17: if j<=i otherwise print new line
Step18: print p[i][j] ,increment j go to step17
Step19: stop
P a g e | 83
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
Read n
i=0
i< n
true
j=0
j ++
false
i++
j<=i
false
true
true
i=0
i<n
if((j==0)
|| (i==j))
false
false
p[i][j]=1
p[i][j]=p[i-1][j-1]+p[i-1][j]
i++
True
k++
k=0
true
k<=n-i
False
j=0
Print
space
j++
true
j<=i
Print
p[i][j]
false
Print
newline
Stop
OUTPUT:
P a g e | 84
www.jntuworld.com || www.jwjobs.net
GEC
P a g e | 85
www.jntuworld.com || www.jwjobs.net
GEC
P a g e | 86
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
start
Read n
false
i=1
i++
i<=n
true
false
j=0
j++
j<=n-i
true
Print space
false
k=1
k++
k<=i
true
Print k
stop
P a g e | 87
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
P a g e | 88
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE : 13
a) To read in two numbers x and n and then compute the sum of this geometric progression
1+x+x2+x3+.+xn
DESCRIPTION:
In this program we have to read the two numbers and then calculate the sum of this geometric
progression in above mention.
ALGORITHM:
Step 1: Start
Step2: initialize sum=1
Step 3: read values of x and n
Step 4: if n<=0 || x<=0 otherwise go to step5
i)
P a g e | 89
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Initialize
sum=1
Read x,n
false
true
if
n<=0||x<=0
Print value
is valid
false
i=1
Print not
valid
i++
i<n
true
sum+=pow(x,i)
Print sum
Stop
P a g e | 90
www.jntuworld.com || www.jwjobs.net
GEC
Output:
Enter the values for x and n:4
4
Value is valid
Sum of series= 341
P a g e | 91
www.jntuworld.com || www.jwjobs.net
GEC
a)Write a C function to read in two numbers, x and n(no. of terms), and then compute sin(x)
and cos(x).
DESCRIPTION:
In this we calculate sin and cos values by reading x value and no.of terms n.
ALGORITHM:
Step 1: START
Step 2: initialize sum=o,term=0
Step 3: while true
Step 4: Read ch
Step 5: if ch!=3
Step 6: Read x,n
Step 7: compute x=(x*3.14)/180
Step 8: if ch==1 otherwise go to step 9
`
i) term=sum=x
ii) initialize i=1
iii) if i<=n otherwise go to step8 (iv)
i) term= ( (-term)*(x*x) ) / ((2*i)*(2*i+1));
ii) sum=sum+term, increment i
iv) Prnt Sin series sum
v) break go to step3
Step 9: if ch==2 otherwise go to step 10
i) term=sum=1
ii) initialize i=1
iii) if i<=n otherwise go to step9 (iv)
i) term= ( (-term)*(x*x) ) / ((2*i)*(2*i-1));
ii) sum=sum+term, increment i
iv) Prnt Cos series sum
v) break go to step3
Step 10: if ch==3 exit
Step 11: STOP
P a g e | 92
www.jntuworld.com || www.jwjobs.net
GEC
Flow chart:
Start
Initialize
sum=0,term=0
If true
Read ch
If(ch!=3)
true
Read x,n
x=(x*3.14)/180
false
If
ch==1
true
term=sum=x
B
i=1
i++
i<=n
true
A
A
P a g e | 93
Print sin
series sum
www.jntuworld.com || www.jwjobs.net
GEC
false
if
ch==2
if
ch==3
true
term=sum=1
false
i=1
exit
i++
i<=n
true
term= ( (-term)*(x*x) ) / ((2*i)*(2*i-1));
sum=sum+term
Print cos
series sum
stop
P a g e | 94
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
1.sin 2.cos 3.exit
1
Enter any number : 45
Enter no.of terms 5
The sin is 0.706825
P a g e | 95
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE : 14
a) Pointer based Function to exchange value of two integers using passing by address.
DESCRIPTION:
This program interchange values of two variable using functions and sending addresses.
ALGORITHM:
Step 1 : Start
Step 2 : Read num1,num2.
Step 3 : Display before swapping num1,num2.
Step 4 : Calling swap function.
Step 5 : Display after swapping.
Step 6 : Stop
Algorithm for swap:
Step 1 : start
Step 2 : assign t = *a;
Step 3 : assign *a =*b
Step 4 : assign *b = t
Step 5 : stop
P a g e | 96
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
start
Swap()
Stop
Subprogram:
void swap()
temp=*a
*a=*b,
*b=temp
main()
P a g e | 97
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter 2 Numbers to Swap:10 20
Before Swapping:
Two Numbers are:Num1=10 Num2=20
After Swapping:
Two Numbers are:Num1=20 Num2=10
Enter 2 Numbers to Swap:55 66
Before Swapping:
Two Numbers are:Num1=55 Num2=66
After Swapping:
Two Numbers are:Num1=66 Num2=55
P a g e | 98
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE : 14
a)Program which explains the use of dynamic arrays.
ALGORITHM
Step 1 : start
Step 2 : read n
Step 3 : allocate memory for n values of given type using malloc
Step 4 : assign 0 to i
Step 5 : if(i<n) go to step 6 else go to step 8
Step 6 : read a+i value
Step 7 : compute i = i+1 goto step 5
Step 8 : assign 0 to i
Step 9: if(i<n) go to step 10 else go to step 12
Step 10 : display *(a+i), (a+i)
Step 11 : increment i go to step9
Step 12: free the memory
Step13: allocate memory for n values of given type using calloc
Step 14: assign 0 to i
Step 15 : if(i<n) go to step16 else go to step 18
Step 16 : read a+i value
Step 17 : compute i = i+1 goto step 15
Step 18 : assign 0 to i
Step 19: if(i<n) go to step 20 else go to step 22
Step 20 : display *(a+i), (a+i)
Step 21 : increment i go to step19
Step 22: free the memory
Step 23 : Stop
P a g e | 99
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Read n
a=(int *) malloc(n*sizeof(int))
false
i=0
i++
i<n
true
Read (a+i)
false
i=0
i++
i<n
true
free(a)
a=(int *) calloc(n,sizeof(int))
false
i=0
i++
i<n
true
Read (a+i)
P a g e | 100
www.jntuworld.com || www.jwjobs.net
GEC
A
i=0
false
i++
i<n
true
free(a)
OUTPUT:
Enter no.of values :5
Stop
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE : 15
a) Program to display students information using structures.
DESCRIPTION:
This program demonstrates about structures.
ALGORITHM:
Step 1 : start
Step 2 : read n
Step 3 : initialize I =0
Step 4 : if(i<n) go to step 5 otherwise go to step 9
Step 5 : initialize total to 0
Step 6 : read student rollno, name, class, marks in 6 subjects
Step 7 : calculate total = sum of 6 subjects
Step 8 : compute i = i+1 goto step 4
Step 9 : initialize i =0
Step 10 : if(i<n) go to step 11 otherwise go to step 13
Step 11 : display Name student name
Display Roll no student rollno
Display class student class
Display total student total
Step 12 : compute i = i+1 , go to step 10
Step 13 : stop
P a g e | 102
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
START
STSTA
RT
Read number of
students n
false
i=0
i++
i<n
true
Initialize
s[i].total=0
j=0
j<6
false
j++
true
s[i].total + = s[i].m[j]
false
i=0
i++
i<n
true
Display student
details
STOP
P a g e | 103
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter number of students :2
Enter 1 student details :
Enter rollno,name, class, marks in 6 subjects
2
ramu
first
20
21
22
23
24
25
P a g e | 104
ramu
first
135
somu
second
201
www.jntuworld.com || www.jwjobs.net
GEC
P a g e | 105
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Stop
OUTPUT:
size of structure=11
size of union=4
Item=123
Cost=45.67
Code=s Size=4
P a g e | 106
www.jntuworld.com || www.jwjobs.net
GEC
EXERCISE: 16
a) Program which copies one file to another
DESCRIPTION:
In this program we have to use the file functions to perform the copy operation from one file to
another file.
ALGORITHM:
Step 1: Start
Step 2: read source file fname1, destination file fname2
Step 3: open source file in read mode
Step 4: open destination file in write mode
Step 5: if NULL pointer, then print unable to open files, go to step 8 otherwise go
to step6
Step 6: read a character from source file and write to destination file until EOF
Step 7: Close source file and destination file and print copied successfully
Step 8: Stop
P a g e | 107
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
if
fs==NULL ||
ft==NULL
false
if ch!=EOF
true
Print
Unable to open
false
true
ch=fgetc(fs);
fputc(ch,ft);
exit
fcloseall()
Stop
P a g e | 108
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
enter sourse file name1.c
enter dest file name2.c
File copy operation performed successfully
P a g e | 109
www.jntuworld.com || www.jwjobs.net
GEC
P a g e | 110
www.jntuworld.com || www.jwjobs.net
GEC
FLOW CHART:
Start
Read command
line args
Print invalid
no of args
true
If
argc!=3
false
true
fp=fopen(argv[1],r
If
fp==NULL
false
k=atoi(argv[2])
n = fread(a,1,k,fp)
a[n]='\0'
len=strlen(a)
i = len-1
s[j+1]=\0
false
i--
i >=0
true
s[j]=a[i]
Print s[j]
j++
Stop
P a g e | 111
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
C:\TC\PROGRAMS>reverse source.c 8
ollehiah
C:\TC\PROGRAMS>reverse source.c
Invalid number of arguments.
P a g e | 112
www.jntuworld.com || www.jwjobs.net
GEC
ADDITIONAL PROGRAMS:
EXCERCISE 1:
Program to find whether the given matrix is symmetric or not.
DESCRIPTION:
The transpose of a matrix is obtained by interchanging the row and column values of the matrix,
hence the order of the resultant matrix changes and therefore symmetry
ALGORITHM:
Step 1: start
Step 2: read the size of matrix
Step 3: read the elements of matrix
Step 4: perform the transpose operation by interchanging the row and
column values, the order of the resultant matrix
number of rows in transpose matrix=number of columns in the given matrix
number of columns in transpose matrix=number of rows in the given matrix
Step 6: Transpose is obtained through
at[i][j]=a[i][j]
Step 7: print the resultant transpose matrix at.
Step 8: check whether the given matrix is symmetric or not by comparing the
elements in the transpose matrix and the given matrix, if equal then goto
step 9 otherwise goto step 10.
Step 9: print the given matrix is symmetric
Step 10: print given matrix is not symmetric
Step 11: stop
P a g e | 113
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
start
Read elements of
matrix
s=symmetric(a,n)
true
if
s==1
Print
symmetric
false
Print not
symmetric
Stop
P a g e | 114
www.jntuworld.com || www.jwjobs.net
GEC
symmetric(a,n)
false
i=0
i++
i<n
true
j=0
false
j<n
j++
true
at[i][j]=a[j][i]
Print resultant
transpose matrix
false
i=0
i++
i<n
Return 1
true
j=0
false
j<n
j++
true
if a[i][j]!=
at[i][j]
false
c++
true
Return 0
Return to
main()
P a g e | 115
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter matrix size :2
Enter 4 elements :1
2
2
1
After Transpose :12
21
Symmetric
P a g e | 116
www.jntuworld.com || www.jwjobs.net
GEC
EXCERCISE 2:
Write a C program to arrange the given numbers in the ascending order.
DESCRIPTION:
To arrange the given numbers in the ascending order i.e, sorting the given numbers in the ascending
order.
ALGORITHM:
Step1: Start
Step2: Read n
Step3: initialize i=0
Step4: if i<n go to step5, otherwise go to step6
Step5: Read a[i], go to step4
Step6: initialize i=0
Step7: if i<n go to step8, otherwise go to step13
Step8: initialize j=0
Step9: if j<n go to step10, otherwise go to step12
Step10: if a[i]>a[j], otherwise go to step 11
i) temp=a[i];
ii) a[i]=a[j];
iii) a[j]=temp; go to step11
Step11: increment j, go to step9
Step12: increment i, go to step7
Step13: initialize i=0
Step14: if i<n go to step15, otherwise go to step16
Step15: Print a[i], go to step14
Step16: Stop
P a g e | 117
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Read n
i=0
false
i++
i<n
true
Read
a[i]
i=0
false
i++
i<n
true
j=0
false
j<n
j++
true
If
a[i]>a[j
]
true
false
temp=a[i]
a[i]=a[j]
a[j]=temp
i=0
false
i++
i<n
true
Print a[i]
Stop
P a g e | 118
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter number of elements :5
5
4
3
2
1
The 5 numbers in Ascending order :
1
2
3
4
5
P a g e | 119
www.jntuworld.com || www.jwjobs.net
GEC
EXCERCISE 3:
Write a c program to merge the contents of two files into one file.
DESCRIPTION:
To merge the contents of two files into the one file. This program uses the standard predefined
functions to perform this operation.
ALGORITHM:
Step1: Start
Step2: Read two files names
Step3: Read another file name to store content of two files
Step4: open two files in read mode
Step5: if fs1 == NULL || fs2 == NULL go to step 6, otherwise go to step 7
Step6: Print Error ,go to step 15
Step7: Read third file in write mode
Step8: if ft==NULL print Error go to step 15, otherwise go to step 9
Step9: if (ch = fgetc(fs1) ) != EOF ,go to step10 otherwise go to step11
Step10: fputc(ch,ft) go to step 9
Step11: if ( ch = fgetc(fs2) ) != EOF, go to step12 otherwise go to step13
Step12: fputc(ch,ft) go to step 11
Step13: Print two files are merged successfully
Step14: close three files
Step15: Stop
P a g e | 120
www.jntuworld.com || www.jwjobs.net
GEC
FLOWCHART:
Start
Read two
File names
fs1 = fopen(file1,"r");
fs2 = fopen(file2,"r");
if
fs1 == NULL ||
fs2 == NULL
true
Print Error
false
ft = fopen(file3,"w")
if
ft == NULL
false
true
Print Error
P a g e | 121
www.jntuworld.com || www.jwjobs.net
GEC
if
( ch = fgetc(fs1) )
!= EOF
false
if
( ch = fgetc(fs2) )
!= EOF
true
fputc(ch,ft)
false
true
fputc(ch,ft)
fclose(fs1)
fclose(fs2)
fclose(ft)
Stop
P a g e | 122
www.jntuworld.com || www.jwjobs.net
GEC
OUTPUT:
Enter name of first file 1.c
Enter name of second file 2.c
Enter name of file which will store contents of two files 4.c
Two files were merged into 4.c file successfully.
P a g e | 123
www.jntuworld.com || www.jwjobs.net
GEC