Exercise 2 2’s complement of a number is obtained by seaming it from right to left and complementing all the bits after

the first appearance of a 1. These 2’s complement of 11100 is 00100. Write a C program to find the 2’s complement of a binary number. Algorithm: main()
Input binary no of length 16 bit binary. Copy binary to dup. test = check(binary). If test ==0 then (i) else (iii). (i) Print “Invalid No”. (ii) goto step 5. (iii) Call compl(binary, a). (iv) Print ‘ 2’s 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: 1. 2. 3. 4.

#include<stdio.h> #include<conio.h> #include<dos.h>

#include<string.h> #define L 16 main() { 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<l;i++) { if(!((binary[i]=='0')||(binary[i]=='1'))) x=0; break; } return(x); } void compl(char binary[],char a[]) { int check; /*char a[L];*/ int l,i;

l=strlen(binary); for(i=l-1;i>=0;i--) { if(binary[i]=='0') a[i]='1'; else a[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; }

a) Start l=strlen(binary) False return (x) for i=l – 1 to 0 True if binary[i]=0 Yes .Flowchart: main() Start Flowchart: input(binary) Start Input binary No without space binary return Flowchart: check(binary) No Input Binary No of 16 length Input(binary) Copy binary to dup Test = check(binary) If test = = 0 Yes Print Invalid No Exit(1) Start l = strlen(binary) No for i = 0 to l Yes If ! (binary[i] =’0’ or ‘1’) x=0 Compl(binary. a) No Print 2’s complement Yes return Stop Flowchart: compl(binary.

No a[i] = 0 a[i]=1 for i=l-1 to 0 False True Yes If a[i] = ‘0’ if i=l-1 No Yes a[i] = ‘1’ If check ==1 && a[i] = ‘0’ No No a[i] = ‘0’ a[i] = ‘1’ check = 1 check = 0 If check == 1 && a[i] Yes = ‘1’ a[i] =Yes 0 check = 1 No a[l] = ‘\0’ return Output: Enter Binary no of Max length 16 Enter only 0's and 1's without spaces up to Max 16 Bits 1111 Original Binary No:1111 2's complement is 0001 .

Write a Program in C to find the sum of individuals Digits of a positive Integer. ii. if not goto step 6. Check n is +ve.Exercise 3 a.m=n Repeat until n=0 No r=n%10 S=s+r n=n/10 print m. s+=s+r. S=0. Solution: - Algorithm 1) 2) 3) 4) Input a number n. n=n/10. Repeat the following until n=0. i. Flowchart Start Input n Yes If n< 0 No S=0. s Yes Print not possible Stop . 6) stop.m=n. m. 5) print s. r=n%10. iii.

Write a program a Fibonacci sequence is defined as follows. . sum+=r. Subsequent terms are found by adding the precedes two preceding two terms in the sequence. } getch().Enter an Integer1234 The given number =1234 The sum =10 b.sum. } printf("The given number =%d\n". m/=10. clrscr(). while(m!=0) { r=m%10. m=n.Program #include<stdio.m.&n). printf("Enter an Integer"). else { sum=0. } OutPut 1. Write a C Program to Generate the first ‘n’ terms of the sequence.h> main() { int r. scanf("%d".h> #include<conio.Enter an Integer45 The given number =45 The sum =9 2. if(n<0) printf("The given number is not +ve Integer").n). printf("The sum =%d".n.sum). The first and second terms in the sequence are 0 and 1.

print f c. 2. f=f1+f2 b. f1=0 f2=1 print f1. n to be generated. 5.h> #include<conio. Flowchart Start Input n f1=0. f2 Repeat the following for n-2 times f=f1+f2 Print f f1=f2 f2=f Program #include<stdio. clrscr(). Enter the no of Fibonacci. int i. 4. 3.f. a.n. f2=f 6.f2 repeat the following for n-2 times. Stop . f2=1 Print f1.h> main() { int f1. stop.Solution: Algorithm 1. f1=f2 d.f2.

i=3. else{ printf("%5d%5d".f2). Enter how many +ve Integer fibonacci numbers10 0 1 1 2 3 5 8 13 21 34 2.&n). printf("%5d". f2=1. i++. while(i<=n) { f=f1+f2. for i=1 to n do (a) k=prime(i). Solution: Algorithm: 1.f1=0. . printf("Enter how many (+ve Integer) fibonacci numbers"). where n is a value supplied by the user. Write a C program to generate all the prime numbers between 1 and n. f1=f2. } } getch(). (b)if(k==1) (i) Print ‘prime’. f2=f. Stop.f1. scanf("%d".f). i 3. if(n<0) printf("Count cannot -Ve"). Enter how many +ve Integer fibonacci numbers15 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 C. Enter a no n 2. } Output 1.

h> #include<conio. j++. if(k==1) printf("%d\n". int prime(int n).i<=n. p=sqrt(i). 6.i).h> main() { int k. l=0. if(r==0) then no 6. clrscr().Algorithm: prime(i) 1. goto step 8.n.&n).i++) { k=prime(i). while(j<=p)( repeat 3 through 7) 4. else step 7.i. } getch(). 7.l=1 3. for(i=1. r=i%j 5. j=2. } int prime(int i) { . 8. return(l) Program: #include<stdio.h> #include<math. scanf("%d". printf("Enter the find n to generate prime"). 2.

p=(int)sqrt(i). break. } j++. } Flowchart: start Input n For i=1 to n No yes k=prime(i) No If k= Yes =1 Yes Print ‘i’ stop Prime(i) p=sqrt(i) . if(r==0) { l=0.j.int p.l=1.r. } return(l). while(j<=p) { r=i%j.

l=1 While(j<=p ) False True r=i%j If r==0 No j++ Yes l=0 return(l) Output 1. Enter the find n to generate prime15 1 2 3 5 7 11 13 . Enter the find n to generate prime9 1 2 3 5 7 2.j=2.

. Print sum 6. 3. 4. i) ii. Sum= x2/2!+x4/4!-x6/6!+x8/8!-x10/10! Solution: Algorithm: 1. Input value of x sum = 1. term *= sign v. Write a C program to calculate the following Sum. sum += term vi. Algorithm for fact() (1) (2) (3) (4) (5) Receive n value p=1 Repeat for n times (i = 1 to n) p = p * i. i. nu=pow(x.0 sign = -1 Repeat the following for i=2 to 10 with step 2. de = fact (i) iii. sign*=-1 5. Return p.Exercise 4 a. 2. term = nu/de iv. Stop.

clrscr(). de. sign*=-1.sum). term=nu/de. de=fact(i). int sign=-1. nu. sum=1. getch(). int p=1. int i. term*=sign.h> main() { float x. i). i++) p*=i.Program: #include<stdio. i<=n. return(p). sum+=term. printf("Enter the value of x:"). } int fact(int n) { int i.i+=2) { nu=pow(x. int fact(int n). scanf("%f". for(i=1. &x). j. term.h> #include<math. } printf("Sum=%f". n. } Flowchart: Start .h> #include<conio. for(i=2.i<=10.

Input x sum = 0. sign =-1 fact(n) p=1 Repeat i=2 to 10 step 2 No Repeat I = 1 to n No Yes nu = pow(x. i) Yes p=p*i de = fact(i) term = nu/de sign*=-1 sum+=term Return p Print sum Stop Output: 1.737000 . Enter the value of x:4 Sum=-47.

d. Write a ‘C’ Program to find the roots of a quadratic equation.474480 b. then choice is 1. b. 2) Calculate discriminate d = b2 – 4ac.2. c. 3) If discriminate is 0. . 4) Print a. Algorithm: 1) Enter the values for a. Enter the value of x:2 Sum=-0. c. b. if d is greater than 0 then choice is 2 otherwise choice is 3.

h> #include<conio. x2. choice = 2 goto step 7.h> #include<math.h> main() { . Program: #include<stdio. x2 goto 9. x1 = -b + √d / 2a x2 = .√d / 2a print x1.5) Choose the sequence based on choice = 1 step 6.b . 6) i) print “Equal roots” ii) x1=-b/2*a iii) x2 = -b/2 * a. 7) i) ii) iii) iv) v) print roots are real and unequal. 9) Stop. choice = 3 goto step 8. v) goto step 9. iii) print x2 ie complex root. 8) i) print roots are imaginary ii) print x1 ie complex root. iv) print the roots x1.

x2).d.2f\n".2f\n".&a.float a.c). else if(d>0){ ch=2. printf("x1=%6. x1=x2=-b/(2*a).2f\n".x1). break.c values").2)-4*a*c.2f\n".2f\n".x1. if(d==0) ch=1.&b. scanf("%f%f%f". c d= b2 . case 2: printf("Roots are Real and inequal"). printf("x2=%6.b. int ch. sqrt(-d)/(2*a)). case 3: printf("Imaginary roots \n").&c).x1). x2=(-b-sqrt(d))/(2*a).2f\nC=%6. sqrt(-d)/(2*a)).c.x2. switch(ch) { case 1: printf("Equal Roots").2f+i%6. break.4 ac Yes If d=0 No If d>0 ch = 1 .d). printf("Discriminate D=%6. d=pow(b. printf("A=%6.2f\n".2f\n". b. x1=(-b+sqrt(d))/(2*a). printf("Enter a.a.b.b.-b/(2*a).2f-i%6.2f\nB=%6. printf("x1=%6.-b/(2*a). } else ch=3. clrscr(). printf("x1=x2=%6. break. printf("x2=%6. } Flowchart: Start Enter values a. } getch().

Enter a.00 B= 2. d Swi tch cas e ch 1 Print Equal roots 2 Print roots are real & unequal 3 Print Imaginary roots x1 = x2 = . b.00 Desciminat D=-16.00 . c.b+√d/2a Print x1.20+i 0.b. Enter a.b / 2a x1 = .c values5 2 1 A= 5.40 2. x2 Print x2 is complex Stop Output: 1.40 x2= -0.c values2 4 2 A= 2. x2 x2=-b-√d/2a Print x1 is complex Print x1.20-i 0.b.No ch = 3 Yes ch = 2 Print a.00 c= 1.00 Imaginary roots x1= -0.

3. Algorithm: 1. 6.B= 4. t<= n . t+=it) repeat following steps (i) (ii) d=u*t+a*pow(t. Input time interval it.00 Equal Roots x1=x2= -1. Input initial velocity u. For (t = i .00 Desciminat D= 0. u. 5. Input starting time and final time i & n. 2. .00 c= 2. 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’. Print t . Begin. Print ‘Time Distance U m/sec A m/sec2 ’. d. Write ‘C’ Program to find the distance travelled at regular intervals of time given the values of ‘u’ and ‘a’. 7. 2)/2.00 Exercise 5 a. 4. 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). Input Acceleration a.

&n). printf("Enter time interval"). clrscr(). scanf("%f". float d. Stop.h> #include<math.2)/2.&i. printf(“\n----------------------------------------------\n”).h> #include<conio.&u).2f\t\t%6.a). a. int t. printf("%d\t%. n. scanf("%d%d".d. Program: #include<stdio.t+=it) { d=u*t+a*pow(t. printf("Enter Initial velocity\n").t<=n. scanf("%d".t. printf("Enter Acceleration").h> main() { float u. for(t=i.&it). . it.&a). printf("Time\t Distance\t U m/sec \t a m/sec2)\n").u.2f\n". i. scanf("%f".8.2f\t\t%6. printf("Enter starting time/final time").

d. u.} getch(). a . } Flowchart: Start Input Initial velocity u Input acceleration a Input start time i Input ending time n Input Interval it Repeat t=i to n with step it No Yes d=ut+1/2 at2 Print t.

00 5 82.00 9 238.00 5.50 6 114.00 4.00 4.00 4.00 5.Stop Input: Enter Initial velocity 4 Enter Acceleration: 5 Enter starting time/final time: 1 10 Enter time interval: 1 Output: Time Distance 1 6.00 4.00 7 150.00 4.00 a m/sec2) 5.00 5.00 5.00 4.50 2 18.00 4.00 U m/sec 4.00 5.00 4.50 10 290.00 5.00 5.00 5.00 5.50 8 192.00 4.00 3 34.50 4 56.00 .

xvi. break Print ‘Multiplication’. xii. case *: ix. iii. vi. d. c=a*b. b values. (iv) Print ‘/ Division’. xv. c=a+b. xi. c=a/b. (v) Print ‘% Remainder’. Enter a. c=a-b. b. x. case /: xiii. (i) Print ‘+ Addition’. which takes two integers operands and one operator from the user.Subtraction’. Switch(op) a. Begin 2. 4. xiv. case -: Print ‘Addition’.b) Write a ‘C’ Program./. Print ‘Quotient=’c. ii. Print ‘Difference=’c. (iii) Print ‘* Multiplication’. case +: i.*.% and use Switch Statement ) Algorithm: 1. Print ‘Sum=’c. . iv. c. Print ‘Enter your choice’. performs the operation and prints the result. (Consider the operators +. 3. (ii) Print ‘. (vi) Print ‘E Exit’. viii. Print ‘MENU’. break v.-. Print ‘Product=’c. break Print ‘Division’. vii. break Print ‘Subtraction’. If op==’E’ then goto step 8 otherwise follow the below steps 6. 5.

h> #include<conio. c=a-b.c). op=getchar(). 8. Program: #include<stdio. xx. printf("E Exit\n").h> #include<math. break f.h> main() { int a. Stop. printf("/ Division\n"). c=a+b. b.Subtraction\n"). char op. xvii. switch(op) { case '+': printf("Addition\n"). c. xxi. getchar(). printf(“Enter your choice”). xxii. case %: Print ‘Remainder’. do{ printf("\n\nMENU\n"). break 7. printf(". c=a%b. clrscr(). case '-': printf("Subtraction\n"). while(1) then goto step 3. printf("% Remainder\n"). xviii. printf("Sum=%d\n". xix. Print ‘Remainder=’c. printf("Enter a and b:").&a.&b). default: Print ‘Invalid Option’. printf("* Multiplication\n"). if(op=='E'||op=='e') exit(1). scanf("%d%d". printf("+ Addition\n").e. . break.

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*/ /*End of while*/

} } while(1);

}/*End of main function*/

Flowchart: Start Input a, b Print c sum+ b =a c Print c Diff– b =a c Print product Stopc = a * b Print Quaff c=a/bc Print remainder c=a%b

Swi tch (OP )

Menu + Addition - Subtraction * Multiplication / Division % Remainder E Exit Input choice op If op = ‘E’ No Yes

+

-

*

/

%

Input: Enter a and b: 5 3 Output: MENU + Addition - Subtraction * Multiplication / Division % Remainder E Exit +

Addition Sum=8 MENU + Addition - Subtraction * Multiplication / Division % Remainder E Exit * Multiplication Product=15 MENU + Addition - Subtraction * Multiplication / Division % Remainder E Exit g Invalid Option MENU + Addition - Subtraction * Multiplication / Division % Remainder E 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.

int p.7. printf("The ma in string is \n%s". char s[L]. print After string insertion the main string is: s. stop.char s1[]).h> #include<string.s1[P]. substring can’t be inserted. printf("Enter the Main string:\n"). l1=string length of s1. because it is too long goto step 13. 8."). . input the substring.s). s1) 12. if((p<0)||(p>l)) printf("Substring position is out of the main string. clrscr(). printf("\nPosition of the substring to be inserted:\n"). 13.h> #include<conio. scanf("%[^\n]". l=strlen(s).l. Program: #include<stdio. if l + l1 > L then go to step 10 otherwise go to step 11. 9.h> #define L 80 #define P 40 main() { int p. scanf("%d". 11.i. void inst(char s[]. else { getchar(). p. s1. 10.s).&p).l1. call insert(s.

s1). repeat i = p to i<=p + l1 . last = last – 1.l = length of s. k = 0 15.printf("Enter substring:\n"). k=length s1.s). l1 = k 5. else { inst(s. scanf("%[^\n]". p1 = p1 – 1 13. if(p!=0) then repeat 6 through 14. 4. 14. 8. printf("\nAfter string insertion:\n"). 3."). p1 = l – (p . s[last] = ‘\0’ 7.p.1) 9. printf("%s". s1) 1. last = k+l+1 6. if((l+l1)>L) printf("\nstring can't be inserted because too long.last= last – 1 11. }/*End of main functioin*/ Algorithm: Insert(s. l1=strlen(s1). p. l = l – 1 12.s1). 2. } } getch(). i=p. s[last] = s[l] 10.

i. void inst(char s[].i) s[i] = s1[k] ii) k = k + 1.j. last--. l1=k. last=k+l+1.last.k. p1--.p1. p1=l-(p-1). k=0.int p. } while(p1!=0). 16. k++.char s1[]) { int l1. }/*End of for loop*/ return. l=strlen(s). l--. last--. return. for(i=p. }/*End of inst function*/ .i<p+l1. i=p. do{ s[last]=s[l].i++) { s[i]=s1[k].l. s[last]='\0'. k=strlen(s1).

s1) Print after insertion: s Stop . p.Flowchart: Start Input main string: s l=strlen(s) print sub string: s1 Input substring p Yes If p<0 and p>l No Input substring s1 Print subs teing of a main string l1=strlen(s1) Yes No If l +l1>l Print string can’t be inserted Insert(s.

1 P1 = l – (p .1 Yes While(p! =0) N0 K=0 For i=p to p+ l1 Yes S[i]= s1[k] K=k+1 return No Output: Enter the Main string: Java is oops language The main string is . s1) Start L=strlen(s) i=p K=strlen(s1) Last=k+l+1 S(last)=’\0’ Last=last . p. l =l-1 P1 = p1 .Flowchart: Insert(s.1) S(last) = s[i] Last = last – 1.

input No of characters to be deleted nc. 6. Algorithm: 1. 5.Java is oops language Position of the substring to be inserted: 8 Enter substring: an After string insertion: Java is an oops language ii) To delete n Characters from a given position in a given string. Determine length of line l. if nc > na then perform the following otherwise goto step 7. . 4. na = l – pos. Input text line. 3. input position of starting character pos. 2.

10.&pos). . printf("Enter the position of the starting character\n"). ii) line[pos]=’\0’. 8.&nc).h> #include<string. ii) pos=pos+1 iii) p = p + 1. 9.h> #include<conio.n.line). scanf("%[^\n]". printf("Enter the line of text:"). 11. 7.l. scanf("%d".i.nc.na. stop. k = p. for i=1 to l – k perform the following i) line[pos]=line[p]. na=l-pos. 12. line[pos]=’\0’. l=strlen(line). print After deleting line is line.pos.na). clrscr(). scanf("%d".i) print text deleted from pos to end. p = pos + nc. int p. Program: #include<stdio.k.h> #define ML 80 main() { char line[ML].s. printf("Enter No of characters to be deleted ie should be <=%d".

line[pos+1]='\0'.i++) { line[pos]=line[p].i<=l-k. p++. } }/*End of main function*/ Flowchart: Start Input a line of text line L=strlen(line) Input position of selecting character pos Input No of characters to be deleted nc na = l – pos If nc > na Yes No P=pos + nc K=p Print the text is deleted from pos to end For i=1 to l-k Yes Line[pos] = line[p] No .line). printf("After deletion the string :%s".line). for (i=1. } else { p=pos+nc. getch(). printf("After Deletion the string is :%s".if(nc>na) { printf("The text is deleted from %d till the end". k=p.pos). } line[pos]='\0'. pos++.

Line[pos+]=’\0’ Pos = pos + 1 P=p+1 Line[pos] = ‘\0’ Print After deletion of a line stop Output: 1) Enter the line of text: ABJ KALAM IS A FORMER PRECIDENT Enter the position of the starting character 14 Enter No of characters to be deleted ie should be <=17 18 The text is deleted from 14 till the end. After deletion the string: ABJ KALAM IS A 2) Enter the line of text: PRASAD BV Enter the position of the starting character 6 Enter No of characters to be deleted ie should be <=3 2 After Deletion the string is: PRASADV .

5. ie output(c). 7. ie output(c1). ie output(c2). 11. 4. 3. Print c2. 10 . Reading a complex number. Algorithm: main() 1. c2). Print c. Stop. Writing a complex number. 16. Input complex c2 ie c2 = input(). C = add (c1. (Represent complex no using structs). c2). Input comples c1 ie c1 = input(). ie output(c2). . Print c. 17. 15. 12. C = add (c1. 14. Repeat 1 through 16 until ch = 3. On ch goto 3. 6. Multiplication of two complex numbers. Ch =menu(). Input comples c1 ie c1 = input(). Addition of two complex numbers. 13. ie output(c). 2. 4. Print c1. Print c1. 10.17 3. Input complex c2 ie c2 = input(). Goto step 16.Exercise 8 Write a C Program that uses functions to perform the following operations using Structure: 1. 8. 2. Print c2. ie output(c1). 9.

c.h> #include<math. typedef struct compl comp.rpart*c2.ipart + c2.rpart = c1. . comp add(comp c1.ipart. 2. comp c2) 1. 3. main() { comp c1.Algorithm: input() 1.rpart. c.rpart.c2.h> #include<dos.rpart = c1. Program #include<stdio. c. Input c. 2. 2. 2.rpart – c1. do{ ch=menu().ipart.rpart*c2. int menu(void). comp input(void). c. printf("\nThank you\n"). int ch.ipart*c2.comp c2). Algorithm: add(comp c1.ipart = c1. if(ch==3) { clrscr(). comp c2) 1. c. void output(comp c).rpart. c.rpart + c2. }. break. print c.h> struct compl { float rpart.ipart + c1. comp mul(comp c1.ipart = c1. return. c.ipart.rpart. return c. Algorithm: output() 1. Algorithm: mul(comp c1.comp c2). return c. 3.h> #include<conio.ipart. return c.ipart*c2. float ipart.

ipart). printf("Enter complex 1").c2).ipart). output(c). printf("Enter Real part & Imaginary part\n").&c. printf("\nc1="). printf("\nEnter complex 2"). output(c2).c2). c=mul(c1. printf("Enter complex 1"). printf("\nOutput/Product c="). return. break. output(c1). case 2: printf("Complex Multiple\n"). . c2=input(). c=add(c1. c=mul(c1.rpart.2f". c1=input(). printf("\nc2=").c./*End of do-while*/ } comp input(void) { comp c. output(c2).rpart. c2=input().&c. printf("\nc1="). output(c).c. break. } }while(1). scanf("%f%f".} switch(ch) { case 1: printf("Complex Addition\n"). output(c1).c2). printf("Sum is c="). c1=input(). } void output(comp c) { printf("r%6. printf("Enter complex 2").2f+i%6. return(c). printf("c2=").

ipart).ipart=(c1.&ch). scanf("%d".rpart=c1.rpart). Exit"). printf("Enter your choice:"). c. } int menu(void) { int ch. } else break.rpart=(c1.} comp add(comp c1. c. c. return(c).comp c2) { comp c.rpart.rpart*c2.rpart+c2. } return(ch).ipart.rpart)-(c1. Multiplication of Complex Numbers"). c.ipart)+(c1. printf("2. printf("3. } Flowchart: main() Start ch = menu() Print ‘Thank . if(ch<1||ch>3) { printf("Choice should be between 1 and 3.ipart+c2.comp c2) { comp c.ipart=c1.rpart*c2. continue.ipart*c2. return(c)."). Addition of Complex Numbers"). } comp mul(comp c1.ipart*c2. while(1) { printf("1.

ipart print c. c2) Print c1 output(c1) Print c2 output(c2) Print c output(c) False While(t) Flowchart: Input() Start Flowchart: output(comp c) Stop Start Input c.rpart.rpart.ipart return c return . c2) Print c1 output(c1) Print c2 output(c2) Print c output(c) True 2 Input complex c1 c1 = input() Input complex c2 c2 = input() c = mul(c1.If ch == 3 No Yes Swi tch ch 1 Input complex c1 c1 = input() Input complex c2 c2 = input() c = add(c1. c. c.

Exit 5.rpart = c1. Print 2.rpart c.rpart – c1. Input ch 6. return(ch) Flowchart: menu() Start .ipart + c2.rpart * c2.rpart + c2. 2.ipart * c2. 7. Addition 3. comp c2) Flowchart: mul(comp c1. print MENU.Flowchart: add(comp c1. Print 1. Print 3.ipart c.ipart = c1.ipart + c1. comp c2) Start Start c.rpart return c return c Algorithm: menu() 1.ipart c. If ch < 1 or ch > 3 goto step 1.ipart * c2.rpart * c2.rpart = c1. Multiplication 4.ipart = c1. else 7.

Exit Enter your choice:1 Complex Addition Enter complex 1Enter Real part & Imaginary part 11 Enter complex 2Enter Real part & Imaginary part 11 c1=r 1.00+i 1.00 c2=r 1.Addition 2. Exit Enter your choice:3 . Multiplication of Complex Numbers 3.While (1) True MENU 1.00Sum is c=r 2.Multiplicatio n 3. Multiplication of Complex Numbers 3. Addition of Complex Numbers 2.00 1. Addition of Complex Numbers 2.00+i 1.00+i 2.Exit Input ch Yes If ch<1 or ch > 3 No return ch Output: 1.

i) To find the factorial of a given integer.e. ii) To find the GCD (greatest common divisor) of two given integers.00+i 1. iii) To solve Towers of Hanoi problem. Write a ‘C’ Program to find the factorial of given number use both recursive and non-recursive functions. Else calculate fact i. n! call function fact(n). (4) Stop. i) ii) If n is –ve print can’t be evaluated the factorial goto step (4). f = f * i. Exit Enter your choice:3 Exercise 10 a).00 Output/Product c=r 0. Addition of Complex Numbers 2. Exit Enter your choice:2 Complex Multiple Enter complex 1Enter Real part & Imaginary part 22 Enter complex 2Enter Real part & Imaginary part 11 c1=r 2.00c2=r 1. (3) Print factorial of n.00+i 4.00 1.1. Algorithm for fact(): (1) f = 1 (2) Repeat i=1 to n times. .00+i 2. Addition of Complex Numbers 2.. Multiplication of Complex Numbers 3. (2) Check whether n is +ve or not. Multiplication of Complex Numbers 3. Non-Recursive Approach: Algorithm: (1) Input a +ve integer No n.

(3) Return f.i++) k*=i.&n).n. Flowchart: Start Flowchart: fact(n) Start Enter a +ve no n f=1 Yes if n<0 No f=fact(n) for i = 1 to n No Yes f*=i Print can’t calculate Print f.h> #include<conio. for(i=1.f). if(n<0) printf("Can't be determined fact for %d". printf("Factorial of %d is %d". else f=fact(n).h> main() { int f. getch().k=1. to find fact"). printf("Enter a +ve no.n). n Return f Program: Stop #include<stdio. } Output: . clrscr().n. } int fact(int n) { int i. return(k). int fact(int n).i<=n. scanf("%d".

fact(n) Stop Flowchart: fact(n) fact(n) If n<= 1 return(n*fact(n1)) return(1) . Enter a +ve no. Enter a +ve no. to find fact3 Factorial of 3 is 6 Recursive Approach Algorithm: FlowChart: Start Input n Print n. to find fact6 Factorial of 6 is 720 2.1.

else return(n*fact(n-1)). printf("n!=%1d\n". printf("n= "). } long int fact(int n) { if(n<=1) return(1). getch(). scanf("%d". main() { int n.&n). long int fact(int n).i.h> #include<conio. clrscr(). } Output: 1. n= 10 n!=24320 .fact(n)). n= 5 n!=120 2.h> long int fact(int n). /*To find factorial of given integer using recursive */ #include<stdio.

a. gcd Stop Flowchart: hcf(p. q) return(r) Program:p . b. b gcd=hcf(a. q) hcf(p.ii. q) r = p-(p/q2) If r= =0 return(q ) No Yes hcf(p. Recursive Approach Algorithm: Flowchart: main() Start Input ‘2 No’s’. Write a ‘C’ Program to find GCD (Greatest Common Divisor) of two given integers use both recursive and non-recursive functions. b) Print a.

gcd=hcf(a.b. printf("GCD of %4d and %4d is %4d \n". Enter in any 2 numbers whose GCD is to be found 25 56 GCD of 25 and 56 is 25 iii. Write a 'C' Program to solve tower of HANOI problem using both Recursive and Non-Recursive. getch().q. } int hcf(p. Recursive Approach Algorithm: Flowchart: Start . { int r. clrscr().#include<stdio. if(r==0) return(q).h> main() { int a. r=p-(p/q*q). } Output: 1.gcd.a.h> #include<conio.&b).q) int p. else hcf(q.&a.b. int hcf(). printf("Enter in any 2 numbers whose GCD is to be found\n").b).gcd). scanf("%d%d". return(r). Enter in any 2 numbers whose GCD is to be found 46 35 GCD of 46 and 35 is 11 2.r).

sndl. invalue.dnvalue='R'. printf("Enter number of disks: "). snvalue.invalue='C'.dnvalue). hanoi(nvalue. char snvalue='L'. clrscr(). void hanoi(). scanf("%d".&nvalue). printf("\nTower of Hanoi problem with %d disks \n.h> main() { int nvalue.sndl. snvalue.dndl. n Print ‘Tower of Hanoi problem with disks’. . dndl) return Program: #include<stdio. n hanoi(n.invalue.snvalue.h> #include<conio. invalue. dnvalue) If n!=0 No Yes hanoi(n1. indl.indl) Print ‘Move disk n from sndl to dndl’ hanoi(n-1. dnvalue) Stop hanoi(n.snvalue=’L’ invalue=’C’ dnvalue=’R’ Input ‘Disks’.nvalue").

(i) (ii) (iii) 5. /*Move disk n from start to destination*/ printf("Move disk %d from %c to %c \n". getch(). 4.dndl).sndl. } Output: Enter number of disks: 3 Tower of Hanoi problem with 3 disks Move disk 1 from L to R Move disk 2 from L to C Move disk 1 from R to C Move disk 3 from L to R Move disk 1 from C to L Move disk 2 from C to R Move disk 1 from L to R Exercise 11 b) Write a C function that uses functions to perform the following: i.printf("\n"). 3.n. . Print t is found is s. Goto step 5.dndl).indl).char sndl.char indl. Algorithm: 1. t).dndl. } void hanoi(int n. Write a C program that displays the position or index in the string S where the string T begins or -1 if S doesn’t contain T. } return. Search t is ie found = strstr(s. Input first string s.sndl.char dndl) { if(n!=0) { /*Move n-1 disks from starting needle to intermediate needle*/ hanoi(n-1. Input second string t ( to be searched). Print t is not found. If found then (i) else (iii). Stop. /*Move n-1 disks from intermediate needle to destination needle*/ hanoi(n-1.indl. 2.sndl.

t) No If found Yes Print string is found Print not found Program: Stop #include<stdio.t[M]. else printf("Not found:-1"). found=strstr(s.t).h> #include<string. printf("Enter the string T to be searched ie end string\n").s). clrscr(). scanf("%[^\n]".t. . if(found) printf("%s string found in %s string".h> #include<conio.s). scanf("%[^\n]". char *found.h> #define L 80 #define M 40 main() { char s[L]. getchar().Flowchart: Start Input first string s Input enter string to be searched t Search t in s is found = strstr(s. printf("Enter the string S ie first one\n").t).

getch(). for i = 0 to c repeat the following a. b. otherwise goto step 6. nl = nl + 1. 6. Print no of lines nl. words and characters in a given text. 2. ii. Write a C program to count the lines. if c = 0 then goto step 3 otherwise goto step iv. Input a line of text. . Determine the length of a line is c. Check line[i] for space or end of line[‘\0’]. 4. Print no of words nw. Algorithm: 1. Stop. Repeat the following until no character is empty. Print no of characters nc. 3. 5. iii. Enter the string T to be searched ie end string N Not found:-1 ii. iv. } Output: i) Enter the string S ie first one HELLO HOW ARE YOU Enter the string T to be searched ie end string L L string found in HELLO HOW ARE YOU string ii) Enter the string S ie first one HI HOW ARE YOU. If space or end of line then nw = nw + 1. i. THIS IS PRASAD. nw = na = nc = 0.

for(i=0. if(c==0)break.nl. c=strlen(line). clrscr(). printf("enter string OR press enter to stop\n").nc. leave one space").\n"). scanf("%[^\n]". } /*end of while*/ /*clrscr().*/ printf("\nNo of lines:%d". #include <ctype. while(1) { getchar().c. strset(line.h>.h>c #include<conio. printf(" between the words and press enter at the end of the text.h> #define L 80 main() { char line[L]. nw=nc=nl=0.' ').nl).n.Program: #include<stdio.nw. .line).h> #include<string.i<=c.i. else nc+=strlen(line). printf("Enter the text. int ch.i++) if(isspace(line[i])||(line[i]=='\0')) nw++. nl++.

printf("\nNo of words:%d".printf("\nNo of charactors:%d". } Flowchart: Start nw = ne = nl = 0 While (1) True Input line c = strlen(line) Yes If c= =0 No nc = nc + c for i = 0 to c Yes If line[i]=space or line[i]=’\0’ Yes nw = nw + 1 No No nl = nl + 1 Print no of lines nl. no of words nw .nw).nc). no of characters c. getch().

PRASAD. Enter string OR press enter to stop MAY I KNOW HOW R U Enter string OR press enter to stop THIS IS RAMU. Enter string OR press enter to stop CAN I SPEAK TO Mr. Enter string OR press enter to stop YA THIS IS PRASAD. Write a C program to generate Pascal’s triangle. Algorithm .Output: Stop Enter the text. leave one space between the words and press enter at the end of the text. Enter string OR press enter to stop No of lines:4 No of charactors:74 No of words:18 Exercise 12 a).

Next j. For I = 0 to m. Return. 5. Next j. Call prps(pas. For j = 0 to i – 1. 6. Input size of the Pascal Triangle rows / columns m. Print Pascal Triangle. Pas[i][i] = 1. 5. 5. 7. Print pas[i][j]. Call the function ps(pas. 4. 8. Next i. Program: . m) (to generate Pascal Triangle). 4. 2. For i=0 to m 2. Next i. Algorithm: ps(pas. 3. 2. m) 1. Stop. 6. 3.Algorithm: main() 1. 3. 4. Algorithm: prps() 1. Pas[i][j] = pas[i-1][j-1] + pas[i-1][j]. m). Pas[i][0] = 1. For j = 0 to i. Return.

}/*end of main function*/ void ps(int pas[][ML]. I to m For j = 0 to return iPrint pas [i][j] Flowchart: Start Input No of Rows Stop Print Prps(pas. }/*end of the function*/ void prps(int pas[][ML].int m) { int i. clrscr().pas[MR][ML]. m) Start For I = 0.&n).j++) pas[i][j]=pas[i-1][j-1]+pas[i-1][j].pas[i][j]).int m).j<i.n.j. scanf("%d%d". void prps(int pas[][ML]. for(i=0.h> #include<conio. void ps(int pas[][ML]. for(i=0.int m) { int i. printf("\n").i++) { pas[i][0]=1. getch().m).m) “Pascal Triangle” Ps(pas. prps(pas.h> #include<dos.i<m.j.int m). }/*end of for loop*/ return.h> #define MR 20 #define ML 30 main() { int m.j++) printf("%3d".m). ps(pas. for(j=1. pas[i][i]=1. printf("\nPascal Triangle\n").i<m.j<=i. m) columns . printf("Enter No of Rows/Columns"). } return.&m.i++) { for(j=0. }/*end of functi on*/ Flowchart: main() prps(pas.#include<stdio.

m) Start False for i = 0 to m True pas[i][0] = 1.False True False True Flowchart: ps(pas. pas[i][i] = 1 for j = 1 to i -1 True False Pas[i][i] = pas[i-1][j-1] + pas[i-1][j] return Output: Enter No of Rows/Columns5 5 Pascal Triangle 1 1 1 1 2 1 1 3 3 1 .

2. (b) Print ‘r’.1 4 6 4 1 b). Write a C program to construct a Pyramid of numbers. Algorithm: 1. Begin. . For j=1 to I repeat the following steps (a) R=k%10. For i=1 to 10 repeat the following steps (i) (ii) K=1 For s=1 to 10-I repeat the following steps Print ‘ (iii) ‘.

Goto next line. Stop.j. (iv) (v) K=k-1.r.k.h> #include<stdlib.h> #include<conio. for(i=1.(c) K=k+1. Program: #include<stdio.i<=10.s. 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) 3.h> main() { int i. clrscr().i++) { .

j<i.j++) { r=k%10. printf("%d". }/*end of for loop3*/ printf("\n").k=1.s++) printf(" "). r=k%10. }/*end of main function*/ Flowchart: Start for i=1 to 10 True k=1 for s=1 to 10-i True Print ‘ ‘ False False for j=1 to i True r=k%10 Print r k=k+1 False . for(j=1.j++) { k--.s<=10-i. if(k==-1) k=10. printf("%d". for(j=1.r).j<=i. k++. }/*end of for loop1*/ getch(). for(s=1.r). }/*end of for loop2*/ k--.

k=k-1 for j=1 to i False True k=k+1 If k==1 k=10 r=k%10 Print ‘r’ Print ‘ \n’ Stop No yes Output: 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321 1234567890987654321 .

Perform till the end of the file reading the following (i) Read a character from a source file.Exercise 16 a) Write a C program which copies one file to another Algorithm: 1. Open the file ie source file in read mode. If not. 5. 7. Check no of parameters ie argc If argc !=3 goto step 10 3. 6. Copy destination file into dfname. otherwise goto step 7. . Check source file opening of file . Copy source file name into sfname 4. Input the program source destination file. print file can’t be opened goto step 10. As command line prompt (argv) 2.

8."). char c.h> #include<conio.argv[2]). sfpt=fopen(sfname. 9. if(sfpt==NULL) printf("File can't be opend %s". Program: #include<stdio. Close source file. strcpy(dfname. Stop. if(argc!=3) { printf("Invalid No of parameters."r").h> #define L 80 main(int argc. clrscr().argv[1]).h> #include<string. Close destination file. char sfname[L]. } strcpy(sfname.(ii) Write to destination file. 10. char *argv[]) { FILE *sfpt.*dfpt.sfname). else .h> #include<dos.dfname[L]. exit(1).

sfname). } } Start C:\> filename sfile dfile If argc!=3 Flowchart: Copy argv[1] to sfile Copy argv[2] to dfile Open sfile in read mode Yes } fclose(sfpt). else { while(!feof(sfpt)) { c=getc(sfpt). fclose(dfpt). getch(). putc(c."w"). if(dfpt==NULL) printf("File can't be found: %s".dfpt).{ dfpt=fopen(dfname. } If No sfpt==NULL Open dfile If dfpt==NULL Yes While !feop(sfpt) No Read c from sfpt Write c into dfpt Yes No Close sfile Close dfile Stop No .

Yes W12a.Output: .

if argc!=3 then goto step (6) 3. Copy argv[1] to sfname 4.b) Write a C program to reverse the first n characters in a file. (Note: The file name and n are specified on the command line) Algorithm: 1. rev [j] = text[i] b. Print file not opened. if sfpt = = NULL 6. j = 1 11. Convert string to integer ie i = a to i(argv[2]) 8. Input C:\> filename. Read source file buffer ie sfpt into text up to I no of characters. 9. j = j – 1 13. 2. for i = 0 to l a. otherwise goto step (7) 7. Open sfname in read mode 5. text[n] = ’\0’( NULL Character assignment) 10. sourcefile. j = j – l 14. Print reversed string : rev . rev[j] = ‘\0’ 12.

exit(0). Stop.h> #include<conio."r").h> #include<stdlib.\n"). char sfname[L]. . char c. Program: #include<stdio.j.1. sfpt=fopen(sfname.h> #include<dos. Close file sfpt 16. char *argv[]) { int l. clrscr(). exit(1). } strcpy(sfname. char rev[L]. text[n]='\0'.h> #define L 800 main(int argc. if(sfpt==NULL) { printf("File not found.argv[1]).i.i. char text[L]. } else{ i=atoi(argv[2]).sfpt). n=fread(text. int n. FILE *sfpt.\n").h> #include<string. if(argc!=3) { printf("Invalid No of arguments.15. l=strlen(text).

i<l.j=l. } /*getch().rev).i++) { rev[j]=text[i]. j--. } printf("Reversed String :\n %s". j--. for(i=0.*/ } . fclose(sfpt). rev[j]='\0'.

i<l.i++ True rev[i] = text[i] j-- False Pprint reversed string rev Stop .Flowchart: Start C:\> filename sfile dfile if argc!=3 No Copy argv[1] to sfname Open sfname in read mode if sfpt==NULL Convert argv[2] to No integer n = fread(text. l. sfpt) Yes Yes Print file not found text[n]=’\0’ l=length(text) j=1 rev[j]=’\0’ j=j+1 for i=0.

Output: .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.