Vortex Mathematics & The Fibonacci Spiral: Unlocking The Fibonacci Sequence
EXERCISE: 1 - cse.gecgudlavalleru.ac.incse.gecgudlavalleru.ac.in/pdf/manuals/clab.pdf · I B.Tech C...
Transcript of EXERCISE: 1 - cse.gecgudlavalleru.ac.incse.gecgudlavalleru.ac.in/pdf/manuals/clab.pdf · I B.Tech C...
I B.Tech
C Programming Lab
P a g e | 1 Department of Computer Science & Engineering GEC
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
I B.Tech
C Programming Lab
P a g e | 2 Department of Computer Science & Engineering GEC
FLOW CHART:
OUTPUT:
Enter Temperature in Fahrenheit:52
Temperature in Celsius is: 11.200000
Enter Temperature in Celsius: 37
Temperature in Fahrenheit is: 98.600000
stop
Start
Read c
fa = 32+(c*1.8)
Display c
Read f
c=(fa-32)*0.56
Display fa
I B.Tech
C Programming Lab
P a g e | 3 Department of Computer Science & Engineering GEC
b)To find Student grading.
DESCRIPTION:
Find the grade of a student by reading marks.
ALGORITHM:
Step 1 : start
Step 2 : read 6 subjects marks
Step 3 : compute total and average of marks
Step 4 : if avg >= 80 then grade =A go to step 8
Ste p 5 : if avg >= 60 and avg <=80 then grade = B go to step 8
Step 6 : if avg >=40 and avg <=60 then grade = C go to step 8
Step 7 : if avg <40 then display failed
Step 8 : display grade.
Step 9 : stop
I B.Tech
C Programming Lab
P a g e | 4 Department of Computer Science & Engineering GEC
FLOW CHART:
yes
no no avg >80 avg >=60
and <=80
avg >=40
and <=60 Display fail
Display
grade =A
Display
grade =B
Display
grade = C
stop
no
start
Read marks
total=m1+m2+m3+m4
+m5+m6;
avg=total/6;
yes yes
I B.Tech
C Programming Lab
P a g e | 5 Department of Computer Science & Engineering GEC
OUTPUT:
Enter your marks 36
36
36
36
36
36
You Failed in this exam
Enter your marks 60
70
35
69
35
69
You got C grade
I B.Tech
C Programming Lab
P a g e | 6 Department of Computer Science & Engineering GEC
c) Program for income tax for calculation.
ALGORITHM:
Step 1: start
Step 2: read income
Step 3: if income >0 and <200000 display “No tax” otherwise go to step 4
Step 4: if income > 200001 and < 500000 then tax = income *0.1 go to step 7 otherwise go to step 5
Step 5: if income > 500001 and < 100000 then tax = income*0.2 go to step 7 otherwise go to step 6
Step6: tax=income*0.3
Step 7: display tax
Step 8: stop
I B.Tech
C Programming Lab
P a g e | 7 Department of Computer Science & Engineering GEC
FLOWCHART:
OUTPUT:
Enter Income: 500000
INCOME TAX AMOUNT= 50000.000000
REMAINING INCOME= 450000.000000
Enter Income: 225000
INCOME TAX AMOUNT= 22500.000000
REMAINING INCOME= 202500.000000
no
yes yes
yes
no no
tax = income *
0.3
start
Read income
income >
0 and <
200000
income >
200001 and
<500000
income >
500001 and
< 1000000
Display
No tax
Display tax
stop
tax = income*0.1
tax = income*0.2
Display tax Display tax
I B.Tech
C Programming Lab
P a g e | 8 Department of Computer Science & Engineering GEC
EXERCISE: 2
To convert the given binary number to 2’s 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 2‟s 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
I B.Tech
C Programming Lab
P a g e | 9 Department of Computer Science & Engineering GEC
FLOWCHART:
OUTPUT:
Enter Binary Number: 101010
Its 2's Complement Is: 010110
Enter Binary Number: 11111
Its 2's Complement Is: 00001
true
true
false
true
false
false
true
false
i = n i--
i >=0
j = i-1 j>=0
j--
Start
n= strlen(b)-1
Read a binary
number
If
b[i]==‟1‟
Stop
if b[j]==‟1‟
b[j] = „0‟ b[j] = „1‟
Display 2‟s
complement
of number
I B.Tech
C Programming Lab
P a g e | 10 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 11 Department of Computer Science & Engineering GEC
FLOW CHART:
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
FALSE TRUE
TRUE
START
IF
N! =0
s=0
Read N
IF N < 0
Print S
STOP P=N%10
S=S+P
N=N/10
I B.Tech
C Programming Lab
P a g e | 12 Department of Computer Science & Engineering GEC
b) To print the Fibonacci series for 1 to n value
DESCRIPTION:
A Fibonacci series is defined as follows
The first term in the sequence is 0
The second term in the sequence is 1
The sub sequent terms 1 found by adding the preceding two terms in the sequence
Formula: let t1,t2,…………tn be terms in Fibonacci sequence
t1=0, t2=1
tn=tn-2+tn-1……where n>2
ALGORITHM:
Step 1: start
Step 2: initialize the a=0, b=1
Step 3: read n
Step 4: if n== 1 print a go to step 7. else goto step 5
Step 5: if n== 2 print a, b go to step 7 else print a,b
Step 6: initialize i=3
i) if i<= n do as follows. If not goto step 7
c=a+b
print c
a=b
b=c
increment i value
goto step 6(i)
Step 7: stop
I B.Tech
C Programming Lab
P a g e | 13 Department of Computer Science & Engineering GEC
FLOWCHART:
OUTPUT:
enter n value5
0 1 1 2 3
enter n value10
0 1 1 2 3 5 8 13 21 34
True False
False True
False
Start
Initialize
a=0,b=1
Read n
If n==1
Output a If n ==2
Output a,b Output a,b
i=3 i++
i<=n
c =a + b
Output c
a = b
b = c
Stop
true
I B.Tech
C Programming Lab
P a g e | 14 Department of Computer Science & Engineering GEC
c) To print prime numbers up to 1 to n
DESCRIPTION:
Prime number is a number which is exactly divisible by one and itself only
Ex: 2, 3,5,7,………;
ALGORITHM:
Step 1: start
Step 2: read n
Step 3: initialize i=1,c=0
Step 4:if i<=n
Initialize c=0 go to step 5
If not go to step 10
Step 5: initialize j=1
Step 6: if j<=i do the following. If no goto step 7
i)if i%j==0 increment c otherwise increment j go to step 6
ii) increment j
iii) goto Step 6
Step 7: if c== 2 print i
Step 8: increment i
Step 9: goto step 4
Step 10: stop
I B.Tech
C Programming Lab
P a g e | 15 Department of Computer Science & Engineering GEC
true
false
false
true
true
false
j = 1 j++
j<=i
if c==2
if i % j == 0
c ++
Display i
Start
Read n
i = 1 i++
i<=n
stop
c=0
false
true
FLOW CHART:
OUTPUT:
Enter Value of n:5
Prime Numbers upto 5 are:
2 3 5
Enter Value of n:9
Prime Numbers up to 9 are:2 3 5 7
I B.Tech
C Programming Lab
P a g e | 16 Department of Computer Science & Engineering GEC
c) To check a given integer is Fibonacci number or not.
DESCRIPTION :
Find the given number is Fibonacci or not. It means if the given number is present in the Fibonacci
series it satisfies the condition otherwise it fails.
ALGORITHM:
Step 1: start
Step 2: initialize a=0 , b=1
Step 3 : compute c=a+b
Step 4 : read n
Step 5 : if c<=n go to step 6 otherwise goto step 12
Step 6 : compute c = a+b
Step 7 : a=b
Step 8 : b = c goto step 5
Step 9 : if n==c or n==0 or n==1 goto step 10 otherwise goto step 11
Step 10 : display “ given number is Fibonacci “ n
Step 11 : display “ given number is not Fibonacci “ n
Step 12 : stop
I B.Tech
C Programming Lab
P a g e | 17 Department of Computer Science & Engineering GEC
FLOWCHART:
OUTPUT:
Enter no to find: 21
21 is Fibonacci
Enter no to find: 25
25 is not Fibonacci
START
a=0,b=1
Read n
if c< n if n ==0 or
n==1 or
n ==c
Display n is
Fibonacci
number
Stop
c = a+b
c= a+b
a=b, b=c
Display n is not
a Fibonacci
number
no no
yes
yes
I B.Tech
C Programming Lab
P a g e | 18 Department of Computer Science & Engineering GEC
EXERCISE: 4
a) To calculate the sum. Sum=1-x2/2! + x
4/4! - x
6/6! + x
8/8! - x
10/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
I B.Tech
C Programming Lab
P a g e | 19 Department of Computer Science & Engineering GEC
true
false
false
Start
Read x,n
f= 1
i=1, i<=n
i++
Display sum
Stop
Initialize Sum = 0.0,
p=0
j=1, j<=p
j++
f= f*j
if i%2
== 0
sum=sum-
(pow(x,p)/f)
sum=sum+
(pow(x,p)/f)
p = p+2
true
true
false
FLOW CHART:
I B.Tech
C Programming Lab
P a g e | 20 Department of Computer Science & Engineering GEC
OUTPUT:
Enter Number of Terms in the Series:3
Enter x value:1
The sum of the Series upto 3 terms: 0.541667
Enter Number of Terms in the Series:3
Enter x value:2
The sum of the Series upto 3 terms: -0.333333
I B.Tech
C Programming Lab
P a g e | 21 Department of Computer Science & Engineering GEC
b) To find the roots of the quadratic equation
DESCRIPTION:
Nature of roots of quadratic equation can be known from the quadrant = b2-4ac
If b2-4ac >0 then roots are real and unequal
If b2-4ac =0 then roots are real and equal
If b2-4ac <0 then roots are imaginary
ALGORITHM:
Step 1: start
Step 2: read the a,b,c value
Step3: assign pow((b*b-4*a*c),0.5) value to d
Step4: if d==0 otherwise go to step5
Compute r1=-b/(2*a)
r2=-b/(2*a)
go to step 7
Step5: if d<0 print roots are imaginary otherwise go to step6
Step6: compute r1 = ((-b+d) / (2*a))
r2 = ((-b-d) / (2*a))
go to step 7
Step7: print r1, r2
Step8: stop
I B.Tech
C Programming Lab
P a g e | 22 Department of Computer Science & Engineering GEC
true
false
false
Start
Read a,b,c
d = pow((b*b-4*a*c),0.5)
If d < 0
r1 = ((-b+d) / (2*a))
r2 = ((-b-d) / (2*a))
Display
r1, r2
Stop
If d== 0
r1=-b / (2 * a )
r2= -b / (2 * a)
Display
roots are
imaginary
true
FLOWCHART:
I B.Tech
C Programming Lab
P a g e | 23 Department of Computer Science & Engineering GEC
OUTPUT:
Enter the values of a,b,c:1 6 9
Roots are Equal:-3.000000,-3.000000
Enter the values of a,b,c:2 7 6
Roots are:-6.750000,-7.250000
Enter the values of a,b,c:1 2 3
Roots are Imaginary
I B.Tech
C Programming Lab
P a g e | 24 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 25 Department of Computer Science & Engineering GEC
FLOWCHART:
true
false
start
Read u, a, t
d= (u*t)+(0.5*a*t*t)
Display distance as d
Read ch
If
ch==1
stop
I B.Tech
C Programming Lab
P a g e | 26 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 27 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 28 Department of Computer Science & Engineering GEC
FLOWCHART:
false
false
false
false
false
true
true
true
true
true
start
Read
op1,op2, op
If op
==‟+‟
If op
==‟-‟
r=op1+op2
r=op1-op2
If op
==‟*‟
If op
==‟/‟
If op
==‟%‟
r=op1*op2
r=op1/op2
r=op1%op2
Display r
stop
Display
invalid
operation
I B.Tech
C Programming Lab
P a g e | 29 Department of Computer Science & Engineering GEC
OUTPUT:
Operations Performed are: + - * / %
Enter 2 Integers & an Operator:2
4
*
MULTIPLICATION -- Product is 8
Operations Performed are: + - * / %
Enter 2 Integers & an Operator:2
4
+
ADDITION -- Sum is 6
Operations Performed are: + - * / %
Enter 2 Integers & an Operator:2
4
-
SUBTRACTION -- Difference is -2
Operations Performed are: + - * / %
Enter 2 Integers & an Operator:2
4
/
DIVISION -- Quotient is 0
Operations Performed are: + - * / %
Enter 2 Integers & an Operator:2
4
%
MODULUS -- Remainder is 2
I B.Tech
C Programming Lab
P a g e | 30 Department of Computer Science & Engineering 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 1st string is “, strrev(s1)
Step 12: display “after concatenation of two strings s1 and s2 is “ strcat(s1,s2)
Step 13: stop
I B.Tech
C Programming Lab
P a g e | 31 Department of Computer Science & Engineering GEC
Flowchart:
true
start
Read strings
s1,s2,s3
l1=strlen(s1)
Display l1
s=strcpy(s3,s1)
Display s1,s2
are equal
e=strcmp(s1,s2)
If
e==0
Display s
Display s1,s2 are not equal
r=strrev(s1)
Display r
c=strcat(s1,s2)
Display c
stop
false
I B.Tech
C Programming Lab
P a g e | 32 Department of Computer Science & Engineering 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
Enter three strings gec
gec
gudlavalleru
length of s1 is 3
After copying 1st string into 3rd string is gec
The two strings s1 and s2 are equal
Reverse of first string is ceg
After adding first two strings ceggec
I B.Tech
C Programming Lab
P a g e | 33 Department of Computer Science & Engineering GEC
b) Verifying a string for its palindrome property.
DESCRIPTION:
Read a string, compare first and last characters of a string, if equal continue up to middle of the
string. If the comparison fails at any character the string is not a palindrome otherwise palindrome
property satisfies.
Algorithm:
Step 1: start
Step 2: read the string s1
Step 3: store reverse of the given string s1 in a temporary string s2
Step 4: compare the two strings s1 and s2
Step 5: if both are equal then print the given string is palindrome
Step 6: otherwise print the given string is not palindrome
Step 7: stop
FLOWCHART:
true
false
start
Read string s1
strcpy(s2,s1)
strrev(s2)
p=strcmp(s1,s2)
if p
== 0
The given
string s1 is
palindrome
The given
string s1 is not
palindrome
stop
I B.Tech
C Programming Lab
P a g e | 34 Department of Computer Science & Engineering GEC
OUTPUT:
Enter a string: madam
The given string is palindrome
Enter a string : manual
The given string is not palindrome
I B.Tech
C Programming Lab
P a g e | 35 Department of Computer Science & Engineering GEC
c) copy one string to another string
DESCRIPTION:
Copying one string to another string
ALGORITHM:
step1: start
step2: read a string s1
step3: initialize i=0
step4: if s1[i]!=‟\0‟ otherwise s2[i]=‟\0‟
s2[i]=s1[i]
increment i
step5: print s2
step6: stop
FLOWCHART:
false
true
start
Read a string
s1
i=0 s1[i]!=‟\0‟
i++
s2[i]=s1[i]
s2[i]=‟\0‟
Display s2
stop
I B.Tech
C Programming Lab
P a g e | 36 Department of Computer Science & Engineering GEC
OUTPUT:
Enter a string : malayalam
The copied string is malayalam
Enter a string : haihello
The copied string is haihello
I B.Tech
C Programming Lab
P a g e | 37 Department of Computer Science & Engineering GEC
d) calculate the length of the given string
DESCRIPTION:
Calculating the length of the given string
ALGORITHM:
Step 1:start
Step 2: initialize sum=0
Step3: read the string s1
Step 4:initialize i=0
Step5: if s1[i]!=‟\0‟ otherwise go to step6
increment sum
increment i
step6: print sum
step7: stop
FLOWCHART:
true
Start
Initialize sum = 0
Read
string s1
i=0 s1[i]!=‟\0‟
i++
sum ++
stop
Display
sum
false
I B.Tech
C Programming Lab
P a g e | 38 Department of Computer Science & Engineering GEC
OUTPUT:
Enter a string : gudlavalleruengineeringcollege
30
Enter a string : string
6
I B.Tech
C Programming Lab
P a g e | 39 Department of Computer Science & Engineering GEC
e) To reverse the given string
DESCRIPTION:
Reverse a given string
ALGORITHM:
Step1: start
Step2: initialize sum=0
Step3: read string s1
Step4: initialize i=0
Step5: if s1[i]!=‟\0‟ otherwise go to step6
(i) increment sum
(ii) increment i
Step6: initialize i=0,j=sum-1
Step7: if s1[i]!=‟\0‟and j>=0 otherwise go to step8
(i) rev[j]=s[i];
(ii) increment i
(iii) decrement j
Step8: rev[j]=‟\0‟
Step9: stop
I B.Tech
C Programming Lab
P a g e | 40 Department of Computer Science & Engineering GEC
FLOW CHART:
false
true
start
Initialize sum = 0
Read
string s1
i=0 s1[i]!=‟\0‟
i++
sum ++
stop
Display
rev
i=0, j=sum-1 s1[i]!=‟\0‟, j>=0
i++,j--
rev[j]=s[i]
rev[j]=‟\0‟
true
false
I B.Tech
C Programming Lab
P a g e | 41 Department of Computer Science & Engineering GEC
OUTPUT:
Enter a string : sai
Reverse of the given string is ias
Enter a string : cprogramming
Reverse of the given string is gnimmargorpc
I B.Tech
C Programming Lab
P a g e | 42 Department of Computer Science & Engineering 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
Read the strings S1,
S2, position i
Insert (s1,s2,i)
Start
I B.Tech
C Programming Lab
P a g e | 43 Department of Computer Science & Engineering GEC
Yes
No
Yes
No
Yes
No
Insert (s1, s2, x)
i=0 i++,c++
s1[ i]!=\0
if i==x
j=0 j++,c++
s2 [j]! =\0
s[c] =s2[j]
s[c]='\0'
string s
Return to main()
s[c]=s1[i]
I B.Tech
C Programming Lab
P a g e | 44 Department of Computer Science & Engineering 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
Enter the Original String: gudlavalleru
Enter the subString: enggcollege
Enter the index where you want to insert the subString: 5
The String now is:
------------------
gudlaenggcollegevalleru
I B.Tech
C Programming Lab
P a g e | 45 Department of Computer Science & Engineering GEC
b) To delete n characters from a given position in a given string
DESCRIPTION:
In this program we need to delete a string from the given string at a specified position.
ALGORITHM:
Step 1: start
Step 2: read string
Step 3: find the length of the string
Step 4: read the value of number of characters to be deleted and positioned
Step 5: string copy part of string from position to end, and (position+number of
characters to end)
Step 6: stop
I B.Tech
C Programming Lab
P a g e | 46 Department of Computer Science & Engineering GEC
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
Start
Read string
Read position, no of
characters
delchar( string, n, pos)
Stop
delchar( )
if ((a+b-1)<=
strlen(x))
Strcpy(&x[b-1],&x[a+b-1])
Print x
Return to
main()
FLOW CHART:
I B.Tech
C Programming Lab
P a g e | 47 Department of Computer Science & Engineering GEC
c) To replace a character of string either from beginning or ending or at a specified location.
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
FLOW CHART:
stop
false
switch
opt
case e case b
case p
s1[0] = ch; s1[l-1] = ch;
s1[pos]= ch;
pos >=0
and pos
<=l-1
Not match
Print string
true
start
Read string
Read replace
character
chara
I B.Tech
C Programming Lab
P a g e | 48 Department of Computer Science & Engineering GEC
OUTPUT:
Enter string :cplab
Enter character to replace :n
Enter b-begin, e-ending , p-position b
after replacing string is nplab
Enter string :clab
Enter character to replace :w
Enter b-begin, e-ending , p-position e
after replacing string is claw
Enter string :cprogramming
Enter character to replace :l
Enter b-begin, e-ending , p-position p
Enter postion to replace 1
after replacing string is clrogramming
I B.Tech
C Programming Lab
P a g e | 49 Department of Computer Science & Engineering GEC
EXERCISE : 8
Write a C program that uses functions to perform the following operations using Structure:
i) Reading a complex number ii) Writing a complex number
iii) Addition of two complex numbers iv) Multiplication of two complexnumbers
DESCRIPTION:
In this program the complex number means it contains the two parts . first one is real part and
second one is imaginary part(2+3i).by taking these two complex numbers we can perform the
addition and multiplication operation.
ALGORITHM:
Step 1: Start
Step 2: declare structure for complex numbers and perform the following
operations
Step 3: read choice
Step 4: if choice=1 then read the complex numbers
Step 5: if choice=2 then write the complex numbers
Step 6: if choice=3 then addition operation will perform and it contains following
steps
i) c3.real = c1.real+c2.real;
ii) c3.img = c1.img+c2.img;
print the result by calling write() function then goto step 3
Step 6: if choice=4 then multiplication operation will perform and it contains
following steps
i) c4.real=(c1.real*c2.real)-(c1.img*c2.img);
ii) c4.img=(c1.real*c2.img)+(c1.img*c2.real);
print the result by calling write() function then goto step 3
Step 7: if choice=5 then exit
Step 9: Stop
I B.Tech
C Programming Lab
P a g e | 50 Department of Computer Science & Engineering GEC
case exit
Start
Declare structure
Read complex
numbers
switch
choice
case read
c3.real=c1.real+c2.real
c4.img=c1.img+c2.img
c4.real=(c1.real*c2.real)-(c1.img*c2.img)
c4.img=(c1.real*c2.img)+(c1.img*c2.real)
Stop
Read choice
Display the
result
case mul case add
case write
FLOW CHART:
I B.Tech
C Programming Lab
P a g e | 51 Department of Computer Science & Engineering GEC
OUTPUT:
1:Reading 2:Writing 3:Addition 4:Multiplication 5:Exit
enter your choice:1
enter real and imaginary parts of first complex number: 2 3
enter real and imaginary parts of second complex number: 4 5
1:Reading 2:Writing 3:Addition 4:Multiplication 5:Exit
enter your choice:2
answer=2.000000+3.000000i
answer=4.000000+5.000000i
1:Reading 2:Writing 3:Addition 4:Multiplication 5:Exit
enter your choice:3
answer=6.000000+8.000000i
1:Reading 2:Writing 3:Addition 4:Multiplication 5:Exit
enter your choice:4
answer= -7.000000+22.000000i
1:Reading 2:Writing 3:Addition 4:Multiplication 5:Exit
enter your choice:5
.
I B.Tech
C Programming Lab
P a g e | 52 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 53 Department of Computer Science & Engineering GEC
FLOWCHART:
true
true
false
false
true
false
i=0 i++
i<r1
j=0 j<c1
j++
start
Read matrices A and B
stop
Read number of rows
r1 and number of
columns c1 of matrix 1
Read number of rows
r2 and number of
columns c2 of matrix 2
If r1!=r2
and c1!=c2
c[i][j]=a[i][j]+b[i][j];
Print result matrix
Display
invalid order
of matrices
I B.Tech
C Programming Lab
P a g e | 54 Department of Computer Science & Engineering 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 4
4 4
Enter Number of rows & columns for Matrix1:3
2
Enter Number of rows & columns for Matrix2:2
3
Invalid order of Matrices
I B.Tech
C Programming Lab
P a g e | 55 Department of Computer Science & Engineering GEC
b) Calculating transpose of a matrix in-place manner.
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.
ALGORITHM:
Step 1: start
Step 2: read the size of matrix A
Step 3: read the elements of matrix A
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: stop
I B.Tech
C Programming Lab
P a g e | 56 Department of Computer Science & Engineering GEC
FLOWCHART:
OUTPUT:
Enter Number of Rows: 2
Enter Number of Columns: 3
Enter 6 Elements: 2
3
4
5
6
7
false
true
true
i=0 i++
i<c
Read elements of
matrix
Read rows and
columns of matrix
j=0 j<r
j++
at[i][j]=a[j][i]
stop
Print resultant transpose matrix
start
false
I B.Tech
C Programming Lab
P a g e | 57 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 58 Department of Computer Science & Engineering GEC
c) Matrix multiplication by checking compatibility
DESCRIPTION:
Takes the two matrixes of different sizes and checks for possibility of multiplication and perform
multiplication if possible.
ALGORITHM:
Step 1: start
Step 2: read the size of matrices A,B
Step 3: check compatibility of matrices for multiplication i.e, number of columns
in the first matrix should be equal to number of rows in the second matrix.
Step 4: read the elements of matrix A
Step 5: read the elements of matrix B
Step 6: perform the multiplication operation by storing the resulting values into
matrix C.
Step 7: print the resultant matrix C.
Step 8: Stop
I B.Tech
C Programming Lab
P a g e | 59 Department of Computer Science & Engineering GEC
FLOWCHART:
true
true
false
true
false
false
true
i=0 i++
i<r1
j=0 j<c2
j++
start
Read matrices A and B
stop
Read number of rows
r1 and number of
columns c1 of matrix
A
Read number of rows
r2 and number of
columns c2 of matrix B
if c1!=r2
c[i][j]=c[i][j]+(a[i][k]*b[k
][j])
Print result matrix
Display
invalid order
of matrices
c[i][j]=0
k=0 k<c1
k++
false
I B.Tech
C Programming Lab
P a g e | 60 Department of Computer Science & Engineering 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:
8 8
8 8
I B.Tech
C Programming Lab
P a g e | 61 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 62 Department of Computer Science & Engineering GEC
true
Start
Read n
Call subprogram
print f
Stop
Sub program
fact()
If n=0 || n=1
Return
n*fact(n-1)
Return to main
program
false
f = fact(n)
Return 1
FLOWCHART:
OUTPUT:
enter the number :7
factorial of number 7 is 5040
enter the number :5
factorial of number 5 is 120
I B.Tech
C Programming Lab
P a g e | 63 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 64 Department of Computer Science & Engineering GEC
false
i= 1 i++
i<=n
start
Read n
Call subprogram
Fact(n)
Print F value
Stop
Fact (n)
Initialize F = 1
if n == 0 ||
n == 1
Return to main program
F = F * i
Return F
Return 1
true
Subprogram Flowchart
FLOWCHART:
OUTPUT:
enter the number :5
factoria of 5 is 120
enter the number :7
factoria of 7 is 5040
I B.Tech
C Programming Lab
P a g e | 65 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 66 Department of Computer Science & Engineering GEC
FLOW CHART:
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
true false
gcdrec( )
If n==0
Return m Call the same function
Return gcdrec(n,m%n)
Return to main()
Start
Read a,b
Call sub program
G=gcdrec(a,b)
Print gcdvalue
Stop
Subprogram Flowchart
I B.Tech
C Programming Lab
P a g e | 67 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 68 Department of Computer Science & Engineering GEC
false true
start
Read a, b
Call subprogram
gcdnonrec(a,b)
Output g
stop
gcdnonrec(m,n)
r=m%n
if r!=0
m=n,
n=r,
r=m%n
Return n
Return to main program
Subprogram Flowchart
FLOWCHART:
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
I B.Tech
C Programming Lab
P a g e | 69 Department of Computer Science & Engineering GEC
c).To solve the towers of Hanoi problem by using the recursive function
DESCRIPTION:
Towers of Hanoi problem means we have three towers
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
Source intermediate destination
I B.Tech
C Programming Lab
P a g e | 70 Department of Computer Science & Engineering GEC
false true
Print A,C Return to main
program
Hanoirec(num,n1,n2,n3)
if num==1
Print A,B
Call subprogram
Hanoirec(num-1,n3,n2,n1)
Call sbgroram
Hanoirec(num-1,n1,n3,n2)
Start
Assume Source = A
Intermediate = C
Destination = B
READ no
Call subprogram
Hanoirec(no,source,intermediate,destination)
Stop
Sub program
FLOWCHART:
I B.Tech
C Programming Lab
P a g e | 71 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 72 Department of Computer Science & Engineering 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
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
FLOW CHART:
Source intermediate destination
false x=1 x++
x < (1 << n)
Start
Read no of
disks
Print (x&x-1)%3,
((x|x-1)+1)%3
Stop
true
I B.Tech
C Programming Lab
P a g e | 73 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 74 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 75 Department of Computer Science & Engineering GEC
false
true
true
true
false
true false
Read n
min = a[0]
max = a[0]
Print min,
max
i = 0 i++
i< n
if a[i]< min
a[i]>max min=a[i]
max=a[i]
i= 0
i<n i++
stop
Read a[i]
Start
FLOWCHART:
I B.Tech
C Programming Lab
P a g e | 76 Department of Computer Science & Engineering GEC
OUTPUT:
enter the array size:5
Enter the elements of array50
40
30
20
10
maximum value is:50
minimum value is:10
enter the array size:8
Enter the elements of array21
-31
54
65
76
89
90
23
maximum value is:90
minimum value is:-31
I B.Tech
C Programming Lab
P a g e | 77 Department of Computer Science & Engineering GEC
b) Program that displays the position or index in the string S where the string T begins , or -
1 if S doesn’t 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
I B.Tech
C Programming Lab
P a g e | 78 Department of Computer Science & Engineering GEC
FLOW CHART:
no yes
Start
Read first string s
Read string t to be searched
Found=strstr(s,t)
If found
Print -1 Print string is
found
Stop
I B.Tech
C Programming Lab
P a g e | 79 Department of Computer Science & Engineering 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.
Enter the first string:
kangaroo
Enter the string to be searched:
rrr
-1
I B.Tech
C Programming Lab
P a g e | 80 Department of Computer Science & Engineering GEC
c) To count the lines, words & characters in a given text
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
I B.Tech
C Programming Lab
P a g e | 81 Department of Computer Science & Engineering GEC
false
true
true
false
false
false
true
Start
Initialize end=0,chars=0,words=0,lines=0
if
end!=0
c= 0
true
if
(ctr=getchar())==‟\n‟
line[c]=‟\0‟
line[c++]=ctr
if
line[0]=‟\0‟
Words ++ i = 0
i ++ line[i]!=‟\0‟
If line[i]==‟ „||
line[i]==‟\t‟
lines++
chars+=strlen(line)
Words ++
Print lines,
Words, chars
stop
true
FLOW CHART:
I B.Tech
C Programming Lab
P a g e | 82 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 83 Department of Computer Science & Engineering GEC
EXERCISE : 12
a) To generate Pascal’s triangle
DESCRIPTION:
Pascal‟s 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
I B.Tech
C Programming Lab
P a g e | 84 Department of Computer Science & Engineering GEC
OUTPUT:
i=0 i<n
i++
true
False
True
true
true
true
false
Start
Read n
i=0 i< n
i++
j=0 j ++
j<=i
true
false
false
p[i][j]=1
p[i][j]
space
Stop
if((j==0)
|| (i==j))
p[i][j]=p[i-1][j-1]+p[i-1][j]
k=0 k++
k<=n-i
j=0 j++
j<=i
newline
false
false
FLOW CHART:
I B.Tech
C Programming Lab
P a g e | 85 Department of Computer Science & Engineering GEC
Enter no of lines to print: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Enter no of lines to print: 8
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
I B.Tech
C Programming Lab
P a g e | 86 Department of Computer Science & Engineering GEC
b) To construct a pyramid of numbers
DESCRIPTION:
In this program the we have to construct output in the pyramid shape manner
ALGORITHM:
Step 1: start
Step 2: read no of lines n
Step 3: initialize i=1
Step 4: if i<=n otherwise go to step 9
Step 5: initialize j=0
Step6: if j<=n-1 then print “ “ and increment j otherwise go to step 7
Step 7: initialize k=1
Step8: if k<=i then print k and increment k otherwise print new line, increment i, go to
step 4
Step 9: stop
I B.Tech
C Programming Lab
P a g e | 87 Department of Computer Science & Engineering GEC
FLOW CHART:
true
true
false
start
Read n
i=1 i++
i<=n
Print space
Print k
k=1 k++
k<=i
stop
j=0 j++
j<=n-i
Print new line
false
false
true
I B.Tech
C Programming Lab
P a g e | 88 Department of Computer Science & Engineering GEC
OUTPUT:
Enter number of lines to print :6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Enter number of lines to print :8
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
I B.Tech
C Programming Lab
P a g e | 89 Department of Computer Science & Engineering 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+……….+x
n
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) print values are not valid go to step8
Step 5: print value is valid
Step6: initialize i=1
Step7: if i<n otherwise print sum go to step8
i) sum+=pow(x,n) and increment i go to step7
Step8: Stop
I B.Tech
C Programming Lab
P a g e | 90 Department of Computer Science & Engineering GEC
FLOWCHART:
false
Start
Initialize
sum=1
Read x,n
if
n<=0||x<=0
Print not
valid i=1 i++
i<n
sum+=pow(x,i)
Print sum
Stop
true false
true
Print value
is valid
I B.Tech
C Programming Lab
P a g e | 91 Department of Computer Science & Engineering GEC
Output:
Enter the values for x and n:4
4
Value is valid
Sum of series= 341
Enter the values for x and n:3
9
Value is valid
Sum of series= 29524
I B.Tech
C Programming Lab
P a g e | 92 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 93 Department of Computer Science & Engineering GEC
true
true
false
Start
Initialize
sum=0,term=0
Read x,n
If true
1.sin 2.cos 3.exit
If(ch!=3)
If
ch==1
true
i=1 i++
i<=n
Read ch
x=(x*3.14)/180
term=sum=x
term= ( (-term)*(x*x) ) / ((2*i)*(2*i+1));
sum=sum+term
B
A
C D
Flow chart:
Print sin
series sum
A
I B.Tech
C Programming Lab
P a g e | 94 Department of Computer Science & Engineering GEC
true
true
false
false
i=1 i++
i<=n
if
ch==2
term=sum=1
term= ( (-term)*(x*x) ) / ((2*i)*(2*i-1));
sum=sum+term
Print cos
series sum
B
D
stop
if
ch==3
exit
I B.Tech
C Programming Lab
P a g e | 95 Department of Computer Science & Engineering GEC
OUTPUT:
1.sin 2.cos 3.exit
1
Enter any number : 45
Enter no.of terms 5
The sin is 0.706825
1.sin 2.cos 3.exit
1
Enter any number : 90
Enter no.of terms 5
The sin is 1.000003
1.sin 2.cos 3.exit
2
Enter any number : 45
Enter no.of terms 5
The cos is 0.707388
1.sin 2.cos 3.exit
2
Enter any number : 90
Enter no.of terms 5
The cos is 0.000821
1.sin 2.cos 3.exit
3
I B.Tech
C Programming Lab
P a g e | 96 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 97 Department of Computer Science & Engineering GEC
FLOWCHART:
Subprogram:
void swap()
temp=*a
*a=*b,
*b=temp
main()
start
After Swaping Print
num1,num2
Read num1, num2
Swap()
Stop
Before Swaping Print
num1,num2
I B.Tech
C Programming Lab
P a g e | 98 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 99 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 100 Department of Computer Science & Engineering GEC
FLOWCHART:
true
true
true
false
false
false
i=0 i++
i<n
i=0 i++
i<n
Start
Read n
a=(int *) malloc(n*sizeof(int))
Read (a+i)
Print *(a+i), (a+i)
free(a)
Read (a+i)
a=(int *) calloc(n,sizeof(int))
A
i=0 i++
i<n
I B.Tech
C Programming Lab
P a g e | 101 Department of Computer Science & Engineering GEC
OUTPUT:
Enter no.of values :5
enter 5 elements :50
40
30
20
10
The given elements are :
50 07B0
40 07B2
30 07B4
20 07B6
10 07B8
enter 5 elements :5
4
3
2
1
The given elements are :
5 07B0
4 07B2
3 07B4
2 07B6
1 07B8
true
false
Print *(a+i), (a+i)
i=0 i++
i<n
A
free(a)
Stop
I B.Tech
C Programming Lab
P a g e | 102 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 103 Department of Computer Science & Engineering GEC
FLOWCHART:
true
true
true
false
false
START
STSTA
RT
Initialize
s[i].total=0
Read number of
students n
i=0 i++
i<n
Read roll num, name, class,
6 subjects marks
j=0 j<6
j++
s[i].total + = s[i].m[j]
Display student
details
i=0 i++
i<n
STOP
false
I B.Tech
C Programming Lab
P a g e | 104 Department of Computer Science & Engineering 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
Enter 2 student details :
Enter rollno,name, class, marks in 6 subjects
2
somu
second
31
32
33
34
35
36
The student‟s details:
NAME ROLLNO CLASS TOTALMARKS
ramu 1 first 135
somu 2 second 201
I B.Tech
C Programming Lab
P a g e | 105 Department of Computer Science & Engineering GEC
b) Program to demonstrate the concept of unions.
DESCRIPTION
This program demonstrates the concept of unions clearly by nesting the union withing the structure
and showing the difference between structures and unions.
ALGORITHM
Step 1 : start
Step 2 : declare a structure.
Step 3 : Now declare a union nested within the structure.
Step 4 : create objects part1,part2 for the union and object obj for the structure.
Step 5 : assign the values for the members of both union and structure.
Step 6 : display the size of the structure and union inorder to see the difference between
the memory allocations of union and structure.
Step 7 : print the values of the members of union and structure using dot operator
obj.member
Step 8 : compute i = i+1 goto step 4
Step 9 : stop
I B.Tech
C Programming Lab
P a g e | 106 Department of Computer Science & Engineering GEC
FLOWCHART:
OUTPUT:
size of structure=11
size of union=4
Item=123 Cost=45.67
Code=s Size=4
Assign values for
members
obj.part1.item=123;
obj.part2.cost=45.67;
obj.code='s';
obj.size=4;
print sizeof(obj)
sizeof(obj.part1)
obj.part1.item,obj.part2.cost,
obj.code, obj.size values
Stop
Start
Declare a union with
objects part1, part2
within a structure with
object obj
I B.Tech
C Programming Lab
P a g e | 107 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 108 Department of Computer Science & Engineering GEC
true
Start
Read source file
fname1
true
fs=fopen(fname1,"r");
ft=fopen(fname2,"w");
if
fs==NULL ||
ft==NULL
false
Unable to open
false
ch=fgetc(fs);
fputc(ch,ft);
if ch!=EOF
fcloseall()
Print File copy
operation performed
successfully
Stop
Read destination
file fname2
exit
FLOW CHART:
I B.Tech
C Programming Lab
P a g e | 109 Department of Computer Science & Engineering GEC
OUTPUT:
enter sourse file name1.c
enter dest file name2.c
File copy operation performed successfully
I B.Tech
C Programming Lab
P a g e | 110 Department of Computer Science & Engineering GEC
b) To reverse the first n characters in a file
DESCRIPTION:
This program perform the reverse operation of n characters in the file
ALGORITHM:
Step 1: Start
Step 2: read the command line arguments
Step 3: check if arguments=3 or not
If not print invalid no of arguments
Step 4: open source file in read mode
Step 5: if NULL pointer, then print file can not be open
Step 6: Store no. of chars to reverse in k
k= *argv[2]-48
Step 7: read the item from file stream using fread
Step 8: Store chars from last position to initial position in another string(temp)
Step 9: print the temp string
Step 10: Stop
I B.Tech
C Programming Lab
P a g e | 111 Department of Computer Science & Engineering GEC
true
true
false
false
true
Start
Read command
line args
If
argc!=3 fp=fopen(argv[1],”r” Print invalid
no of args
If
fp==NULL
Print file can
not be opened
false
k=atoi(argv[2])
n = fread(a,1,k,fp)
a[n]='\0'
len=strlen(a)
i = len-1 i--
i >=0
s[j]=a[i]
Print s[j]
j++
s[j+1]=‟\0‟
FLOW CHART:
Stop
I B.Tech
C Programming Lab
P a g e | 112 Department of Computer Science & Engineering GEC
OUTPUT:
C:\TC\PROGRAMS>reverse source.c 8
ollehiah
C:\TC\PROGRAMS>reverse source.c
Invalid number of arguments.
I B.Tech
C Programming Lab
P a g e | 113 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 114 Department of Computer Science & Engineering GEC
FLOWCHART:
false true if
s==1
Stop
symmetric
Print not
symmetric
Read elements of
matrix
start
Read matrix size
s=symmetric(a,n)
I B.Tech
C Programming Lab
P a g e | 115 Department of Computer Science & Engineering GEC
true
false
true
false
true
symmetric(a,n)
true
true
false
false
at[i][j]=a[j][i]
Print resultant
transpose matrix
i=0 i++
i<n
j=0 j<n
j++
false
j=0 j<n
j++
if a[i][j]!=
at[i][j]
Return 0
c++
Return to
main()
Return 1
i=0 i++
i<n
I B.Tech
C Programming Lab
P a g e | 116 Department of Computer Science & Engineering GEC
OUTPUT:
Enter matrix size :2
Enter 4 elements :1
2
2
1
After Transpose :12
21
Symmetric
Enter matrix size :3
Enter 9 elements :1
2
3
4
5
6
7
8
9
After Transpose :147
258
369
Not symmetric
I B.Tech
C Programming Lab
P a g e | 117 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 118 Department of Computer Science & Engineering GEC
FLOWCHART:
true
false
false
false
true
true
true
Start
Read n
i=0 i++
i<n
i=0 i++
i<n
j=0 j<n
j++
If
a[i]>a[j
]
temp=a[i]
a[i]=a[j]
a[j]=temp
Read
a[i]
i=0 i++
i<n
Print a[i]
Stop
false
true
false
I B.Tech
C Programming Lab
P a g e | 119 Department of Computer Science & Engineering GEC
OUTPUT:
Enter number of elements :5
5
4
3
2
1
The 5 numbers in Ascending order :
1
2
3
4
5
Enter number of elements :8
33
44
22
11
66
55
17
88
The 8 numbers in Ascending order :
11
17
22
33
44
55
66
88
I B.Tech
C Programming Lab
P a g e | 120 Department of Computer Science & Engineering 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
I B.Tech
C Programming Lab
P a g e | 121 Department of Computer Science & Engineering GEC
FLOWCHART:
true false
false
Start
Read another File
to store content of
two files
Read two
File names
fs1 = fopen(file1,"r");
fs2 = fopen(file2,"r");
if
fs1 == NULL ||
fs2 == NULL
Print Error
ft = fopen(file3,"w")
if
ft == NULL Print Error
A B C
true
I B.Tech
C Programming Lab
P a g e | 122 Department of Computer Science & Engineering GEC
true false
false if
( ch = fgetc(fs1) )
!= EOF
fputc(ch,ft)
if
( ch = fgetc(fs2) )
!= EOF
fputc(ch,ft)
Two files are
merged
successfully
fclose(fs1)
fclose(fs2)
fclose(ft)
Stop
C
true
A B