Você está na página 1de 46

CONTENTS

Week 1 a. To find the sum of individual digits of a positive integer b. To generate the first n terms of the Fibonacci sequence c. To generate all the prime numbers between 1 and n Week 2 a. To calculate the Sum=1- X2/2! + X4/4! X6/6! + X8/8! X10/10! b. To find the roots of the Quadratic equation Week 3 a. i) To find the factorial of a given integer using Recursive and NonRecursive Functions ii) To find the GCD (greatest common divisor) of two given integers Using Non-Recursive Using Recursive iii) To Solve Towers of Hanoi Problem Week 4 a. To find the distance traveled at regular intervals of given u and a b. To perform the operation on two integer operands using an operator Week 5 a. To find both the largest and smallest number in a list of integers b. i) Addition of Two Matrices ii) Multiplication of Two Matrices Week 6 a. Using functions i) To insert a sub-string in to given main string from a given position ii) To delete n characters from a given position in a given string b. To determine if the given string is a palindrome or not Week 7 a. To display the position or index in the string S where the string T begins b. To count the lines, words and characters in a given text Week 8 a. To generate Pascals triangle b. To construct a pyramid of numbers Week 9 To compute the sum of this geometric expression by reading x and n 1+x+x2+x3+..............................+xn

Week 10 a. To find the 2s complement of a binary number b. To convert a Roman numeral to its decimal equivalent Week 11 Using functions i) ii) iii) iv)

Reading a complex number Writing a complex number Addition of two complex numbers Multiplication of two complex numbers

Week 12 a. To copy one file to another b. To reverse the first n characters in a file Week 13 On Singly linked list i) Creation ii) Insertion iii) Deletion iv) Traversal Week 14 On doubly linked list i) Creation ii) Insertion iii) Deletion iv) Traversal in both ways Week 15 Implementing Stack(its operations) using i) Arrays ii) Pointers Week 16 Implementing Queue(its operations) using i) Arrays ii) Pointers Week 17 Using Stack operations i) Converting infix expression into postfix expression ii) Evaluating the postfix expression Week 18 Using functions i) Creating a binary Tree of integers ii) Traversing the above binary tree in preorder, in order and post order Week 19 i) Linear search Using Non-Recursive function Using Recursive function ii) Binary search Using Non-Recursive function Using Recursive function

Week 20 i) ii) Bubble sort Quick sort

Week 21 i) ii) Insertion sort Merge sort

Week 22 Implementing the Lagrange interpolation and Newton-Gregory forward interpolation Week 23 Implementing the Linear regression and polynomial regression Week 24 Implementing Trapezoidal and Simpson methods

Write a C program to find the sum of individual digits of a positive integer


AIM: To Print the sum of individual numbers of a given number ALGORITHM:

Step Step Step Step Step

Start Accept i, num, sum=0, rem, n, s=0, number Read the number Store the number in num while(num>0) begin rem=num%10; sum=sum+rem; num=num/10; end Step 6: Assign nsum Step 7: if(sum>10) begin while(sum>0) begin rem=sum%10; s=s+rem; sum=sum/10; end Write s end else write sum Step 8: Stop

1: 2: 3: 4: 5:

FLOWCHART

PROGRAM /* write a C program to find the sum of individual digits of a positive integer */ #include<stdio.h> #include<conio.h> void main() { int i,num,sum=0,rem,n,s=0,number; clrscr(); printf("Enter any positive integer:"); scanf("%d",&num); number=num; while(num>0) { rem=num%10; sum=sum+rem; num=num/10; } n=sum; if(sum>10) { while(sum>0) { rem=sum%10; s=s+rem; sum=sum/10; } printf("sum of individual digits of %d is %d which is again %d",number,n,s); } else printf("sum of individual digits of %d is %d",number,sum); getch(); } OUTPUT: Enter any positive integer: 145 sum of individual digits of 145 is 10 which is again 1 Enter any positive integer: 23 Sum of individual digits of 23 is 5

Write a C program to generate the first n terms of the Fibonacci sequence


AIM: To Print the first n terms of the Fibonacci sequence

ALGORITHM:

Step Step Step Step

Start Declare first=0,second=1,third,n,i Read the no. of terms(n) for(i=1;i<=n-2;i++) begin thirdfirst+second; Write third Firstsecond; Secondthird; end Step 5: Stop FLOWCHART

1: 2: 3: 4:

PROGRAM /* A fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1.

Subsequent terms are found by adding the preceding two terms in the sequence.Write a C program to generate the first n terms of the sequence */ #include<stdio.h> #include<conio.h> void main() { int first=0,second=1,third,n,i; clrscr(); printf("Enter the no. of terms:"); scanf("%d",&n); printf("\nThe fibonacci sequence upto %d terms are:\n",n); printf("\n%d %d",first,second); for(i=1;i<=n-2;i++) { third=first+second; first=second; second=third; printf(" %d",third); } getch(); } OUTPUT: Enter the no. of terms: 10 The fibonacci sequence upto 10 terms are: 0 1 1 2 3 5 8 13 21 34

Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user
AIM: To print all the prime numbers between 1 and n ALGORITHM: Step 1: Start Step 2: Declare i, j, n, count Step 3: Read the number(n) Step 4: for(i=1;i<=n;i++) begin count=0;

for(j=1;j<=i;j++) begin if(i%j==0) count++; end end if(count<=2) Print i Step 5: Stop

FLOWCHART

PROGRAM /* Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user */ #include<stdio.h> #include<conio.h> void main() { int i,j,n,count; clrscr(); printf("\nEnter a number:"); scanf("%d",&n); printf("\nThe prime numbers between 1 and %d are:\n",n);

for(i=1;i<=n;i++) { count=0; for(j=1;j<=i;j++) { if(i%j==0) count++; } if(count<=2) printf(" %d",i); } getch(); } OUTPUT: Enter a number:20 The prime numbers between 1 and 20 are: 1 2 3 5 7 11 13 17 19

Write a C program to find the sum of 1- X2/2! + X4/4! X6/6! + X8/8! X10/10!
AIM: To find the sum of 1- X2/2! + X4/4! X6/6! + X8/8! X10/10! ALGORITHM Step 1: Start Step 2: Declare i,j,x,fact=1.00,s,total=0.00,val; Step 3: Read the x value Step 4: for(i=2;i<=10;i++) begin count=0; for(j=1;j<=i;j++) fact=fact*j s= pow(x,i)/fact if(i==2) s=1-s else if(i==6||i==10) s=(-s)

total=total+s end Step 5: val=1+total Step 6: Print total Step 7: Stop

FLOWCHART

Write a C program to find the roots of a quadratic equation


AIM: To print the roots of a quadratic equation ALGORITHM Step 1: Start Step 2: Declare a,b,c,r1,r2,d,x,y Step 3: Read the values for the coefficients of the QE (a,b,c) Step 4: d(b*b)-(4*a*c) Step 5: if(d==0) begin r1r2 -b/(2*a); write r1,r2 write roots are real and equal end Step 6: else if(d>0) begin r1=(-b+sqrt(d))/(2*a); r2=(-b-sqrt(d))/(2*a); write r1,r2 write roots are real and distinct

end Step 7: else if(d<0) write roots are imaginary Step 8: Stop FLOWCHART

PROGRAM /* Write a C program to find the roots of a quadratic equation */ #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,r1,r2,d; clrscr(); printf("Enter the values of a,b,c:"); scanf("%f%f%f",&a,&b,&c); d=(b*b)-(4*a*c); if(d==0) { r1=r2=-b/(2*a); printf("r1=%f , r2=%f",r1,r2); printf("\nroots are real and equal"); } else if(d>0) {

r1=(-b+sqrt(d))/(2*a); r2=(-b-sqrt(d))/(2*a); printf("r1=%f , r2=%f",r1,r2); printf("\nroots are real and distinct"); } else if(d<0) Printf(\nroots are imaginary); getch(); } OUTPUT: Enter the values of a,b,c: 4 4 1 r1=-0.500000 , r2=-0.500000 roots are real and equal Enter the values of a,b,c: 5 4 2 roots are imaginary Enter the values of a,b,c: 4 5 1 r1=-0.250000 , r2=-1.000000 roots are real and distinct

Write a C program to find the factorial of a given integer using Recursive and Non-Recursive functions
AIM: To print the factorial of a given integer using recursive and non recursive method ALGORITHM Step 1: Start Step 2: Declare fact=1 Step 3: Read any integer(n) Step 4: if (n==0) then write factorial as 1 else begin write factorial as rec_fact(n) write factorial as nonrec_fact(n) end Step 5: Stop ALGORITHM FOR RECURSIVE FUNCTION Step 1: int rec_fact(int n) Step 2: return (n>=1) ? n* rec_fact(n-1) : 1 Step 3: Exit ALGORITHM FOR NON-RECURSIVE FUNCTION Step 1: int nonrec_fact(int n) Step 2: for(i=1;i<=n;i++) fact*=i;

return fact; Step 3: Exit

FLOW CHART

FLOW CHART FOR THE RECURSIVE & NON RECURSIVE FUNCTIONS

PROGRAM /* Write a C program to find the factorial of a given integer using Recursive and Non-Recursive method */ #include<stdio.h> #include<conio.h> void main() { int i,n; clrscr(); printf("\nEnter any integer: "); scanf("%d",&n); if(n==0) printf("Factorial of 0 is 1"); else { printf("\nFactorial of %d using Recursive method is %d",n,rec_fact(n)); printf("\nFactorial of %d using Non-Recursive method is %d",n,nonrec_fact(n)); } getch(); } int rec_fact(int n) {

return (n>=1) ? n* rec_fact(n-1) : 1; } int nonrec_fact(int n) { int fact=1; int i; for(i=1;i<=n;i++) { fact*=i; } return fact; } OUTPUT Enter any integer: 0 Factorial of 0 is 1 Enter any integer: 5 Factorial of 5 using Recursive method is 120 Factorial of 5 using Non-Recursive method is 120

Write a C program to find the GCD of two given integers using Non-Recursive function
AIM: To print the GCD of two given integers using Non-Recursive function ALGORITHM Step 1: Start Step 2: Declare a,b,n Step 3: Read any two integers a,b Step 4: n=gcd(a,b); Step 5: Write n Step 7: Stop ALGORITHM FOR FUNCTION Step 1: int gcd(int x,int y) Step 2: Declare t Step 3: while(y!=0) begin t=x; x=y; y=t%y; end Step 4: return x Step 4: Exit FLOW CHART

FLOW CHART FOR THE FUNCTION

PROGRAM /* GCD of two given integers using Non-Recursive function */ #include<stdio.h> #include<conio.h> void main() { int a,b,n; clrscr(); printf("\nEnter any two integers: "); scanf("%d%d",&a,&b); n=gcd(a,b); printf("\nGCD of given numbers is %d",n); getch(); } int gcd(int x,int y) { int t; while(y!=0) { t=x; x=y; y=t%y; } return(x); }

OUTPUT: Enter any two integers: 27 9 GCD of given numbers is 9

Write a C program to find the GCD of given two integers using Recursive function
AIM: To print the GCD of given two integers ALGORITHM Step 1: Start Step 2: Declare a,b,n Step 3: Read any two integers a,b Step 4: n gcd(a,b); Step 5: Write n Step 6: Stop ALGORITHM FOR THE FUNCTION Step 1: int gcd(int x,int y) begin if(y==0) return(x); else return(gcd(y,x%y)); end Step 2: Exit FLOW CHART

FLOW CHART FOR THE FUNCTION

PROGRAM /* GCD(Greatest Common Divisor) of given two integers using Recursive function */ #include<stdio.h> #include<conio.h> void main() { int a,b,n; clrscr(); printf("\nEnter any two integers: "); scanf("%d%d",&a,&b); n=gcd(a,b); printf("\nThe GCD of given numbers is %d",n); getch(); } int gcd(int x,int y) { if(y==0) return(x); else return(gcd(y,x%y));

} OUTPUT: Enter any two integers: 27 9 The GCD of given numbers is 9

Write a C program to find the Towers of Hanoi problem


AIM: To print the all the moves to solve the Towers of Hanoi problem

Towers of Hanoi Problem for 8 Disks

Towers of Hanoi Problem for 4 Disks Solution: Let Disk A Disk B Disk C Disk D Step 1: Move Disk A from tower 1 to tower 2

Step 2: Move Disk B from tower 1 to tower 3

Step 3: Move Disk A from tower 2 to tower 3

Step 4: Move Disk C from tower 1 to tower 2

Step 5: Move Disk A from tower 3 to tower 1

Step 6: Move Disk C from tower 3 to tower 2

Step 7: Move Disk A from tower 1 to tower 2

Step 8: Move Disk D from tower 1 to tower 3

Step 9: Move Disk A from tower 2 to tower 3

Step 10: Move Disk B from tower 2 to tower 1

Step 11: Move Disk A from tower 3 to tower 1

Step 12: Move Disk C from tower 2 to tower 3

Step 13: Move Disk A from tower 1 to tower 2

Step 14: Move Disk B from tower 1 to tower 3

Step 15: Move Disk A from tower 2 to tower 3

ALGORITHM Step 1: Start Step 2: Read no (no. of disks to be transferred) Step 3: if(no<1) Write Theres Nothing to move else hanoiRecursion(no,'A','B','C'); Step 4: Stop ALGORITHM FOR THE FUNCTION Step 1: int hanoiRecursion(int num,char ndl1,char ndl2,char ndl3) Step 2: if(num==1) begin Write Move top disk from needle ndl1 to needle ndl2." return; end hanoiRecursion(num-1,ndl1,ndl3,ndl2 ); Write "Move top disk from needle ndl1 to needle ndl2."); hanoiRecursion( num-1,ndl3,ndl2,ndl1 ); Step 3: Exit

FLOW CHART

FLOW CHART FOR THE FUNCTION

PROGRAM /* program to find the Towers of Hanoi problem */ #include<stdio.h> #include<conio.h> void main() { int no; clrscr(); printf("Enter the no. of disks to be transferred: "); scanf("%d",&no); if(no<1) printf("\nThere's nothing to move."); else printf("\nRecursive\n"); hanoiRecursion(no,'A','B','C'); getch(); } int hanoiRecursion(int num,char ndl1,char ndl2,char ndl3) { if(num==1) {

printf( "\nMove top disk from needle %c to needle %c.",ndl1,ndl2 ); return; } hanoiRecursion(num-1,ndl1,ndl3,ndl2 ); printf( "\nMove top disk from needle %c to needle %c.", ndl1, ndl2 ); hanoiRecursion( num-1,ndl3,ndl2,ndl1 ); } Output:
Enter the no. of disks to be transferred: 4 Move top disk from needle A to needle C Move top disk from needle A to needle B Move top disk from needle C to needle B Move top disk from needle A to needle C Move top disk from needle B to needle A Move top disk from needle B to needle C Move top disk from needle A to needle C Move top disk from needle A to needle B Move top disk from needle C to needle B Move top disk from needle C to needle A Move top disk from needle B to needle A Move top disk from needle C to needle B Move top disk from needle A to needle C Move top disk from needle A to needle B Move top disk from needle C to needle B

Write a C program to find the s=ut+ at2 for different values of u and a in a chosen time range
AIM: to print the value of s=ut+at2 ALGORITHM Step 1: Start Step 2: Declare u,t,a,ch,s variables Step 3: Read u and a values Step 4: Read the ch value (time Range chosen) Step 5: switch(ch) Step 6: if ch==1 then Read t (time value in range 0-40) break; if ch==2 then Read t (time value in range 41-80) break; if ch==3 then Read t (time value in range 81-120) break; if ch==4 then Read t (time value in range 121-160) break;

if ch==5 then Read t (time value in range 161-200) break; else Write Enter value from 1 to 5 only" exit(0); Step 7: s=u*t + (o.5*a*t*t) Step 8: Write s Step 9: Write Do you want to continue?(y or n) Step 10: Read ch1 (answer) Step 11: if ch1 is y or Y goto step 3 else goto step 12 Step 12: Stop

FLOW CHART

PROGRAM /* s=ut+at2 */ #include<stdio.h>

#include<conio.h> void main() { int u,t,a,ch; double s; char ch1; clrscr(); do { printf("\nEnter values of u and a:"); scanf("%d%d",&u,&a); printf("\nChoose the time range:\n"); printf("1. 0-40 \n2. 41-80 \n3.81-120 \n4.121-160 \n5.161-200 \n"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter time value in range 0-40: "); scanf("%d",&t); break; case 2: printf("\nEnter time value in range 41-80: "); scanf("%d",&t); break; case 3: printf("\nEnter time value in range 81-120: "); scanf("%d",&t); break; case 4: printf("\nEnter time value in range 121-160: "); scanf("%d",&t); break; case 5: printf("\nEnter time value in range 161-200: "); scanf("%d",&t); break; default: printf("\nEnter value from 1 to 5 only"); exit(0); } s=u*t+(0.5*a*t*t); printf("\nDistance in mts: %lf mts",s); printf("\nDo u want to change values of 'u' and 'a'? ('Y' or 'N'):"); fflush(stdin); scanf("%c",&ch1); }while(ch1=='y'||ch1=='Y'); getch(); } OUTPUT: Enter values of u and a: 4 2 Choose the time range: 1. 0-40 2. 41-80 3. 81-120 4. 121-160 5. 161-200 2 Enter time value in the range of 41-80: 50 Distance in mts: 2700.000000 mts Do u want to change values of u and a? (Y or N): N

Write a C program which takes two integer operands and one operator from the user, performs the operation and then prints the result
AIM: To print the switch case operators

ALGORITHM Step 1: Start Step 2: Declare a,b,res,op,ans variables Step 3: Read any two integer values (a,b) Step 4: Read any operator (op) Step 5: switch(op) Step 6: if op=+ then res=a+b and write res value Step 7: if op=- then res=a-b and write res value Step 8: if op=* then res=a*b and write res value Step 9: if op=/ then res=a/b and write res value Step 10: if op=% then res=a%b and write res value Step 11: Write Do u want to select another operator?('y' or 'n')? Step 12: Read ans if ans=y or ans=Y then goto step 4 otherwise goto step 13 Step 13: Stop FLOW CHART

PROGRAM /* C program which takes two integer operands and one operator from the user, performs the operation and then prints the result */ #include<stdio.h> #include<conio.h> void main()

{ int a,b; float res; char op,ans; clrscr(); printf("\nEnter any two numbers: "); scanf("%d%d",&a,&b); do { printf("\nEnter any operator(+,-,*,/,%): "); fflush(stdin); scanf("%c",&op); switch(op) { case '+': res=a+b; break; case '-': res=a-b; break; case '*': res=a*b; break; case '/': res=a/b; break; case '%': res=a%b; break; default: printf("\nWrong selection of operator"); exit(0); } printf("%f",res); printf("\nDo u want to select another operator?('y' or 'n'): "); fflush(stdin); scanf("%c",&ans); }while(ans=='Y'||ans=='y'); getch(); } OUTPUT: Enter any two numbers: 25 7 Enter any operator(+,-,*,/,%): * 175.000000 Do u want to select another operator?(y or n): n

Write a C program to find the largest and smallest number in given numbers
AIM: To print the largest and smallest number in given numbers ALGORITHM Step 1: Start

Step Step Step Step Step

2: 3: 4: 5:

Step Step

Step Step

Declare n,a[10],i,max,min variables Assign max=0 Read the n (no. of integers to be entered) values for(i=0;i<n;i++) a[i] Read n integers 6: for(i=0;i<n;i++) begin if(a[i]>max) maxa[i]; end 7: min max 8: for(i=0;i<n;i++) begin if(a[i]<min) min=a[i]; end 9: Write max and min 10: Stop FLOW CHART

PROGRAM /* C program to find the largest and smallest number in given numbers */ #include<stdio.h> #include<conio.h> void main() {

int n,a[10],i,max=0,min; clrscr(); printf("\nHow many integers u want to enter?: "); scanf("%d",&n); printf("\nEnter %d integers: ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { if(a[i]>max) max=a[i]; } min=max; for(i=0;i<n;i++) { if(a[i]<min) min=a[i]; } printf("\n Largest number is %d",max); printf("\n Smallest number is %d",min); getch(); } OUTPUT: How many integers u want to enter?: 5 Enter 5 integers: 12 3 5 25 10 Largest number is 25 Smallest number is 3

Write a C program to find the addition of two matrices


AIM: To calculate the addition of two matrices and to print the result ALGORITHM Step 1: Start Step 2: Declare a[10][10],b[10][10],i,m,j,n variables Step 3: Read m and n values (no. of rows and columns) Step 4: for(i=0;i<m;i++) begin for(j=0;j<n;j++)

Read a[i][j] values (elements for the first matrix) end Step 5: for(i=0;i<m;i++) begin for(j=0;j<n;j++) Read b[i][j] values (elements for the second matrix) end Step 6: for(i=0;i<m;i++) begin for(j=0;j<n;j++) Write a[i][j]+b[i][j] values end Step 7: Stop FLOW CHART

PROGRAM /* Addition of two matrices */ #include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],i,m,j,n; clrscr(); printf("\nEnter No. of rows and columns: ");

scanf("%d%d",&m,&n); printf("\nEnter %d elements for the first %dX%d matrix: ",m*n,m,n); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } printf("\nEnter %d elements for the second %dX%d matrix: ",m*n,m,n); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&b[i][j]); } printf("\n First Matrix is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",a[i][j]); printf("\n\n"); } printf("\n Second Matrix is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",b[i][j]); printf("\n\n"); } printf("\n Sum of first and second matrices is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",a[i][j]+b[i][j]); printf("\n\n"); } getch(); } OUTPUT: Enter No. of rows and columns: 2 2 Enter 4 elements for the first 2X2 matrix: 1 2 3 4 Enter 4 elements for the second 2X2 matrix: 1 2 3 4 First Matrix is: 12 34 Second Matrix is: 12 34 Sum of first and second matrices is: 24 68

Write a C program to find the multiplication of two matrices


AIM: To calculate the multiplication of two matrices and to print the result ALGORITHM Step 1: Start Step 2: Declare a[10][10],b[10][10],c[10][10],i,m1,m2,j,n1,n2,k variables Step 3: Read m1 and n1values (no. of rows and columns for the first matrix) Step 4: for(i=0;i<m1;i++) begin for(j=0;j<n1;j++) Read a[i][j] values (elements for the first matrix) end Step 5: Read m2 and n2 values (no. of rows and columns for the second matrix) Step 6: if m1 equals n2 then

for(i=0;i<m2;i++) begin for(j=0;j<n2;j++) Read b[i][j] values (elements for the second matrix) end Step 7: for(i=0;i<m1;i++) begin for(j=0;j<n2;j++) begin c[i][j]=0; for(k=0;k<n1;k++) begin c[i][j]+=a[i][k]*b[k][j]; end end end Step 8: if m1 not equals n2 then write no. of rows in the first matrix must be same as the no. of columns in the second matrix Step 9: Stop

FLOW CHART

PROGRAM /* Multiplication of two matrices */ #include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10],i,m1,m2,j,n1,n2,k; clrscr(); printf("\nEnter No. of rows and columns for the first matrix: "); scanf("%d%d",&m1,&n1); printf("\nEnter %d elements for the first %dX%d matrix: ",m1*n1,m1,n1); for(i=0;i<m1;i++) { for(j=0;j<n1;j++) scanf("%d",&a[i][j]); } printf("\nEnter No. of rows and columns for the second matrix: "); scanf("%d%d",&m2,&n2); if(m1==n2) { printf("\nEnter %d elements for the second %dX%d matrix: ",m2*n2,m2,n2); for(i=0;i<m2;i++) { for(j=0;j<n2;j++) scanf("%d",&b[i][j]); } printf("\n First Matrix is:\n"); for(i=0;i<m1;i++) { for(j=0;j<n1;j++) printf("%d ",a[i][j]); printf("\n\n"); } printf("\n Second Matrix is:\n"); for(i=0;i<m2;i++) { for(j=0;j<n2;j++) printf("%d ",b[i][j]); printf("\n\n"); } for(i=0;i<m1;i++) { for(j=0;j<n2;j++) { c[i][j]=0; for(k=0;k<n1;k++) { c[i][j]+=a[i][k]*b[k][j]; }

} } printf("\n Product of first and second matrices is:\n"); for(i=0;i<m1;i++) { for(j=0;j<n2;j++) printf("%d ",c[i][j]); printf("\n\n"); } } else { textcolor(RED+BLINK); cprintf("\n No. of rows in the first matrix must be same as the no. of columns in the second matrix"); } getch(); } OUTPUT: Enter No. of rows and columns: 2 2 Enter 4 elements for the first 2X2 matrix: 1 2 3 4 Enter 4 elements for the second 2X2 matrix: 1 2 3 4 First Matrix is: 12 34 Second Matrix is: 12 34 Sum of first and second matrices is: 24 68 Product of first and second matrices is: 7 10 15 22

PROGRAM /* To insert a sub-string in to given main string from a given position */ #include<stdio.h> #include<conio.h>

#include<string.h> void main() { char mstr[80],substr[80],str[100]; int k,j=0,i; clrscr(); printf("\nEnter first string: "); gets(mstr); printf("\nEnter second string: "); gets(substr); printf("\nEnter insertion position: "); scanf("%d",&k); for(i=0;i<k-1;i++) str[j++]=mstr[i]; for(i=0;substr[i]!='\0';i++) str[j++]=substr[i]; for(i=k-1;mstr[i]!='\0';i++) str[j++]=mstr[i]; str[j]='\0'; printf("\nString after insertion: %s",str); getch(); }

PROGRAM /* To delete n characters from a given position in a given string */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char mstr[80],str[80]; int i,j=0,k,n; clrscr(); printf("\nEnter a string: "); gets(mstr); printf("\nEnter starting position number of the deletion: "); scanf("%d",&k); printf("\nEnter number of characters to be deleted: "); scanf("%d",&n); for(i=0;i<k-1;i++) str[j++]=mstr[i]; for(i=k+n-1;mstr[i]!='\0';i++) str[j++]=mstr[i]; str[j]='\0';

printf("\nString after deletion: %s",str); getch(); } PROGRAM /* To determine if the given string is a palindrome or not */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s1[20],s2[20]; int i,j,len; clrscr(); printf("\nEnter a string: "); gets(s1); len=strlen(s1); for(i=len-1,j=0;i>=0;i--,j++) s2[j]=s1[i]; s2[j]='\0'; if(strcmp(s1,s2)==0) { textcolor(GREEN+BLINK); cprintf("Entered string is palindrome"); } else { textcolor(RED+BLINK); cprintf("Entered string is not a palindrome"); } getch(); }

PROGRAM /* Searching for a given string and displaying position */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[20],t[20]; int len1,len2,p; clrscr(); printf("\nEnter the main string: "); gets(s); len1=strlen(s); printf("\nEnter the search string: ");

gets(t); len2=strlen(t); p=findpos(len1,len2,s,t); if(p!=-1) printf("\nThe string %s is found at position %d",t,p); else printf("\nThe string %s is not found in the main string %s",t,s); getch(); } int findpos(int l1,int l2, char s1[],char t1[]) { int i,j=0,flag=0; for(i=0;i<l1&&j<l2;i++) { if(s1[i]!=t1[j]) flag=1; else { flag=0; j++; } } if(flag==0) return(i-l2+1); else return(-1); } PROGRAM /* To count the number of lines, words, and characters in a given text */ #include<stdio.h> #include<conio.h> void main() { char text[100],ch; int i,c,sp,nchar=0,wds=0,lines=0; clrscr(); printf("\nEnter the text: "); while(1) { c=0; while((ch=getchar())!='\n') { text[c]=ch; c++; } text[c]='\0'; if(text[0]=='\0') break; else

{ wds++; sp=0; for(i=0;text[i]!='\0';i++) if(text[i]==' '||text[i]=='\t') { sp++; wds++; } } lines++; nchar=nchar+strlen(text)-sp; } printf("\nNo. of lines=%d",lines); printf("\nNo. of words=%d",wds); printf("\nNo. of characters=%d",nchar); getch(); } PROGRAM /* To generate pascal's triangle */ #include<stdio.h> #include<conio.h> void main() { int a,i,j,k,n; clrscr(); printf("\nEnter no. of rows: "); scanf("%d",&n); for(i=0;i<n;i++) { for(k=1;k<n-i;k++) printf(" "); for(j=0;j<=i;j++) { if((j==0)||(i==0)) a=1; else a=((a*(i-j+1))/j); printf("%4d",a); } printf("\n"); } getch(); }

PROGRAM /* program to generate pyramid of numbers */ #include<stdio.h> #include<conio.h> void main() { int i,j,k,n; clrscr(); printf("\nEnter no. of rows: "); scanf("%d",&n); for(i=1;i<=n;i++) { for(k=1;k<=n-i;k++) printf(" "); for(j=1;j<=i;j++) printf("%4d",i); printf("\n"); } getch(); }

PROGRAM /* program for geometric progression */ #include<stdio.h> #include<conio.h> void main() { int x,n,y,i,sum; clrscr(); label1: printf("\nEnter the values of x and n: "); scanf("%d%d",&x,&n); if(x<0||n<0) { printf("\n x and n cannot be negative,enter again"); goto label1; } else { sum=1; y=1; for(i=0;i<n;i++) {

y=y*x; sum=sum+y; } } printf("x=%d n=%d sum=%d",x,n,sum); getch(); }

Você também pode gostar