Cp Lab Manual 2010
-
Upload
angeleena-grace -
Category
Documents
-
view
226 -
download
0
Transcript of Cp Lab Manual 2010
-
8/3/2019 Cp Lab Manual 2010
1/73
Exercise 2
2s complement of a number is obtained by seaming it from right to left and complementingall the bits after the first appearance of a 1. These 2s complement of 11100 is 00100.Write a C program to find the 2s complement of a binary number.Algorithm: main()
1. Input binary no of length 16 bit binary.2. Copy binary to dup.3. test = check(binary).
4. If test ==0 then (i) else (iii).(i) Print Invalid No.(ii) goto step 5.(iii) Call compl(binary, a).(iv) Print 2s complement , a.
5. Stop.Algorithm: Input(binary)
1. Input binary.2. return.
Algorithm: check(binary)
1. l = strlen(binary).
2. for i =0 to l.(i) if !(binary[i] = 0 or binary[i] = 1) then (ii) else (iii).(ii) goto step 2.(iii) x = 0.
3. return.Algorithm: compl(binary, a)
1. l = strlen(binary).2. for i = l 1 to 0 (Repeat step 3).3. If (binary[i]=0 ) then (i) else (iii).
(i) a[i] = 1.(ii) goto step 2.
(iii) a[i] = 0.(iv) goto step 2.4. for i =l 1 to 0.5. If (a[i] = 0) then (a) else if (b) else (c).
(a) a[i] = 1.(b) a[i] = 0.(c) if (check = 1 && a[i] = 0) then (i) else (iv).
(i) a[i] = 1.(ii) check = 0.(iii) goto step 4.(iv) if (check == 1 && a[i] == 1) then (a) else goto step 4.
(a) a[i] = 0.(b) check = 1.6. return
Program:
#include#include#include
-
8/3/2019 Cp Lab Manual 2010
2/73
#include#define L 16main(){
int i,test;char binary[L],dup[L],a[L];void input(char binary[]);int check(char binary[]);void compl(char binary[],char a[]);
clrscr();printf("Enter Binary no of Max length 16\n");input(binary);strcpy(dup,binary);test=check(binary);if(test==0){
printf("\nInvalid Binary number.");exit(1);
}
printf("\nOriginal Binary No:%s",dup);compl(binary,a);printf("\n2's complement is %s",a);getch();
}void input(char binary[]){
printf("Enter only 0's and 1's without spaces up to Max 16 Bits\n");scanf("%s",binary);return;
}
int check(char binary[]){
int i,l,x=1;l=strlen(binary);for(i=0;i
-
8/3/2019 Cp Lab Manual 2010
3/73
l=strlen(binary);for(i=l-1;i>=0;i--){
if(binary[i]=='0')a[i]='1';elsea[i]='0';
}for(i=l-1;i>=0;i--)
{if(i==l-1){
if(a[i]=='0')a[i]='1';
else{
a[i]='0';check=1;
}
}else{if((check==1)&&(a[i]=='0')){
a[i]='1';check=0;
}
else if((check==1)&&(a[i]=='1')){
a[i]='0';
check=1;}}
}a[l]='\0';return;
}
-
8/3/2019 Cp Lab Manual 2010
4/73
-
8/3/2019 Cp Lab Manual 2010
5/73
No
False
True
Yes No
Yes No
No
Yes No
Yes
Output:
Enter Binary no of Max length 16Enter only 0's and 1's without spaces up to Max 16 Bits1111
Original Binary No:11112's complement is 0001
a[i] = 0 a[i]=1
fori=l-1 to 0
if i=l-1
If a[i] =0
If check==1 &&a[i] = 0
a[i] = 1
a[i] = 0
check = 1
a[i] = 1
check = 0
If check== 1
&& a[i]= 1
a[i] = 0check = 1
a[l] = \0
return
-
8/3/2019 Cp Lab Manual 2010
6/73
Exercise 3
a. Write a Program in C to find the sum of individuals Digits of a positive Integer.Solution: -
Algorithm
1) Input a number n.2) Check n is +ve, if not goto step 6.3) S=0,m=n.4) Repeat the following until n=0.
i. r=n%10.ii. s+=s+r.iii. n=n/10.
5) print s, m.6) stop.
Flowchart
Yes No
Yes
No
Input n
If
n0){
ch=2;}else
ch=3;printf("A=%6.2f\nB=%6.2f\nC=%6.2f\n",a,b,c);printf("Discriminate D=%6.2f\n",d);switch(ch){
case 1:printf("Equal Roots");
x1=x2=-b/(2*a);printf("x1=x2=%6.2f\n",x1);break;
case 2:printf("Roots are Real and inequal");x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);
printf("x1=%6.2f\n",x1);printf("x2=%6.2f\n",x2);break;
case 3:
printf("Imaginary roots \n");printf("x1=%6.2f+i%6.2f\n",-b/(2*a), sqrt(-d)/(2*a));printf("x2=%6.2f-i%6.2f\n",-b/(2*a), sqrt(-d)/(2*a));break;
}getch();
}Flowchart:
Yes No
Start
Enter values a, b, c
d= b2 - 4 ac
Ifd=0
Ifd>0ch = 1
-
8/3/2019 Cp Lab Manual 2010
19/73
No Yes
1 2 3
Output:
1. Enter a,b,c values5 2 1
A= 5.00B= 2.00c= 1.00
Desciminat D=-16.00Imaginary rootsx1= -0.20+i 0.40x2= -0.20-i 0.40
2. Enter a,b,c values2 4 2
A= 2.00
ch = 3 ch = 2
Print a, b, c, d
Swi
tch
cas
ech
Print Equal roots
x1 = x2 = - b / 2a
Print x1, x2
Print roots are real &unequal
x1 = - b+d/2a
x2=-b-d/2a
Print x1, x2
Print Imaginaryroots
Print x1 iscomplex
Print x2 iscomplex
Stop
-
8/3/2019 Cp Lab Manual 2010
20/73
B= 4.00c= 2.00Desciminat D= 0.00Equal Roots x1=x2= -1.00
Exercise 5
a. The total distance travelled by vehicle in t seconds is given by distance =ut+1/2at2 whereu and a are the initial velocity(m/sec) and acceleration (m/sec2). Write C Program tofind 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 repeatthe calculations for different values of u and a.
Algorithm:
1. Begin.
2. Input initial velocity u.
3. Input Acceleration a.
4. Input starting time and final time i & n.
5. Input time interval it.
6. Print Time Distance U m/sec A m/sec2 .
7. For (t = i ; t
-
8/3/2019 Cp Lab Manual 2010
21/73
8. Stop.
Program:
#include#include#includemain(){
float u, a;float d;int t, i, n, it;clrscr();
printf("Enter Initial velocity\n");scanf("%f",&u);
printf("Enter Acceleration");scanf("%f",&a);
printf("Enter starting time/final time");scanf("%d%d",&i,&n);
printf("Enter time interval");scanf("%d",&it);
printf("Time\t Distance\t U m/sec \t a m/sec2)\n");printf(\n----------------------------------------------\n);for(t=i;t
-
8/3/2019 Cp Lab Manual 2010
22/73
}getch();
}
Flowchart:
No
Yes
Start
Input Initial velocity u
Input acceleration a
Input start time i
Input ending time n
Input Interval it
Repeat t=i ton with step
it
d=ut+1/2 at2
Print t, d, u, a
-
8/3/2019 Cp Lab Manual 2010
23/73
Input:
Enter Initial velocity4Enter Acceleration: 5Enter starting time/final time: 1 10Enter time interval: 1
Output:
Time Distance U m/sec a m/sec2)1 6.50 4.00 5.002 18.00 4.00 5.003 34.50 4.00 5.004 56.00 4.00 5.005 82.50 4.00 5.006 114.00 4.00 5.007 150.50 4.00 5.008 192.00 4.00 5.009 238.50 4.00 5.0010 290.00 4.00 5.00
Stop
-
8/3/2019 Cp Lab Manual 2010
24/73
b) Write a C Program, which takes two integers operands and one operator from the user,performs the operation and prints the result. (Consider the operators +,-,*,/,% and use SwitchStatement )Algorithm:
1. Begin
2. Enter a, b values.
3. Print MENU.
(i) Print + Addition.(ii) Print - Subtraction.(iii) Print * Multiplication.(iv) Print / Division.(v) Print % Remainder.(vi) Print E Exit.
4. Print Enter your choice.
5. If op==E then goto step 8 otherwise follow the below steps
6. Switch(op)
a. case +:
i. Print Addition.ii. c=a+b.iii. Print Sum=c.
iv. breakb. case -:
v. Print Subtraction.vi. c=a-b.vii. Print Difference=c.viii. break
c. case *:
ix. Print Multiplication.x. c=a*b.
xi. Print Product=c.xii. breakd. case /:
xiii. Print Division.xiv. c=a/b.xv. Print Quotient=c.xvi. break
-
8/3/2019 Cp Lab Manual 2010
25/73
e. case %:
xvii. Print Remainder.xviii. c=a%b.xix. Print Remainder=c.xx. break
f. default:
xxi. Print Invalid Option.xxii. break
7. while(1) then goto step 3.
8. Stop.
Program:
#include#include#includemain(){
int a, b, c;char op;clrscr();printf("Enter a and b:");scanf("%d%d",&a,&b);do{
printf("\n\nMENU\n");printf("+ Addition\n");printf("- Subtraction\n");printf("* Multiplication\n");
printf("/ Division\n");printf("% Remainder\n");printf("E Exit\n");printf(Enter your choice);getchar();op=getchar();if(op=='E'||op=='e')
exit(1);switch(op){
case '+':
printf("Addition\n");c=a+b;
printf("Sum=%d\n",c);break;
case '-':printf("Subtraction\n");c=a-b;
-
8/3/2019 Cp Lab Manual 2010
26/73
printf("Difference=%d\n",c);break;
case '*':printf("Multiplication\n");c=a*b;
printf("Product=%d\n",c);break;
case '/':
printf("Division\n");c=a/b;
printf("Quotient=%d\n",c);break; case '%':
printf("Remainder\n");c=a%b;
printf("Remainder=%d\n",c);break;
default:printf("Invalid Option\n");
break;} /*end of switch statement*/}while(1); /*End of while*/
}/*End of main function*/
Flowchart:
Start
Input a,b
Swi
tch(OP)
c = a + bPrintsum c c = a b
PrintDiff c c = a * b
Printproductc c = a / b
PrintQuaff c c = a % b
PrintremainderStop
-
8/3/2019 Cp Lab Manual 2010
27/73
Yes
No
+ - * / %
Input:
Enter a and b: 5 3Output:
MENU+ Addition- Subtraction* Multiplication/ Division% RemainderE Exit+
Menu+ Addition- Subtraction*Multiplication/ Division% RemainderE Exit
Input choice op
If op =E
-
8/3/2019 Cp Lab Manual 2010
28/73
AdditionSum=8MENU+ Addition- Subtraction* Multiplication/ Division% RemainderE Exit
*MultiplicationProduct=15
MENU+ Addition- Subtraction* Multiplication/ Division% Remainder
E ExitgInvalid Option
MENU+ Addition- Subtraction* Multiplication/ Division% RemainderE Exit
E (Exited)Exercise 7
Write a C program that uses functions to perform the following operations.
i) To insert a sub-string into given main string from a given position.
Algorithm:
1. Enter the main string: s
2. Determine length of string: s i.e. l
3. Print the main string: s
4. Input position of the substring to be inserted: p
5. if p is ve or p is greater than length of main string (l), then goto step 6 otherwise goto step 7 .
6. print ie substring out of the main string position, goto step 13.
-
8/3/2019 Cp Lab Manual 2010
29/73
7. input the substring, s1.
8. l1=string length of s1.
9. if l + l1 > L then go to step 10 otherwise go to step 11.
10. substring cant be inserted, because it is too long goto step 13.
11. call insert(s, p, s1)
12. print After string insertion the main string is: s.
13. stop.
Program:
#include#include#include#define L 80#define P 40main(){
int p,l,i,l1;char s[L],s1[P];void inst(char s[],int p,char s1[]);clrscr();
printf("Enter the Main string:\n");scanf("%[^\n]",s);l=strlen(s);
printf("The ma in string is \n%s",s);printf("\nPosition of the substring to be inserted:\n");scanf("%d",&p);
if((pl))printf("Substring position is out of the main string.");else { getchar();
-
8/3/2019 Cp Lab Manual 2010
30/73
printf("Enter substring:\n");scanf("%[^\n]",s1);l1=strlen(s1);if((l+l1)>L)
printf("\nstring can't be inserted because too long.");else{inst(s,p,s1);
printf("\nAfter string insertion:\n");
printf("%s",s);}
}getch();
}/*End of main functioin*/
Algorithm: Insert(s, p, s1)
1.l = length of s.
2. i=p;
3. k=length s1.
4. l1 = k
5. last = k+l+1
6. s[last] = \0
7. last = last 1.
8. p1 = l (p - 1)
9. s[last] = s[l]
10.last= last 1
11. l = l 1
12. p1 = p1 1
13. if(p!=0) then repeat 6 through 14.
14. k = 0
15. repeat i = p to i
-
8/3/2019 Cp Lab Manual 2010
31/73
i) s[i] = s1[k]
ii) k = k + 1.
16. return.
void inst(char s[],int p,char s1[]){
int l1,last,p1,i,j,l,k;l=strlen(s);i=p;k=strlen(s1);l1=k;last=k+l+1;s[last]='\0';last--;
p1=l-(p-1);
do{
s[last]=s[l];last--;l--;
p1--;}while(p1!=0);
k=0;for(i=p;i
-
8/3/2019 Cp Lab Manual 2010
32/73
Flowchart:
Yes No
Yes No
Start
Input main string: s
l=strlen(s)
print sub string:
s1
Input substring p
If pl
Print subs teing of
a main string
Input substring s1
l1=strlen(s1)
If l
+l1>l
Print string cant
be inserted
Insert(s, p,
s1)
Print after
insertion: s
Stop
-
8/3/2019 Cp Lab Manual 2010
33/73
-
8/3/2019 Cp Lab Manual 2010
34/73
Java is oops languagePosition of the substring to be inserted:8Enter substring:an
After string insertion:Java is an oops language
ii) To delete n Characters from a given position in a given string.
Algorithm:
1. Input text line.
2. Determine length of line l.
3. input position of starting character pos.
4. input No of characters to be deleted nc.
5. na = l pos.
6. if nc > na then perform the following otherwise goto step 7.
-
8/3/2019 Cp Lab Manual 2010
35/73
i) print text deleted from pos to end.
ii) line[pos]=\0.
7. p = pos + nc.
8. k = p.
9. for i=1 to l k perform the following
i) line[pos]=line[p].
ii) pos=pos+1
iii) p = p + 1.
10. line[pos]=\0.
11. print After deleting line is line,s.
12. stop.
Program:
#include#include#include#define ML 80main(){
char line[ML];int p,l,n,pos,nc,na,k,i;clrscr();
printf("Enter the line of text:");scanf("%[^\n]",line);
l=strlen(line);
printf("Enter the position of the starting character\n");scanf("%d",&pos);
na=l-pos;printf("Enter No of characters to be deleted ie should be
-
8/3/2019 Cp Lab Manual 2010
36/73
if(nc>na){
printf("The text is deleted from %d till the end",pos);line[pos+1]='\0';
printf("After deletion the string :%s",line);}else{
p=pos+nc;
k=p;for (i=1;i na
Print the text is deletedfrompos to end
P=pos + nc
K=p
For i=1 to
l - k
Line[pos] =line[p]
-
8/3/2019 Cp Lab Manual 2010
37/73
Output:
1) Enter the line of text: ABJ KALAM IS A FORMER PRECIDENT
Enter the position of the starting character14Enter No of characters to be deleted ie should be
-
8/3/2019 Cp Lab Manual 2010
38/73
Exercise 8
Write a C Program that uses functions to perform the following operations usingStructure:
1. Reading a complex number.
2. Writing a complex number.
3. Addition of two complex numbers.
4. Multiplication of two complex numbers.
(Represent complex no using structs).
Algorithm: main()
1. Ch =menu().
2. On ch goto 3, 10 ,17
3. Input comples c1 ie c1 = input().
4. Input complex c2 ie c2 = input().
5. C = add (c1, c2).
6. Print c1, ie output(c1).
7. Print c2, ie output(c2).
8. Print c, ie output(c).
9. Goto step 16.
10. Input comples c1 ie c1 = input().
11. Input complex c2 ie c2 = input().
12. Print c1, ie output(c1).
13. Print c2, ie output(c2).
14. C = add (c1, c2).
15. Print c, ie output(c).
16. Repeat 1 through 16 until ch = 3.
17. Stop.
-
8/3/2019 Cp Lab Manual 2010
39/73
Algorithm: input()
1. Input c.rpart, c.ipart.2. return c.
Algorithm: output()
1. print c.rpart, c.ipart.2. return.
Algorithm: add(comp c1, comp c2)
1. c.rpart = c1.rpart + c2.rpart.
2. c.ipart = c1.ipart + c2.ipart.
3. return c.
Algorithm: mul(comp c1, comp c2)
1. c.rpart = c1.rpart*c2.rpart c1.ipart*c2.ipart.2. c.ipart = c1.rpart*c2.ipart + c1.ipart*c2.rpart.3. return c.
Program
#include#include#include#includestruct compl{
float rpart;float ipart;
};typedef struct compl comp;main(){
comp c1,c2,c;int ch;int menu(void);comp input(void);comp add(comp c1,comp c2);comp mul(comp c1,comp c2);void output(comp c);do{
ch=menu();if(ch==3){
clrscr();printf("\nThank you\n");break;
-
8/3/2019 Cp Lab Manual 2010
40/73
}switch(ch){case 1:
printf("Complex Addition\n");printf("Enter complex 1");c1=input();
printf("Enter complex 2");c2=input();
c=add(c1,c2);printf("\nc1=");output(c1);
printf("\nc2=");output(c2);
printf("Sum is c=");output(c);
break;case 2:
printf("Complex Multiple\n");
printf("Enter complex 1");c1=input();printf("\nEnter complex 2");c2=input();c=mul(c1,c2);
printf("\nc1=");output(c1);
printf("c2=");output(c2);c=mul(c1,c2);
printf("\nOutput/Product c=");
output(c);break;}
}while(1);/*End of do-while*/}
comp input(void){
comp c;printf("Enter Real part & Imaginary part\n");scanf("%f%f",&c.rpart,&c.ipart);return(c);
}
void output(comp c){
printf("r%6.2f+i%6.2f",c.rpart,c.ipart);return;
-
8/3/2019 Cp Lab Manual 2010
41/73
}
comp add(comp c1,comp c2){
comp c;c.rpart=c1.rpart+c2.rpart;c.ipart=c1.ipart+c2.ipart;return(c);
}
comp mul(comp c1,comp c2){
comp c;c.rpart=(c1.rpart*c2.rpart)-(c1.ipart*c2.ipart);c.ipart=(c1.rpart*c2.ipart)+(c1.ipart*c2.rpart);return(c);
}int menu(void){
int ch;while(1){
printf("1. Addition of Complex Numbers");printf("2. Multiplication of Complex Numbers");printf("3. Exit");printf("Enter your choice:");scanf("%d",&ch);if(ch3){
printf("Choice should be between 1 and 3.");
continue;}else
break;}return(ch);
}
Flowchart: main() Start
ch = menu()
Print Thankou
-
8/3/2019 Cp Lab Manual 2010
42/73
Yes
No
1 2
True False
Flowchart: Input() Flowchart: output(comp c)
Start
Input c.rpart,c.ipart
return c
Start
print c.rpart, c.ipart
return
If ch== 3
Swi
tch
ch
Input complexc1
c1 = input()
While(t)
Input complexc1
c1 = input()
Input complexc2
c2 = input()
Input complexc2
c2 = input()
c = add(c1,c2)
c = mul(c1, c2)
Print c1
output(c1)
Print c1
output(c1)
Print c2
output(c2)
Print c2
output(c2)
Print c
output(c)
Print c
output(c)
Stop
-
8/3/2019 Cp Lab Manual 2010
43/73
Flowchart: add(comp c1, comp c2) Flowchart: mul(comp c1, comp c2)
Algorithm: menu()
1. print MENU.
2. Print 1. Addition
3. Print 2. Multiplication
4. Print 3. Exit
5. Input ch
6. If ch < 1 or ch > 3
goto step 1, else 7.
7. return(ch)
Flowchart: menu()
Start
c.rpart = c1.rpart + c2.rpart
c.ipart = c1.ipart + c2.ipart
return c
Start
c.rpart = c1.rpart * c2.rpart c1.ipart * c2.ipart
c.ipart = c1.rpart * c2.ipart + c1.ipart * c2.rpart
return c
Start
-
8/3/2019 Cp Lab Manual 2010
44/73
True
Yes
No
Output:
1. Addition of Complex Numbers2. Multiplication of Complex Numbers3. ExitEnter your choice:1Complex AdditionEnter complex 1Enter Real part & Imaginary part1 1Enter complex 2Enter Real part & Imaginary part1 1
c1=r 1.00+i 1.00c2=r 1.00+i 1.00Sum is c=r 2.00+i 2.001. Addition of Complex Numbers2. Multiplication of Complex Numbers3. ExitEnter your choice:3
While (1)
If ch
3
return ch
MENU1.Addition2.Multiplicatio
n3.Exit
Input ch
-
8/3/2019 Cp Lab Manual 2010
45/73
1. Addition of Complex Numbers2. Multiplication of Complex Numbers3. ExitEnter your choice:2Complex MultipleEnter complex 1Enter Real part & Imaginary part2 2
Enter complex 2Enter Real part & Imaginary part1 1
c1=r 2.00+i 2.00c2=r 1.00+i 1.00Output/Product c=r 0.00+i 4.001. Addition of Complex Numbers2. Multiplication of Complex Numbers3. ExitEnter your choice:3
Exercise 10
a). Write a C Program to find the factorial of given number use both recursive and
non-recursive functions.
i) To find the factorial of a given integer.
ii) To find the GCD (greatest common divisor) of two given integers.
iii) To solve Towers of Hanoi problem.
Non-Recursive Approach:Algorithm:
(1) Input a +ve integer No n.
(2) Check whether n is +ve or not.
i) If n is ve print cant be evaluated the factorial goto step (4).
ii) Else calculate fact i.e., n! call function fact(n).
(3) Print factorial of n.
(4) Stop.
Algorithm for fact():
(1) f = 1
(2) Repeat i=1 to n times.
f = f * i.
-
8/3/2019 Cp Lab Manual 2010
46/73
(3) Return f.
Flowchart: Flowchart: fact(n)
No
Yes No Yes
Program:
#include#includemain(){
int f,n;int fact(int n);clrscr();
printf("Enter a +ve no. to find fact");scanf("%d",&n);if(n
-
8/3/2019 Cp Lab Manual 2010
47/73
1. Enter a +ve no. to find fact6Factorial of 6 is 720
2. Enter a +ve no. to find fact3Factorial of 3 is 6
Recursive Approach
Algorithm:
FlowChart:
Flowchart: fact(n)
Start
Input n
Print n,
fact(n)
Stop
fact(n)
If
n
-
8/3/2019 Cp Lab Manual 2010
48/73
i. /*To find factorial of given integer using recursive */
#include#includelong int fact(int n);main(){
int n;long int fact(int n);clrscr();
printf("n= ");scanf("%d",&n);printf("n!=%1d\n",fact(n));getch();
}long int fact(int n){
if(n
-
8/3/2019 Cp Lab Manual 2010
49/73
ii. Write a C Program to find GCD (Greatest Common Divisor) of two given integers
use both recursive and non-recursive functions.
Recursive Approach
Algorithm:
Flowchart: main()
Flowchart: hcf(p, q)
No
Yes
Program:p
Start
Input 2 Nos,
a, b
gcd=hcf(a, b)
Print a, b,
gcd
Stop
hcf(p, q)
r = p-(p/q2)
If
r=
=0
return(q)
return(r)
hcf(p, q)
-
8/3/2019 Cp Lab Manual 2010
50/73
#include#includemain(){
int a,b,gcd;int hcf();clrscr();
printf("Enter in any 2 numbers whose GCD is to be found\n");scanf("%d%d",&a,&b);
gcd=hcf(a,b);printf("GCD of %4d and %4d is %4d \n",a,b,gcd);getch();
}int hcf(p,q)int p,q;{
int r;r=p-(p/q*q);if(r==0)
return(q);elsehcf(q,r);
return(r);}
Output:
1. Enter in any 2 numbers whose GCD is to be found46 35GCD of 46 and 35 is 11
2. Enter in any 2 numbers whose GCD is to be found25 56GCD of 25 and 56 is 25
iii. Write a 'C' Program to solve tower of HANOI problem using bothRecursive and Non-Recursive.
Recursive Approach
Algorithm:
Flowchart:
Start
-
8/3/2019 Cp Lab Manual 2010
51/73
No
Yes
Program:
#include#includemain(){
int nvalue;char snvalue='L',invalue='C',dnvalue='R';void hanoi();clrscr();
printf("Enter number of disks: ");scanf("%d",&nvalue);
printf("\nTower of Hanoi problem with %d disks \n,nvalue");hanoi(nvalue,snvalue,invalue,dnvalue);
snvalue=Linvalue=C
dnvalue=R
Input
Disks, n
Print Tower of Hanoi problem with
disks, n
hanoi(n, snvalue, invalue,
dnvalue)
Stop
hanoi(n, snvalue, invalue,
dnvalue)
If n!=0
hanoi(n-
1,sndl,dndl,indl)
Print Move disk n from sndl
to dndl
hanoi(n-1, indl, sndl,
dndl)
return
-
8/3/2019 Cp Lab Manual 2010
52/73
printf("\n");getch();
}void hanoi(int n,char sndl,char indl,char dndl){
if(n!=0){
/*Move n-1 disks from starting needle to intermediate needle*/hanoi(n-1,sndl,dndl,indl);
/*Move disk n from start to destination*/printf("Move disk %d from %c to %c \n",n,sndl,dndl);/*Move n-1 disks from intermediate needle to destination needle*/hanoi(n-1,indl,sndl,dndl);
}return;
}
Output:
Enter number of disks: 3Tower of Hanoi problem with 3 disksMove disk 1 from L to RMove disk 2 from L to CMove disk 1 from R to CMove disk 3 from L to RMove disk 1 from C to LMove disk 2 from C to RMove disk 1 from L to RExercise 11
b) Write a C function that uses functions to perform the following:
i. Write a C program that displays the position or index in the string S where thestring T begins or -1 if S doesnt contain T.
Algorithm:
1. Input first string s.
2. Input second string t ( to be searched).
3. Search t is ie found = strstr(s, t).
4. If found then (i) else (iii).
(i) Print t is found is s.
(ii) Goto step 5.
(iii) Print t is not found.
5. Stop.
-
8/3/2019 Cp Lab Manual 2010
53/73
Flowchart:
No
Yes
Program:
#include#include
#include#define L 80#define M 40main(){
char s[L],t[M];char *found;clrscr();
printf("Enter the string S ie first one\n");scanf("%[^\n]",s);getchar();
printf("Enter the string T to be searched ie end string\n");scanf("%[^\n]",t);found=strstr(s,t);if(found)
printf("%s string found in %s string",t,s);else
printf("Not found:-1");
Start
Input first string s
Input enter string to be searched t
Print stringis found
Print notfound
Stop
Search t in s is found = strstr(s, t)
If found
-
8/3/2019 Cp Lab Manual 2010
54/73
getch();
}
Output:
i) Enter the string S ie first oneHELLO HOW ARE YOUEnter the string T to be searched ie end string
LL string found in HELLO HOW ARE YOU string
ii) Enter the string S ie first oneHI HOW ARE YOU, THIS IS PRASAD.Enter the string T to be searched ie end string
NNot found:-1
ii. Write a C program to count the lines, words and characters in a given text.
Algorithm:
1. nw = na = nc = 0.
2. Repeat the following until no character is empty.
i. Input a line of text.
ii. Determine the length of a line is c.
iii. if c = 0 then goto step 3 otherwise goto step iv.
iv. for i = 0 to c repeat the following
a. Check line[i] for space or end of line[\0].
If space or end of line then nw = nw + 1, otherwise goto step 6.
b. nl = nl + 1.
3. Print no of lines nl.
4. Print no of words nw.
5. Print no of characters nc.
6. Stop.
-
8/3/2019 Cp Lab Manual 2010
55/73
Program:
#include;#include c
#include#include#define L 80main(){
char line[L];int ch,i,n,nw,nc,nl,c;clrscr();nw=nc=nl=0;
printf("Enter the text, leave one space");printf(" between the words and press enter at the end of the text.\n");
while(1){getchar();printf("enter string OR press enter to stop\n");scanf("%[^\n]",line);c=strlen(line);if(c==0)break;elsenc+=strlen(line);for(i=0;i
-
8/3/2019 Cp Lab Manual 2010
56/73
printf("\nNo of charactors:%d",nc);printf("\nNo of words:%d",nw);getch();
}
Flowchart:
True
Yes
No
No
Yes
No
Yes
Start
nw = ne = nl = 0
While (1)
c = strlen(line)
If c=
=0
nc = nc + c
for i = 0 to c
Ifline[i]=spaceor line[i]=\0
nw = nw + 1
nl = nl + 1
Print no of lines nl, no of characters c, no of words nw
Input line
-
8/3/2019 Cp Lab Manual 2010
57/73
Output:
Enter the text, leave one space between the words and press enter at the end of the text.
Enter string OR press enter to stopCAN I SPEAK TO Mr.PRASAD.Enter string OR press enter to stop
YA THIS IS PRASAD.Enter string OR press enter to stopMAY I KNOW HOW R UEnter string OR press enter to stopTHIS IS RAMU.Enter string OR press enter to stop
No of lines:4No of charactors:74No of words:18
Exercise 12
a). Write a C program to generate Pascals triangle.
Algorithm
Stop
-
8/3/2019 Cp Lab Manual 2010
58/73
Algorithm: main()
1. Input size of the Pascal Triangle rows / columns m.
2. Call the function ps(pas, m) (to generate Pascal Triangle).
3. Print Pascal Triangle.
4. Call prps(pas, m).
5. Stop.
Algorithm: ps(pas, m)
1. For i=0 to m
2. Pas[i][0] = 1.
3. Pas[i][i] = 1.
4. For j = 0 to i 1.
5. Pas[i][j] = pas[i-1][j-1] + pas[i-1][j].
6. Next j.
7. Next i.
8. Return.
Algorithm: prps()
1. For I = 0 to m.
2. For j = 0 to i.
3. Print pas[i][j].
4. Next j.
5. Next i.
6. Return.
Program:
-
8/3/2019 Cp Lab Manual 2010
59/73
#include#include#include#define MR 20#define ML 30main(){
int m,n,pas[MR][ML];
void ps(int pas[][ML],int m);void prps(int pas[][ML],int m);clrscr();
printf("Enter No of Rows/Columns");scanf("%d%d",&m,&n);
ps(pas,m);
printf("\nPascal Triangle\n");
prps(pas,m);getch();}/*end of main function*/void ps(int pas[][ML],int m){
int i,j;for(i=0;i
-
8/3/2019 Cp Lab Manual 2010
60/73
False
True
False
True
Flowchart: ps(pas, m)
False
True
False
True
Output:
Enter No of Rows/Columns5 5
Pascal Triangle11 11 2 11 3 3 1
Start
for i = 0 to m
pas[i][0] = 1; pas[i][i] = 1
for j = 1 to i -1
Pas[i][i] = pas[i-1][j-1] + pas[i-1][j]
return
-
8/3/2019 Cp Lab Manual 2010
61/73
1 4 6 4 1
b). Write a C program to construct a Pyramid of numbers.
Algorithm:
1. Begin.
2. For i=1 to 10 repeat the following steps
(i) K=1
(ii) For s=1 to 10-I repeat the following steps
Print .
(iii) For j=1 to I repeat the following steps
(a) R=k%10.
(b) Print r.
-
8/3/2019 Cp Lab Manual 2010
62/73
(c) K=k+1.
(iv) K=k-1.
(v) For j=1 to I repeat the following steps
(a) K=k-1
(b) If k==-1 then
K=10
(c) R=k%10
(d) Print r.
(vi) Goto next line.
3. Stop.
Program:
#include#include
#includemain(){
int i,j,k,r,s;clrscr();for(i=1;i
-
8/3/2019 Cp Lab Manual 2010
63/73
k=1;for(s=1;s
-
8/3/2019 Cp Lab Manual 2010
64/73
False
True
No
yes
Output:
1
121123211234321
12345432112345654321
1234567654321123456787654321
123456789876543211234567890987654321
k=k-1
for j=1 to i
k=k+1
If
k==-
1
r=k%10
k=10
Print r
Stop
Print \n
-
8/3/2019 Cp Lab Manual 2010
65/73
Exercise 16
a) Write a C program which copies one file to anotherAlgorithm:
1. Input the program source destination file.
As command line prompt (argv)
2. Check no of parameters ie argc
If argc !=3 goto step 10
3. Copy source file name into sfname
4. Copy destination file into dfname.
5. Open the file ie source file in read mode.
6. Check source file opening of file .
If not, print file cant be opened goto step 10, otherwise goto step 7.
7. Perform till the end of the file reading the following
(i) Read a character from a source file.
-
8/3/2019 Cp Lab Manual 2010
66/73
(ii) Write to destination file.
8. Close source file.
9. Close destination file.
10. Stop.
Program:
#include#include#include#include
#define L 80main(int argc, char *argv[]){
FILE *sfpt,*dfpt;char c;char sfname[L],dfname[L];clrscr();if(argc!=3){
printf("Invalid No of parameters.");exit(1);
}strcpy(sfname,argv[1]);strcpy(dfname,argv[2]);sfpt=fopen(sfname,"r");if(sfpt==NULL)
printf("File can't be opend %s",sfname);else
-
8/3/2019 Cp Lab Manual 2010
67/73
{dfpt=fopen(dfname,"w");if(dfpt==NULL)
printf("File can't be found: %s",sfname);else{
while(!feof(sfpt)){
c=getc(sfpt);
putc(c,dfpt);}
}}fclose(sfpt);fclose(dfpt);getch();
}
Flowchart:
Yes
No
Yes
No
Yes
No
No
Start
C:\> filename sfiledfile
If argc!=3
Copy argv[1] to sfileCopy argv[2] to dfile
Open sfile in readmode
Ifsfpt==NULL
Open dfile
Ifdfpt==NULL
While !feop(sfpt)
Read c from sfptWrite c into dfpt
Close sfile
Stop
Close dfile
-
8/3/2019 Cp Lab Manual 2010
68/73
Yes
W12a.Output:
-
8/3/2019 Cp Lab Manual 2010
69/73
b) Write a C program to reverse the first n characters in a file. (Note: The file name and n arespecified on the command line)
Algorithm:
1. Input C:\> filename, sourcefile.
2. if argc!=3 then goto step (6)
3. Copy argv[1] to sfname
4. Open sfname in read mode
5. if sfpt = = NULL
6. Print file not opened, otherwise goto step (7)
7. Convert string to integer ie i = a to i(argv[2])
8. Read source file buffer ie sfpt into text up to I no of characters.
9. text[n] = \0( NULL Character assignment)
10.j = 1
11.rev[j] = \0
12.j = j 1
13.for i = 0 to l
a. rev [j] = text[i]
b. j = j l
14. Print reversed string : rev
-
8/3/2019 Cp Lab Manual 2010
70/73
15. Close file sfpt
16. Stop.
Program:#include#include#include#include
#include#define L 800main(int argc, char *argv[]){
int l,i,j;char text[L];char rev[L];FILE *sfpt;char c;char sfname[L];int n;clrscr();if(argc!=3){
printf("Invalid No of arguments.\n");exit(0);
}strcpy(sfname,argv[1]);sfpt=fopen(sfname,"r");if(sfpt==NULL){
printf("File not found.\n");exit(1);}else{
i=atoi(argv[2]);n=fread(text,1,i,sfpt);text[n]='\0';l=strlen(text);
-
8/3/2019 Cp Lab Manual 2010
71/73
j=l;rev[j]='\0';
j--;for(i=0;i
-
8/3/2019 Cp Lab Manual 2010
72/73
Flowchart:
Yes
No
Yes
No
False
True
Start
C:\> filename sfiledfile
if argc!=3
Copy argv[1] to sfname
Open sfname in readmode
ifsfpt==NULL
Convert argv[2] tointeger
Print file notfound
n = fread(text, l,
sfpt)
text[n]=\0
l=length(text)
j=1
rev[j]=\0
j = j + 1
for i=0;i
-
8/3/2019 Cp Lab Manual 2010
73/73
Output: