46
CONTENTS

Btech programs

Embed Size (px)

DESCRIPTION

c for all

Citation preview

Page 1: Btech programs

CONTENTS

Page 2: Btech programs

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 2a. To calculate the Sum = 1- X 2 /2! + X 4 /4! – X 6 /6! + X 8 /8! – X 10 /10! b. To find the roots of the Quadratic equation

Week 3a. i) To find the factorial of a given integer using Recursive and Non-

Recursive 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 4a. 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 5a. 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 7a. To display the position or index in the string S where the string T

beginsb. To count the lines, words and characters in a given text

Week 8a. To generate Pascal’s triangleb. 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

Page 3: Btech programs

Week 10a. To find the 2’s complement of a binary numberb. To convert a Roman numeral to its decimal equivalent

Week 11• Using functions

i) Reading a complex numberii) Writing a complex numberiii) Addition of two complex numbersiv) Multiplication of two complex numbers

Week 12a. To copy one file to anotherb. 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 expressionii) 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 orderWeek 19 i) Linear search

• Using Non-Recursive function• Using Recursive function

ii) Binary search• Using Non-Recursive function• Using Recursive function

Page 4: Btech programs

Week 20i) Bubble sortii) Quick sort

Week 21i) Insertion sort

ii) 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

Page 5: Btech programs

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 1: StartStep 2: Accept i, num, sum=0, rem, n, s=0, numberStep 3: Read the numberStep 4: Store the number in numStep 5: while(num>0) begin rem=num%10; sum=sum+rem; num=num/10; endStep 6: Assign nsumStep 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

Page 6: Btech programs

FLOWCHART

Page 7: Btech programs

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: 145sum of individual digits of 145 is 10 which is again 1

Enter any positive integer: 23Sum 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

Page 8: Btech programs

ALGORITHM:

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

FLOWCHART

PROGRAM

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

Page 9: Btech programs

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: 10The 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: StartStep 2: Declare i, j, n, countStep 3: Read the number(n)Step 4: for(i=1;i<=n;i++) begin count=0;

Page 10: Btech programs

for(j=1;j<=i;j++) begin if(i%j==0) count++; end end if(count<=2) Print iStep 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);

Page 11: Btech programs

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:20The 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!ALGORITHMStep 1: StartStep 2: Declare i,j,x,fact=1.00,s,total=0.00,val;Step 3: Read the x valueStep 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)

Page 12: Btech programs

total=total+s endStep 5: val=1+totalStep 6: Print totalStep 7: Stop

FLOWCHART

Write a C program to find the roots of a quadratic equationAIM: To print the roots of a quadratic equationALGORITHMStep 1: StartStep 2: Declare a,b,c,r1,r2,d,x,yStep 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 endStep 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

Page 13: Btech programs

endStep 7: else if(d<0) write roots are imaginaryStep 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) {

Page 14: Btech programs

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 1r1=-0.500000 , r2=-0.500000roots are real and equal

Enter the values of a,b,c: 5 4 2roots are imaginary

Enter the values of a,b,c: 4 5 1r1=-0.250000 , r2=-1.000000roots 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 methodALGORITHMStep 1: StartStep 2: Declare fact=1Step 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) endStep 5: StopALGORITHM FOR RECURSIVE FUNCTIONStep 1: int rec_fact(int n)Step 2: return (n>=1) ? n* rec_fact(n-1) : 1Step 3: ExitALGORITHM FOR NON-RECURSIVE FUNCTIONStep 1: int nonrec_fact(int n)Step 2: for(i=1;i<=n;i++) fact*=i;

Page 15: Btech programs

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){

Page 16: Btech programs

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;}

OUTPUTEnter any integer: 0Factorial of 0 is 1

Enter any integer: 5Factorial of 5 using Recursive method is 120Factorial 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 functionALGORITHMStep 1: StartStep 2: Declare a,b,nStep 3: Read any two integers a,bStep 4: n=gcd(a,b);Step 5: Write n Step 7: StopALGORITHM FOR FUNCTIONStep 1: int gcd(int x,int y)Step 2: Declare tStep 3: while(y!=0) begin t=x; x=y; y=t%y; endStep 4: return xStep 4: Exit

FLOW CHART

Page 17: Btech programs

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);}

Page 18: Btech programs

OUTPUT:Enter any two integers: 27 9GCD 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 integersALGORITHMStep 1: StartStep 2: Declare a,b,nStep 3: Read any two integers a,bStep 4: n gcd(a,b);Step 5: Write n Step 6: StopALGORITHM FOR THE FUNCTIONStep 1: int gcd(int x,int y) begin if(y==0) return(x); else return(gcd(y,x%y)); endStep 2: Exit

FLOW CHART

Page 19: Btech programs

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));

Page 20: Btech programs

}OUTPUT:Enter any two integers: 27 9The 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

Page 21: Btech programs

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

Page 22: Btech programs

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

Page 23: Btech programs

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

Page 24: Btech programs

ALGORITHMStep 1: StartStep 2: Read no (no. of disks to be transferred)Step 3: if(no<1) Write “There’s Nothing to move” else hanoiRecursion(no,'A','B','C');Step 4: StopALGORITHM FOR THE FUNCTIONStep 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

Page 25: Btech programs

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) {

Page 26: Btech programs

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: 4Move top disk from needle A to needle CMove top disk from needle A to needle BMove top disk from needle C to needle BMove top disk from needle A to needle CMove top disk from needle B to needle AMove top disk from needle B to needle CMove top disk from needle A to needle CMove top disk from needle A to needle B Move top disk from needle C to needle BMove top disk from needle C to needle AMove top disk from needle B to needle AMove top disk from needle C to needle BMove top disk from needle A to needle CMove top disk from needle A to needle BMove 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

ALGORITHMStep 1: StartStep 2: Declare u,t,a,ch,s variablesStep 3: Read u and a valuesStep 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;

Page 27: Btech programs

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 sStep 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 12Step 12: Stop

FLOW CHART

Page 28: Btech programs

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

Page 29: Btech programs

#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 2Choose the time range:

1. 0-402. 41-803. 81-1204. 121-1605. 161-2002

Enter time value in the range of 41-80: 50Distance in mts: 2700.000000 mtsDo 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

Page 30: Btech programs

ALGORITHMStep 1: StartStep 2: Declare a,b,res,op,ans variablesStep 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’ valueStep 7: if op=’-’ then res=a-b and write ‘res’ valueStep 8: if op=’* ’ then res=a*b and write ‘res’ valueStep 9: if op=’/’ then res=a/b and write ‘res’ valueStep 10: if op=’%’ then res=a%b and write ‘res’ valueStep 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 13Step 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()

Page 31: Btech programs

{ 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 7Enter any operator(+,-,*,/,%): *175.000000Do 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

ALGORITHMStep 1: Start

Page 32: Btech programs

Step 2: Declare n,a[10],i,max,min variablesStep 3: Assign max=0Step 4: Read the n (no. of integers to be entered) valuesStep 5: for(i=0;i<n;i++) a[i] Read n integersStep 6: for(i=0;i<n;i++) begin if(a[i]>max) maxa[i]; endStep 7: min maxStep 8: for(i=0;i<n;i++) begin if(a[i]<min) min=a[i]; endStep 9: Write max and minStep 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(){

Page 33: Btech programs

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?: 5Enter 5 integers: 12 3 5 25 10Largest number is 25Smallest number is 3

Write a C program to find the addition of two matricesAIM: To calculate the addition of two matrices and to print the resultALGORITHMStep 1: StartStep 2: Declare a[10][10],b[10][10],i,m,j,n variablesStep 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++)

Page 34: Btech programs

Read a[i][j] values (elements for the first matrix) endStep 5: for(i=0;i<m;i++) begin for(j=0;j<n;j++) Read b[i][j] values (elements for the second matrix) endStep 6: for(i=0;i<m;i++) begin for(j=0;j<n;j++) Write a[i][j]+b[i][j] values endStep 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: ");

Page 35: Btech programs

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 2Enter 4 elements for the first 2X2 matrix: 1 2 3 4Enter 4 elements for the second 2X2 matrix: 1 2 3 4First Matrix is:1 23 4Second Matrix is:1 2 3 4Sum of first and second matrices is:2 46 8

Page 36: Btech programs

Write a C program to find the multiplication of two matricesAIM: To calculate the multiplication of two matrices and to print the result

ALGORITHMStep 1: StartStep 2: Declare a[10][10],b[10][10],c[10][10],i,m1,m2,j,n1,n2,k variablesStep 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) endStep 5: Read m2 and n2 values (no. of rows and columns for the second matrix)Step 6: if m1 equals n2 then

Page 37: Btech programs

for(i=0;i<m2;i++) begin for(j=0;j<n2;j++) Read b[i][j] values (elements for the second matrix) endStep 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 endStep 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 matrixStep 9: Stop

FLOW CHART

Page 38: Btech programs
Page 39: Btech programs

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]; }

Page 40: Btech programs

} } 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 2Enter 4 elements for the first 2X2 matrix: 1 2 3 4Enter 4 elements for the second 2X2 matrix: 1 2 3 4First Matrix is:1 23 4Second Matrix is:1 2 3 4Sum of first and second matrices is:2 46 8Product of first and second matrices is:7 1015 22

PROGRAM

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

Page 41: Btech programs

#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';

Page 42: Btech programs

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: ");

Page 43: Btech programs

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

Page 44: Btech programs

{ 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();}

Page 45: Btech programs

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++) {

Page 46: Btech programs

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