You are on page 1of 123

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

Enter Temperature in Celsius: 37


Temperature in Fahrenheit is: 98.600000

Page |2 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

Page |3 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART:
start

Read marks

total=m1+m2+m3+m4
+m5+m6;
avg=total/6;

no avg >=60 no avg >=40 no


avg >80
and <=80 and <=60
Display fail

yes yes yes

Display Display Display


grade =A grade =B grade = C

stop

Page |4 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

Page |5 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

Page |6 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:

start

Read income

income > income >


income > no no 500001 and no
200001 and
0 and < <500000 < 1000000
200000

yes
yes yes
tax = income *
Display tax = income*0.2 0.3
tax = income*0.1
No tax

Display tax Display tax Display tax

OUTPUT:
stop
Enter Income: 500000
INCOME TAX AMOUNT= 50000.000000
REMAINING INCOME= 450000.000000
Enter Income: 225000
INCOME TAX AMOUNT= 22500.000000
REMAINING INCOME= 202500.000000

Page |7 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

Page |8 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART: Start

Read a binary
number

n= strlen(b)-1

false i=n i--

i >=0

true

If false
b[i]==‟1‟

true
j = i-1 j>=0
false

j--
true

if false
b[j]==‟1‟

true
b[j] = „0‟ b[j] = „1‟

Display 2‟s
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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART:
START

s=0

Read N

IF N < 0
TRUE FALSE

IF
Print S N! =0

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 12 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

False i=3 i++


i<=n
true

c =a + b

Output c

a=b
b=c
Stop

OUTPUT:
enter n value5
0 1 1 2 3
enter n value10
0 1 1 2 3 5 8 13 21 34

P a g e | 13 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 14 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART: Start

Read n

i=1 i++
false
i<=n

true
c=0

j=1 j++
false
false
j<=i
true

if c==2
if i % j == 0 false
true

true
Display i
c ++

OUTPUT: stop

Enter Value of n:5


Prime Numbers upto 5 are:
235

Enter Value of n:9


Prime Numbers up to 9 are:2 3 5 7

P a g e | 15 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 16 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:

START

a=0,b=1

c = a+b

Read n

if n ==0 or
if c< n no no
n==1 or
n ==c
yes
c= a+b
a=b, b=c yes

Display n is Display n is not


Fibonacci a Fibonacci
number number

OUTPUT: Stop

Enter no to find: 21
21 is Fibonacci
Enter no to find: 25
25 is not Fibonacci

P a g e | 17 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART: Start

Initialize Sum = 0.0,


p=0

Read x,n

i=1, i<=n
false
i++
true

f= 1

true
j=1, j<=p f= f*j

j++
false

false
if i%2
== 0

true

sum=sum- sum=sum+
(pow(x,p)/f) (pow(x,p)/f)

p = p+2
Display
sum

Stop

P a g e | 19 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 20 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 21 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:

Start

Read a,b,c

d = pow((b*b-4*a*c),0.5)

false true
If d== 0 If d < 0

true false
r1 = ((-b+d) / (2*a)) Display
r1=-b / (2 * a ) r2 = ((-b-d) / (2*a)) roots are
r2= -b / (2 * a) imaginary

Display
r1, r2

Stop

P a g e | 22 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 23 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:

start

Read
op1,op2, op

If op false
==‟+‟

If op false
true
==‟-‟

r=op1+op2
If op false
true
==‟*‟
r=op1-op2
If op false
true
==‟/‟

r=op1*op2
If op false
true
==‟%‟

r=op1/op2
true

r=op1%op2

Display
invalid
Display r operation

stop

P a g e | 28 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 29 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

Flowchart:
start

Read strings
s1,s2,s3

l1=strlen(s1)

Display l1

s=strcpy(s3,s1)

Display s

e=strcmp(s1,s2)

r=strrev(s1) c=strcat(s1,s2)
false
If
e==0

true

Display s1,s2 Display s1,s2


are equal are not equal Display r Display c

stop

P a g e | 31 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 32 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 start
FLOWCHART:

Read string s1

strcpy(s2,s1)

strrev(s2)

p=strcmp(s1,s2)

if p false
== 0

true

The given The given


string s1 is string s1 is not
palindrome palindrome

stop
P a g e | 33 Department of Computer Science & Engineering GEC
C Programming Lab
I B.Tech

OUTPUT:
Enter a string: madam
The given string is palindrome

Enter a string : manual


The given string is not palindrome

P a g e | 34 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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:
start

Read a string
s1

i=0 s1[i]!=‟\0‟
false
i++

true
s2[i]=s1[i]

s2[i]=‟\0‟

Display s2

stop

P a g e | 35 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

OUTPUT:

Enter a string : malayalam


The copied string is malayalam

Enter a string : haihello


The copied string is haihello

P a g e | 36 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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:
Start

Initialize sum = 0

Read
string s1

false i=0 s1[i]!=‟\0‟

i++

true
sum ++

Display
sum

stop

P a g e | 37 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

OUTPUT:

Enter a string : gudlavalleruengineeringcollege


30

Enter a string : string


6

P a g e | 38 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 39 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART:

start

Initialize sum = 0

Read
string s1

false i=0 s1[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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

OUTPUT:
Enter a string : sai
Reverse of the given string is ias

Enter a string : cprogramming


Reverse of the given string is gnimmargorpc

P a g e | 41 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 42 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

Insert (s1, s2, x)

No i=0 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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 44 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 45 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

Start FLOW CHART:

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()

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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
start

FLOW CHART:

Read string

Read replace
character
chara

switch
opt case p
false
case b pos >=0
case e and pos
<=l-1
true

s1[0] = ch; s1[l-1] = ch; s1[pos]= ch;

Not match

Print string

stop Department of Computer Science & Engineering GEC


P a g e | 47
C Programming Lab
I B.Tech

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

P a g e | 48 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 49 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART:

Start

Declare structure

Read choice

switch
choice
case mul
case add

c3.real=c1.real+c2.real c4.real=(c1.real*c2.real)-(c1.img*c2.img)
c4.img=c1.img+c2.img c4.img=(c1.real*c2.img)+(c1.img*c2.real)

case read case write

Read complex Display the


numbers result

case exit

Stop

P a g e | 50 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 51 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:
start

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 true
and c1!=c2

false
Display
Read matrices A and B 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];

Print result matrix

stop

P a g e | 53 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 54 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 55 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:

start

Read rows and


columns of matrix

Read elements of
matrix

i=0 i++
false
i<c

true
j=0 j<r false
j++

true

at[i][j]=a[j][i]

Print resultant
transpose matrix

OUTPUT: stop
Enter Number of Rows: 2
Enter Number of Columns: 3
Enter 6 Elements: 2
3
4
5
6
7

P a g e | 56 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 58 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:
start

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
true
false
Read matrices A and B

Display
i=0 i++ invalid order
false i<r1 of matrices

true
j=0 j<c2 false
j++
true
c[i][j]=0
false
k=0 k<c1
k++

true
c[i][j]=c[i][j]+(a[i][k]*b[k
][j])

Print result matrix

stop

P a g e | 59 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 60 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 i++
Return 1 i<=n

false true

F=F*i

Return to main
program Return F
OUTPUT:
enter the number :5
factoria of 5 is 120
enter the number :7
factoria of 7 is 5040

P a g e | 64 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART:

Start

Read a,b

Call sub program


G=gcdrec(a,b)

Print gcdvalue

Stop

Subprogram Flowchart

gcdrec( )

true false
If n==0

Return m Call the same function


Return gcdrec(n,m%n)

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART: start

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, Return n
n=r,
r=m%n

Return to main program

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 true
if num==1

Call sbgroram Print A,B


Hanoirec(num-1,n1,n3,n2)

Print A,C Call subprogram Return to main


Hanoirec(num-1,n3,n2,n1) program

P a g e | 70 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:
Start

Read n

i= 0
false 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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 76 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 77 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART:

Start

Read first string s

Read string t to be
searched

Found=strstr(s,t)

no yes
If found

Print -1
Print string is
found

Stop

P a g e | 78 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 79 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 80 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART:
Start

Initialize end=0,chars=0,words=0,lines=0

true
if
end!=0

true

c= 0

if false
(ctr=getchar())==‟\n‟

true line[c]=‟\0‟
line[c++]=ctr

if
line[0]=‟\0‟

false
Words ++
i=0

i ++ line[i]!=‟\0‟ false

true
lines++
Print lines, false If line[i]==‟ „|| chars+=strlen(line)
Words, chars line[i]==‟\t‟

true

stop Words ++

P a g e | 81 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 83 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART: Start

Read n

i=0 i< n true j=0 j ++


false

i++ j<=i
false
true

true if((j==0) false


|| (i==j))
i=0 i<n false
p[i][j]=1
p[i][j]=p[i-1][j-1]+p[i-1][j]
i++
True
k=0 k++

k<=n-i true Print


space
False

j=0 j++

j<=i true Print


p[i][j]
false

Print
newline

Stop

OUTPUT:

P a g e | 84 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

Enter no of lines to print: 5


1
11
121
1331
14641

Enter no of lines to print: 8

1
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1

P a g e | 85 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 86 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

Print new line

stop

P a g e | 87 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

OUTPUT:

Enter number of lines to print :6


1
12
123
1234
12345
123456

Enter number of lines to print :8


1
12
123
1234
12345
123456
1234567
12345678

P a g e | 88 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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) 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

P a g e | 89 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:

Start

Initialize
sum=1

Read x,n

false true
if
n<=0||x<=0

Print value
is valid
Print not
false i=1 i++ valid

i<n
true

sum+=pow(x,i)

Print sum

Stop

P a g e | 90 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 91 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech
Flow chart:

Start

Initialize
C D
sum=0,term=0

If true

1.sin 2.cos 3.exit

Read ch

If(ch!=3)

true
Read x,n

x=(x*3.14)/180

If false
ch==1

true
term=sum=x

B
i=1 i++

i<=n
true
term= ( (-term)*(x*x) ) / ((2*i)*(2*i+1));
sum=sum+term

P a g e | 93 Department of Computer Science & Engineering GEC


Print sin
series sum
C Programming Lab
I B.Tech

if false if
ch==2 ch==3
true
term=sum=1 exit

i=1 i++
false

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 95 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:
start

Read num1, num2

Before Swaping Print


num1,num2

Swap()

After Swaping Print


num1,num2

Stop

Subprogram:

void swap()

temp=*a
*a=*b,
*b=temp

main()

P a g e | 97 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:
Start

Read n

a=(int *) malloc(n*sizeof(int))

i=0 i++
false
i<n
true
Read (a+i)

i=0 i++
false
i<n

true
Print *(a+i), (a+i)

free(a)

a=(int *) calloc(n,sizeof(int))

i=0 i++
false
i<n

true
Read (a+i)

P a g e | 100 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

i=0 i++
false
i<n

true

Print *(a+i), (a+i)

OUTPUT: free(a)

Enter no.of values :5 Stop


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

P a g e | 101 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:
START
STSTA
RT
Read number of
students n

i=0 i++
false
i<n
true
Initialize
s[i].total=0

Read roll num, name, class,


6 subjects marks

false
j=0 j<6

j++
true
s[i].total + = s[i].m[j]

i=0 i++
false
i<n

true

Display student
details

STOP

P a g e | 103 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 104 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 105 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:

Start

Declare a union with


objects part1, part2
within a structure with
object obj

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
OUTPUT:
size of structure=11
size of union=4
Item=123 Cost=45.67
Code=s Size=4

P a g e | 106 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOW CHART:
Start

Read source file


fname1

Read destination
file fname2

fs=fopen(fname1,"r");
ft=fopen(fname2,"w");

if false
fs==NULL ||
ft==NULL
false
true if ch!=EOF

Print true
Unable to open
ch=fgetc(fs);
exit fputc(ch,ft);

fcloseall()

Print File copy


operation performed
successfully

Stop

P a g e | 108 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

OUTPUT:

enter sourse file name1.c


enter dest file name2.c
File copy operation performed successfully

P a g e | 109 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 110 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech
FLOW CHART:

Start

Read command
line args

true
Print invalid If false fp=fopen(argv[1],”r”
no of args argc!=3

Print file can true If


not be opened fp==NULL

false

k=atoi(argv[2])
n = fread(a,1,k,fp)
a[n]='\0'
len=strlen(a)

i = len-1 i--

s[j+1]=‟\0‟ false i >=0

true

s[j]=a[i]

Print s[j]

j++
Stop

P a g e | 111 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

OUTPUT:
C:\TC\PROGRAMS>reverse source.c 8
ollehiah

C:\TC\PROGRAMS>reverse source.c
Invalid number of arguments.

P a g e | 112 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:
start

Read matrix size

Read elements of
matrix

s=symmetric(a,n)

true if false
s==1

Print Print not


symmetric symmetric

Stop

P a g e | 114 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

symmetric(a,n)

false i=0 i++


i<n
true
false
j=0 j<n
j++

true
at[i][j]=a[j][i]

Print resultant
transpose matrix

i=0 i++
false
i<n
Return 1
true
j=0 j<n false

j++

true

if a[i][j]!= false
at[i][j] c++

true
Return 0

Return to
main()

P a g e | 115 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 116 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:

Start

Read n

i=0 i++
false
i<n
true

Read
a[i]

i=0 i++
false i<n
true
false
j=0 j<n

j++
true

If false
a[i]>a[j
]
true
temp=a[i]
a[i]=a[j]
a[j]=temp

i=0 i++
false i<n
true
Print a[i]

Stop

P a g e | 118 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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

P a g e | 119 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

FLOWCHART:

Start

Read two
File names

Read another File


to store content of
two files

fs1 = fopen(file1,"r");
fs2 = fopen(file2,"r");

if
false
fs1 == NULL || ft = fopen(file3,"w")
fs2 == NULL

true false
if
ft == NULL
Print Error
true

Print Error

A B C

P a g e | 121 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

if
false
( ch = fgetc(fs1) )
!= EOF

true if
false
( ch = fgetc(fs2) )
fputc(ch,ft) != EOF

true
fputc(ch,ft)

Two files are


merged
successfully

fclose(fs1)
fclose(fs2)
A B
fclose(ft)

Stop

P a g e | 122 Department of Computer Science & Engineering GEC


C Programming Lab
I B.Tech

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 Department of Computer Science & Engineering GEC

You might also like