Professional Documents
Culture Documents
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
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
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
FLOW CHART:
start
Read marks
total=m1+m2+m3+m4
+m5+m6;
avg=total/6;
stop
OUTPUT:
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
FLOWCHART:
start
Read income
yes
yes yes
tax = income *
Display tax = income*0.2 0.3
tax = income*0.1
No 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
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
FLOWCHART: Start
Read a binary
number
n= strlen(b)-1
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:
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
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:
DESCRIPTION:
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
FLOWCHART:
Start
Initialize
a=0,b=1
Read n
True False
If n==1
True False
If n ==2
Output a
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
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
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
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
OUTPUT: Stop
Enter no to find: 21
21 is Fibonacci
Enter no to find: 25
25 is not Fibonacci
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
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
OUTPUT:
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
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
OUTPUT:
Enter the values of a,b,c:1 6 9
Roots are Equal:-3.000000,-3.000000
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
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
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
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
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
OUTPUT:
Operations Performed are: + - * / %
Enter 2 Integers & an Operator:2
4
*
MULTIPLICATION -- Product is 8
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
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
stop
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
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
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
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
OUTPUT:
FLOWCHART:
Start
Initialize sum = 0
Read
string s1
i++
true
sum ++
Display
sum
stop
OUTPUT:
FLOW CHART:
start
Initialize sum = 0
Read
string s1
i++
true
sum ++
i++,j--
true
rev[j]=s[i]
rev[j]=‟\0‟
Display
rev
stop
OUTPUT:
Enter a string : sai
Reverse of the given string is ias
EXERCISE: 7
a) Functions to insert a sub string into given main string from a given position
DESCRIPTION:
In this program we need to insert a string into another string from a specified position.
ALGORITHM:
Step 1: start
Step 2: read main string and sub string
Step 3: find the length of main string(r)
Step 4: find length of sub string (n)
Step 5: copy main string into sub string
Step 6: read the position to insert the sub string (p)
Step 7: copy sub string into main string from position p-1
Step 8: copy temporary string into main string from position p+n-1
Step 9: print the strings
Step 10: stop
FLOWCHART:
Start
Insert (s1,s2,i)
Start
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()
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
Read string
Read position, no of
characters
Stop
delchar( )
if ((a+b-1)<=
strlen(x))
Strcpy(&x[b-1],&x[a+b-1])
Print x
Return to
main()
OUTPUT:
1. enter the string
nagaraju
Enter the position from where to delete:4
Enter the number of charcters to be deleted3
nagju
2. enter the string
kaliraju
Enter the position from where to delete:0
Enter the number of charcters to be deleted4
Raju
DESCRIPTION:
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
Not match
Print string
OUTPUT:
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
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 exit
Stop
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
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
FLOWCHART:
start
If r1!=r2 true
and c1!=c2
false
Display
Read matrices A and B invalid order
of matrices
true
j=0 j<c1 false
j++
true
c[i][j]=a[i][j]+b[i][j];
stop
OUTPUT:
FLOWCHART:
start
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
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
FLOWCHART:
start
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])
stop
OUTPUT:
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
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
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
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
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
FLOW CHART:
Start
Read a,b
Print gcdvalue
Stop
Subprogram Flowchart
gcdrec( )
true false
If n==0
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
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
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
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
c).To solve the towers of Hanoi problem by using the recursive function
DESCRIPTION:
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
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
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
ii)To solve the towers of Hanoi problem by using the non recursive function
DESCRIPTION:
Towers of Hanoi problem means we have three towers
true
Print (x&x-1)%3,
((x|x-1)+1)%3
Stop
Output:
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
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
OUTPUT:
enter the array size:5
Enter the elements of array50
40
30
20
10
maximum value is:50
minimum value is:10
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
FLOW CHART:
Start
Read string t to be
searched
Found=strstr(s,t)
no yes
If found
Print -1
Print string is
found
Stop
OUTPUT:
Enter the first string:
gudlavalleru
Enter the string to be searched:
lava
Second String is found in the First String at 3 position.
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
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 ++
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
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
Read n
i++ j<=i
false
true
j=0 j++
Print
newline
Stop
OUTPUT:
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
FLOW CHART:
start
Read n
i<=n
true
false j=0 j++
j<=n-i
true
Print space
k<=i
true
Print k
stop
OUTPUT:
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
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
Output:
Enter the values for x and n:4
4
Value is valid
Sum of series= 341
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
Start
Initialize
C D
sum=0,term=0
If true
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
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
OUTPUT:
1.sin 2.cos 3.exit
1
Enter any number : 45
Enter no.of terms 5
The sin is 0.706825
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
FLOWCHART:
start
Swap()
Stop
Subprogram:
void swap()
temp=*a
*a=*b,
*b=temp
main()
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
EXERCISE : 14
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
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)
i=0 i++
false
i<n
true
OUTPUT: free(a)
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
FLOWCHART:
START
STSTA
RT
Read number of
students n
i=0 i++
false
i<n
true
Initialize
s[i].total=0
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
OUTPUT:
FLOWCHART:
Start
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
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
FLOW CHART:
Start
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()
Stop
OUTPUT:
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
Start
Read command
line args
true
Print invalid If false fp=fopen(argv[1],”r”
no of args argc!=3
false
k=atoi(argv[2])
n = fread(a,1,k,fp)
a[n]='\0'
len=strlen(a)
i = len-1 i--
true
s[j]=a[i]
Print s[j]
j++
Stop
OUTPUT:
C:\TC\PROGRAMS>reverse source.c 8
ollehiah
C:\TC\PROGRAMS>reverse source.c
Invalid number of arguments.
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
FLOWCHART:
start
Read elements of
matrix
s=symmetric(a,n)
true if false
s==1
Stop
symmetric(a,n)
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()
OUTPUT:
Enter matrix size :2
Enter 4 elements :1
2
2
1
After Transpose :12
21
Symmetric
EXCERCISE 2:
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
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
OUTPUT:
Enter number of elements :5
5
4
3
2
1
The 5 numbers in Ascending order :
1
2
3
4
5
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
FLOWCHART:
Start
Read two
File names
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
if
false
( ch = fgetc(fs1) )
!= EOF
true if
false
( ch = fgetc(fs2) )
fputc(ch,ft) != EOF
true
fputc(ch,ft)
fclose(fs1)
fclose(fs2)
A B
fclose(ft)
Stop
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.