Clab
Clab
net
C Programming Lab
I B.Tech
EXERCISE: 1
a) Convert temperature from centigrade to Fahrenheit and Fahrenheit to centigrade. DESCRIPTION: To convert Fahrenheit to centigrade, subtract 32 and multiply by 0.56. To convert centigrade to Fahrenheit, multiply by 1.8 and add 32 degrees. ALGORITHM: Step 1: start Step2: read temperature in Fahrenheit Step 3: calculate centigrade = (Fahrenheit -32)*0.56 Step 4: display centigrade value Step 5: read temperature in centigrade Step 6: calculate Fahrenheit = 32 + (centigrade * 1.8) Step 7: display Fahrenheit value Step 8: stop
Page |1
GEC
www.jntuworld.com || www.jwjobs.net
FLOW CHART:
Start
Read f
c=(fa-32)*0.56
Display c
Read c
fa = 32+(c*1.8)
Display fa
stop
Page |2
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
Read marks
avg >80
no
no
no Display fail
stop
Page |4
GEC
www.jntuworld.com || www.jwjobs.net
OUTPUT:
Enter your marks 36 36 36 36 36 36 You Failed in this exam
Page |5
GEC
www.jntuworld.com || www.jwjobs.net
c) Program for income tax for calculation. ALGORITHM: Step 1: start Step 2: read income Step 3: if income >0 and <200000 displa y 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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
start
Read income
no
no
no
Display No tax
Display tax
Display tax
Display tax
INCOME TAX AMOUNT= 50000.000000 REMAINING INCOME= 450000.000000 Enter Income: 225000 INCOME TAX AMOUNT= 22500.000000 REMAINING INCOME= 202500.000000
Page |7
GEC
www.jntuworld.com || www.jwjobs.net
EXERCISE: 2
To convert the given binary number to 2s complement . DESCRIPTION: In this program the given binary number is first covert the numbers 0 to1 and 1 to 0. And finally add the 1 to the converted number. Then we will get the 2s complement number. ALGORITHM: Step1: Start Step2: Read a binary number b Step3: store length of the binary number-1 value in n Step4: initialize i=n Step5: if i>=0 otherwise go to step7 (i) if b[i]==1 is true, otherwise go to step6 (A) initialize j=i-1 (B) if j>=0 otherwise break (a) if b[j]==1 otherwise b[j]=1 go to step5(i)(B)(b) b[j]=0 (b) decrement j go to step5(i)(B) Step6: decrement i, go to step5 Step7: print b Step8: stop
Page |8
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
Start
Read a binary
number n= strlen(b)-1
false
i--
false
true
false
j = i-1 j--
j>=0
true
if b[j]==1
true
false
b[j] = 0
b[j] = 1
Stop OUTPUT: Enter Binary Number: 101010 Its 2's Complement Is: 010110 Enter Binary Number: 11111 Its 2's Complement Is: 00001
Page |9
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
s=0
Read N
Print S
IF N! =0
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
GEC
www.jntuworld.com || www.jwjobs.net
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-1where 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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
Start
True
If n==1 True
False
If n ==2
False
Output a,b
False
i=3
i++
i<=n
true c =a + b
Output c
a=b b=c
Stop
enter n value10 0 1 1 2 3 5 8 13 21 34
P a g e | 13
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOW CHART:
Start
Read n
i=1
i++ i<=n
false
true c=0
j=1
j++
false
false
j<=i
true
if c==2
if i % j == 0
false
stop
P a g e | 15
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
START
a=0,b=1 c = a+b
Read n
if c< n
no
no
Stop
P a g e | 17
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOW CHART:
Start
Read x,n
false
i=1, i++
i<=n
true
f= 1
true
j=1, j++ j<=p
f= f*j
false false
if i%2 == 0 true
sum=sum(pow(x,p)/f)
sum=sum+ (pow(x,p)/f)
Display sum
p = p+2
Stop
P a g e | 19
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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))
P a g e | 21
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
Start
Read a,b,c
d = pow((b*b-4*a*c),0.5)
false If d== 0 true r1=-b / (2 * a ) r2= -b / (2 * a) If d < 0 false r1 = ((-b+d) / (2*a)) r2 = ((-b-d) / (2*a))
true
Display r1, r2
Stop
P a g e | 22
GEC
www.jntuworld.com || www.jwjobs.net
P a g e | 23
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
start
Read u, a, t
d= (u*t)+(0.5*a*t*t)
Display distance as d
Read ch
true
P a g e | 25
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART: start
Read op1,op2, op
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
If op ==%
false
true r=op1%op2
Display r
stop
P a g e | 28
GEC
www.jntuworld.com || www.jwjobs.net
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 % MODULUS -- Remainder is 2 P a g e | 29 Department of Computer Science & Engineering
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
Flowchart:
start
l1=strlen(s1)
Display l1
s=strcpy(s3,s1)
Display s
e=strcmp(s1,s2)
If e==0 true
Display s1,s2 are equal
false
r=strrev(s1)
c=strcat(s1,s2)
Display r
Display c
stop
P a g e | 31
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
b) Verifying a string for its palindrome property. DESCRIPTION: Read a string, compare first and last characters of a string, if equal continue up to middle of the string. If the comparison fails at any character the string is not a palindrome otherwise palindrome property satisfies. Algorithm: Step 1: start Step 2: read the string s1 Step 3: store reverse of the given string s1 in a temporary string s2 Step 4: compare the two strings s1 and s2 Step 5: if both are equal then print the given string is palindrome Step 6: otherwise print the given string is not palindrome Step 7: stop FLOWCHART: Read string s1 start
false
www.jntuworld.com || www.jwjobs.net
P a g e | 34
GEC
www.jntuworld.com || www.jwjobs.net
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
false
i=0
s2[i]=\0
Display s2
stop
P a g e | 35
GEC
www.jntuworld.com || www.jwjobs.net
OUTPUT: Enter a string : malayalam The copied string is malayalam Enter a string : haihello The copied string is haihello
P a g e | 36
GEC
www.jntuworld.com || www.jwjobs.net
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
Display sum
www.jntuworld.com || www.jwjobs.net
P a g e | 38
GEC
www.jntuworld.com || www.jwjobs.net
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]!=\0and 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
GEC
www.jntuworld.com || www.jwjobs.net
Read string s1
false
i=0
false
i=0, j=sum-1
rev[j]=s[i]
rev[j]=\0
Display rev
stop
P a g e | 40
GEC
www.jntuworld.com || www.jwjobs.net
P a g e | 41
GEC
www.jntuworld.com || www.jwjobs.net
EXERCISE: 7
a) Functions to insert a sub string into given main string from a given position DESCRIPTION: In this program we need to insert a string into another string from a specified position. ALGORITHM: Step 1: start Step 2: read main string and sub string Step 3: find the length of main string(r) Step 4: find length of sub string (n) Step 5: copy main string into sub string Step 6: read the position to insert the sub string (p) Step 7: copy sub string into main string from position p-1 Step 8: copy temporary string into main string from position p+n-1 Step 9: print the strings Step 10: stop
FLOWCHART:
Start
Insert (s1,s2,i)
Start
P a g e | 42
GEC
www.jntuworld.com || www.jwjobs.net
No
i=0
Yes No if i==x
s[c]=s1[i]
s[c]='\0'
Print string s
Return to main()
P a g e | 43
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
Start
FLOW CHART:
Read string
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
www.jntuworld.com || www.jwjobs.net
DESCRIPTION: Replace a character of string either from beginning or ending or at a specified location. ALGORITHM: Step 1: start Step 2: read string Step 3: find the length of the string Step 4: read the replace characters Step 5: read the position to be placed like end,begin,pos Step 6: char placed in a part of string from position to end, and (position+number of characters to end) Step 7: print string Step 8: stop FLOW CHART: Read string start
switch opt
false
true
s1[0] = ch;
s1[l-1] = ch;
Print string
P a g e | 47
stop
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
Declare structure
Read choice
switch choice case add c3.real=c1.real+c2.real c4.img=c1.img+c2.img case mul c4.real=(c1.real*c2.real)-(c1.img*c2.img) c4.img=(c1.real*c2.img)+(c1.img*c2.real)
case read
case write
P a g e | 50
GEC
www.jntuworld.com || www.jwjobs.net
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
1:Reading
2:Writing
3:Addition
4:Multiplication
5:Exit
1:Reading
2:Writing
3:Addition
4:Multiplication
5:Exit
1:Reading
2:Writing
3:Addition
4:Multiplication
5:Exit
P a g e | 51
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART: start
true
false
i=0 i<r1
i++ true
j=0 j++
j<c1 true
false
c[i][j]=a[i][j]+b[i][j];
stop
P a g e | 53
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART: start
false
i=0 i<c
i++
stop
P a g e | 56
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART: start
if c1!=r2
true
false
Read matrices A and B
i=0 false i<r1 true j=0 j++ true c[i][j]=0 k=0 k++
i++
j<c2
false
k<c1
false
stop
P a g e | 59
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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)
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
start
Read n
Print F value
Initialize F = 1
if n == 0 || n == 1
i= 1
i++
Return 1 false
i<=n
true
F=F*i
OUTPUT:
Return F
enter the number :5 factoria of 5 is 120 enter the number :7 factoria of 7 is 5040
P a g e | 64
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOW CHART:
Start
Read a,b
Stop
Subprogram Flowchart
gcdrec( )
true If n==0
false
Return m
Return to main()
OUTPUT: enter the two numbers whose gcd is to be found:36 GCD of a,b is 9 enter the two numbers whose gcd is to be found:15 GCD of a,b is 5 25 63
P a g e | 66
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
start
Read a, b
Output g
stop
Subprogram Flowchart
gcdnonrec(m,n)
r=m%n
true
if r!=0
false
Return n
OUTPUT: enter the two numbers whose gcd is to be found:36 GCD of a,b is 9 enter the two numbers whose gcd is to be found:15 GCD of a,b is 5 enter the two numbers whose gcd is to be found:36 GCD of a,b is 18 54 25 63
P a g e | 68
GEC
www.jntuworld.com || www.jwjobs.net
c).To solve the towers of Hanoi problem by using the recursive function
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
Start
no
Sub program
Hanoirec(num,n1,n2,n3)
false
if num==1
true
Print A,B
P a g e | 70
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
Stop
P a g e | 72
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
Start
Read n
false
i= 0
i<n
i++
i++
true
min=a[i]
stop
P a g e | 75
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
b) Program that displays the position or index in the string S where the string T begins , or 1 if S doesnt contain T ALGORITHM: Step 1: start Step 2: read the string and then displayed Step 3: read the string to be searched and then displayed Step 4: searching the string T in string S and then perform the following steps i. found=strstr(S,T) ii. if found print the second string is found in the first string at the position. otherwise goto step 5 Step 5: print -1 Step 6: stop
P a g e | 77
GEC
www.jntuworld.com || www.jwjobs.net
FLOW CHART:
Start
Found=strstr(s,t)
no If found
yes
Print -1
Stop
P a g e | 78
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
c)
DESCRIPTION: In this program we have to count the no of lines, no of words and no of characters in a given program or given text by using the string function ALGORITHM: Step 1: Start Step 2: Read the text until an empty line Step 3: Compare each character with newline char \n to count no of lines Step 4: Compare each character with tab char \t\ or space char to count no of words Step 5: Compare first character with null char \0 to find the end of text Step 6: No of characters = length of each line of text Step 7: Print no of lines, no of words, no of chars Step 8: Stop
P a g e | 80
GEC
www.jntuworld.com || www.jwjobs.net
Initialize end=0,chars=0,words=0,lines=0
true
if end!=0 true c= 0
false
line[c]=\0
if line[0]=\0 false Words ++ i=0 i ++ line[i]!=\0 true Print lines, Words, chars false If line[i]== || line[i]==\t true stop Words ++ false lines++ chars+=strlen(line)
P a g e | 81
GEC
www.jntuworld.com || www.jwjobs.net
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.
P a g e | 82
GEC
www.jntuworld.com || www.jwjobs.net
EXERCISE : 12
a) To generate Pascals triangle DESCRIPTION: Pascals triangle which is used for a coefficient in the equation in polynomials. ALOGRITHM: Step 1: Start Step 2: Read n Step3: initialize i=0 Step4: if i<n otherwise go to step 11 Step5: initialize j=0 Step6: if j<=i otherwise go to step10 Step7: if j==0 or i==j otherwise p[i][j]=p[i-1][j-1] + p[i-1][j] go to step 9 Step8: p[i][j]=1 Step9: increment j go to step6 Step10: increment i go to step 4 Step11: initialize i=0 Step12: if i<n otherwise go to step19 Step13: initialize k=0 Step14: if k<=n-i otherwise go to step16 Step15: print space ,Increment k, go to step14 Step 16: initialize j=0 Step17: if j<=i otherwise print new line Step18: print p[i][j] ,increment j go to step17 Step19: stop
P a g e | 83
GEC
www.jntuworld.com || www.jwjobs.net
FLOW CHART:
Start
Read n true
i< n
j=0
j ++
if((j==0) || (i==j))
false
p[i][j]=p[i-1][j-1]+p[i-1][j]
k++
true Print space
true
Print p[i][j]
Stop
OUTPUT:
P a g e | 84
GEC
www.jntuworld.com || www.jwjobs.net
P a g e | 85
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
Read n
false
i=1 i<=n
i++
false
k=1 k<=i
k++
true Print k
stop
P a g e | 87
GEC
www.jntuworld.com || www.jwjobs.net
OUTPUT:
Enter number of lines to print :8 1 12 123 1234 12345 123456 1234567 12345678
P a g e | 88
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART: Start
Initialize sum=1 Read x,n false if n<=0||x<=0 Print value is valid false i=1 i<n true sum+=pow(x,i) i++ Print not valid true
Print sum
Stop
P a g e | 90
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
If true
Read ch
false
A A
P a g e | 93
GEC
www.jntuworld.com || www.jwjobs.net
false
if ch==3
exit i++
false
i=1 i<=n
stop
P a g e | 94
GEC
www.jntuworld.com || www.jwjobs.net
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
P a g e | 95
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART: start
Swap()
P a g e | 97
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART: Start
Read n
a=(int *) malloc(n*sizeof(int))
false
i++
false
i++
free(a)
false
P a g e | 100
GEC
www.jntuworld.com || www.jwjobs.net
false
OUTPUT: Enter no.of values :5 enter 5 elements :50 40 30 20 10 The given elements are : 50 07B0 40 07B2 30 07B4 20 07B6 10 07B8 enter 5 elements :5 4 3 2 1 The given elements are : 5 07B0 4 07B2 3 07B4 2 07B6 1 07B8 P a g e | 101
free(a)
Stop
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART:
false
i++
j<6
s[i].total + = s[i].m[j]
false
i++
STOP
P a g e | 103
GEC
www.jntuworld.com || www.jwjobs.net
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 students details: NAME ROLLNO CLASS TOTALMARKS ramu somu 1 2 first second 135 201
P a g e | 104
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
Code=s Size=4
P a g e | 106
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
Read source file fname1 Read destination file fname2 fs=fopen(fname1,"r"); ft=fopen(fname2,"w");
false
false
fcloseall()
Stop
P a g e | 108
GEC
www.jntuworld.com || www.jwjobs.net
OUTPUT: enter sourse file name1.c enter dest file name2.c File copy operation performed successfully
P a g e | 109
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
true
If argc!=3
false
fp=fopen(argv[1],r
true
If fp==NULL
i--
Print s[j]
j++ Stop
P a g e | 111
GEC
www.jntuworld.com || www.jwjobs.net
P a g e | 112
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART: start
s=symmetric(a,n)
true
if s==1
false
Print symmetric
Stop
P a g e | 114
GEC
www.jntuworld.com || www.jwjobs.net
symmetric(a,n)
false
i=0 i<n
i++ true
j=0 j++
j<n
false
true at[i][j]=a[j][i]
false
i=0 i<n
j=0 j++
j<n
false
true Return 0
Return to main()
P a g e | 115
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART: Start
Read n
i++
true Read a[i] i=0 false i<n true j=0 j++ true If a[i]>a[j ] true temp=a[i] a[i]=a[j] a[j]=temp false j<n false i++
i++
Stop
P a g e | 118
GEC
www.jntuworld.com || www.jwjobs.net
P a g e | 119
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net
FLOWCHART: Start
false
ft = fopen(file3,"w")
false
P a g e | 121
GEC
www.jntuworld.com || www.jwjobs.net
if ( ch = fgetc(fs1) ) != EOF
false
true fputc(ch,ft)
if ( ch = fgetc(fs2) ) != EOF
false
true fputc(ch,ft)
Stop
P a g e | 122
GEC
www.jntuworld.com || www.jwjobs.net
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
GEC
www.jntuworld.com || www.jwjobs.net