Вы находитесь на странице: 1из 49

Department of Computer Science and Engineering

LAB MANUAL IN COMPUTER PROGRAMMING LAB

KAUSHIK COLLEGE OF ENGINEERING , (Affiliated to JNTU,Kakinada;Approved by AICTE New Delhi) GAMBHEERAM, VISAKHAPATNAM VISAKHAPATNAM DISTRICT. PIN:531163.

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

CONTENTS

Kaushik college 2 of ngineering vizag

S.NO

Name Of The Experiment/Program


DEARTMENT OF CSE

Page no:
COMPUTER

WEEK-1: a)To implement a C program PROGRAMMING LAB to find the sum of individual digits of a positive integer

6 7

b) To implement a C program to generate the first n terms of the fibonacci sequence 1 c) To implement a C program to generate all the prime numbers between 1 and n , where n is a value supplied by the user WEEK-2: a)To implement a C program to calculate thefollowing sum : sum=1-x2/2!+x4/4!-x6/6!+x8/8!-x10/10! 2 b) To implement a C program to find the roots of a quadratic equation WEEK-3: To implement a C program that use both recursive and non-recursive functions i)to find the factorial of a given integer 3 ii)to find the GCD(Greatest Common Divisor) of two given integers. iii)to solve towers of Hanoi problem WEEK-4: a)To implement a C program to find the distance travelled at regular intervals of time given the values of u and a. 4 b) To implement a C program which takes two integer operands and one operator from the user,perform the operation and then prints the result WEEK-5:a) To implement a C program to find both the largest and smallest number in a list of integers b) To implement a C program that uses functions to perform the following: 5 i)addition of two matrices ii)multiplication of two matrices WEEK-6:a) To implement a C program that uses vizag functions to perform the following operations.
Kaushik college 3 of ngineering

10

11

13

15 17 19

21

22 24

26 28

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

WEEK-1: a) STATEMENT: To implement sum of individual digits of a given number ALGORITHM: Step-1: declare n,sum,d Step-2: read n Step-3: d=n%10 Step-4: sum=sum+d Step-5: n=n/10 Step-6: repeat steps-3,4,5 until n>0 Step-7: print sum PROGRAM: #include<stdio.h> #include<conio.h> main() { int n,sum=0,d; clrscr(); printf("enter the number\n"); scanf("%d",&n); while(n>0) { d=n%10; sum=sum+d;
Kaushik college 4 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

n=n/10; } printf("sum=%d\n",sum); getch(); } DESCRIPTION: This program deals with calculating the sum of digits of a given number by simply extracting the last digit with n%10 for each and every while loop and making the sum to each and every extracted digit until n>0

OUTPUT: enter the number sum=14 WEEK-1: b)

4325

STATEMENT: To implement the Fibannoci series upto n ALGORITHM: Step-1: Step-2: Step-3: Step-4: Step-5: Step-6: Step-7: Step-8: Step-9: declare f1,f2,f3,n,i read n print f1,f2 f3=f1+f2 print f3 f1=f2 f2=f3 i++ repeat steps-4,5,6,7,8 until i<n

PROGRAM: #include<stdio.h> #include<conio.h> main() { int f1=0,f2=1,f3=0,n; clrscr();


Kaushik college 5 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

printf("enter the n value\n"); scanf("%d",&n); printf("%d\t%d",f1,f2); while(f3<n) { f3=f1+f2; printf("\t%d",f3); f1=f2; f2=f3; } getch(); } DESCRIPTION: This program deals with finding the Fibonacci terms upto the given value of n .Fibonacci series starts with first term as 0,second term as 1.The third term can be calculated by making the sum of first term and second term OUTPUT: enter the n value 10 0 1 1 2 3 5 8 13

WEEK-1:c) STATEMENT: numbers 1 to n ALGORITHM: STEP-1: STEP-2: STEP-3: STEP-4: STEP-5: declare i,j,n,s read n if i%j==0 s=1 if s==0 print I
Kaushik college 6 of ngineering vizag

To implement a program to find out the

prime

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

STEP-6: repeat 3,4,5 steps until i<=n,j<i PROGRAM: #include<stdio.h> #include<conio.h> main() { int i,j,n,s=0; clrscr(); printf("enter the n value\n"); scanf("%d",&n); for(i=1;i<=n;i++) { if(i==1) printf("%d\t",i); else { for(j=2;j<i;j++) { s=0; if(i%j==0) { s=1; break; } } if(s==0) printf("%d\t",i); } } getch(); } DESCRIPTION: This program finds out the prime numbers between 1 to n.Inorder to find out whether a number is a prime or not each and every number is divided by 2,3,.n and we are taking the Boolean variable which

Kaushik college 7 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

was having the initial value as 0.After verifying with ith and jth loops if the Boolean variable value is 0, then it is a prime. OUTPUT: enter n value 15 1 2 3 5 7 11 13

WEEK-2:a) STATEMENT : To implement a program to find sum of 1-x2/2!+x4/4!x6/6!+x8/8!-x10/10! ALGORITHM: STEP-1: STEP-2: STEP-3: STEP-4: STEP-5: STEP-6: STEP-7: STEP-8: STEP-9: declare I,k,sign,x,f,sum,t read x f=f*k repeat step-3 until k!=0 t=pow(x,i)/f sum=sum+sign*t sign*=-1 repeat step-3,4,5,6,7 until i<10 print sum

PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> main() { int i,k,sign=-1,x; float f,sum=1,t=0;


Kaushik college 8 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

clrscr(); printf("enter the x value\n"); scanf("%d",&x); for(i=2;i<=10;i=i+2) { f=1; k=i; while(k!=0) f*=k--; t=(float)pow(x,i)/f; sum=sum+sign*t; sign*=-1; } printf("sum=%f",sum); getch(); } DESCRIPTION: This program generates the series with even powers on numerators and even factorials on denominators with alternative + and OUTPUT: enter x value 2 sum=-0.416155 enter x value 4 sum=-0.685785

WEEK-2:b) STATEMENT: To implement a program to find out the Roots of a Quadratic equation ALGORITHM: STEP-1: declare a,b,c,d,r1,r2,rp,ip
Kaushik college 9 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

STEP-2: STEP-3: STEP-4: STEP-5: STEP-6: STEP-7:

read a,b,c d=b*b-4*a*c if(d==0) print r1=-b/2a,r2=-b/2a else if (d>0) print r1=-b+sqrt(d)/2a,r2=-b-sqrt(d)/2a else print r1=rp+ip,r2=rp-ip

PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> main() { int a,b,c,d; float r1,r2,rp,ip; clrscr(); printf("enter a,b,c values\n"); scanf("%d%d%d",&a,&b,&c); d=b*b-4*a*c; if(d==0) { printf("the roots are real and equal\n"); r1=(float)-b/2*a; r2=(float)-b/2*a; printf("r1=%f\n r2=%f",r1,r2); } else if(d>0) { printf("the roots are real and distinct\n"); r1=(float)(-b+sqrt(d))/2*a; r2=(float)(-b-sqrt(d))/2*a; printf("r1=%f\n r2=%f",r1,r2); } else { printf("the complex roots are\n"); rp=(float)-b/2*a;
Kaushik college 10 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

ip=(float)sqrt(-d)/2*a; printf("r1=%f+i%f\n",rp,ip); printf("r1=%f-i%f",rp,ip); } getch(); } DESCRIPTION: In this program first we are calculating discrimininant value from the values of a,b,c in quadratic equation.if the discriminanat value is zero, then the roots are real and equal where r1=r2=-b/2a,if the discriminant is greater than zero, then the roots are real and distinct where r1=(float)(-b+sqrt(d))/2*a, r2=(float)(-b-sqrt(d))/2*a;if the discriminant is less than zero,then the roots are real and complex where real part= -b/2*a and imaginary part= sqrt(-d)/2*a, such that r1=rp+ip,r2=rp-ip OUTPUT: enter a,b,c values 123 the complex roots are r1=-1.000000+i1.414214 r2=-1.000000-i1.414214 enter a,b,c values 121 the roots are real and equal r1=-1.000000 r2=-1.000000 enter a,b,c values 120 r1= 0.000000 r2=-2.000000

WEEK-3:a) (i)

Kaushik college 11 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

STATEMENT: To find factorial of a given no using Non-recursive function AND recursive. ALGORITHM: STEP-1: declare n STEP-2: creating the user defined function factorial STEP-3: read n STEP-4: called function defines F*=m STEP-5: repeat step-4 until m>0 STEP-6: print factorial PROGRAM: #include<stdio.h> #include<conio.h> main() { int n; void factorial(int); clrscr(); printf("enter n"); scanf("%d",&n); factorial(n); getch(); } void factorial(int m) { float f=1; while(m>0) f*=m--; printf("Factorial =%f",f); } DESCRIPTION: This program desribes how to create the user-defined function and this function is said to be calling function which will be inside the main.called function which is defined outside the main defines code f*=minside the while loop until m>0
Kaushik college 12 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

OUTPUT: Enter n 4 Factorial= 24.000000

/*To find factorial of a given no using recursive function*/ #include<stdio.h> #include<conio.h> main() { int n; float k; float factorial(int); clrscr(); printf("enter n"); scanf("%d",&n); k=factorial(n); printf("factorial=%f",k); getch(); } float factorial(int m) { if(m==1) return 1; else return m*factorial(m-1); }

output:
Enter n 4 Factorial= 24.000000
Kaushik college 13 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

WEEK-3:a) (ii) STATEMENT: To find GCD of two numbers using non recursive functions ALGORITHM: STEP-1: declare a,b STEP-2: Creating the user-defined function GCD STEP-3: read a,b STEP-4: called function defines D=m%n,m=n,n=d STEP-5: print m PROGRAM: #include<stdio.h> #include<conio.h> main() { int a,b; void GCD(int,int); clrscr(); printf("enter a,b values"); scanf("%d%d",&a,&b); GCD(a,b); getch(); } void GCD(int m,int n) { int d; while(d>0) { d=m%n; m=n; n=d;
Kaushik college 14 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

} printf("Gcd of the given numbers=%d",m); } DESCRIPTION: This program deals with finding out the Greatest Common Divisor of two numbers by considering d=m%n,m=n,n=d and finally m will be the GCD

output:
Enter a,b values 18 9 Gcd of the given numbers=9 /*To find GCD of two numbers using recursive functions*/ #include<stdio.h> #include<conio.h> main() { int a,b,c; int GCD(int,int); clrscr(); printf("enter a,b values"); scanf("%d%d",&a,&b); c=GCD(a,b); printf("%d",c); getch(); } int GCD(int m,int n) { if(n==0) return m; else return GCD(n,m%n); }

output:
Kaushik college 15 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

Enter a,b values 18 9 Gcd of the given numbers=9

WEEK-3:a) (iii) STATEMENT: To implement functions ALGORITHM: STEP-1: declare n STEP-2: read n STEP-3: creating the user-defined function towers STEP-4: called function defines STEP-5: towers(n-1,fp,ap,tp) STEP-6: move disk from fp to tp STEP-7: towers(n-1,ap,tp,fp) PROGRAM: #include<stdio.h> #include<conio.h> main() { int n; clrscr(); printf("enter no of disks"); scanf("%d",&n); towers(n,'A','B','C'); getch(); } towers(int n,char fp,char tp,char ap) { if(n==1) { printf("\n%s%c%s%c","Move disk 1 from pole ",fp," to pole ",tp);
Kaushik college 16 of ngineering vizag

Towers of Hanoi using recursive

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

return; } towers(n-1,fp,ap,tp); printf("\n%s%d%s%c%s%c","Move disk ",n," from pole ",fp," to pole ",tp); towers(n-1,ap,tp,fp); } DESCRIPTION: In this program initially source disk contains highest numbered plate will be placed in bottom and the next lowered numbered plate will be placed on the top and so on .the same ordered disks should be placed in target disk.in making this arrangement we have to consider the temporary disk OUTPUT: Enter no of poles 4 Move disk 1 from pole A to pole C Move disk 2 from pole A to pole B Move disk 1 from pole C to pole B Move disk 3 from pole A to pole C Move disk 1 from pole B to pole A Move disk 2 from pole B to pole C Move disk 1 from pole A to pole C Move disk 4 from pole A to pole B Move disk 1 from pole C to pole B Move disk 2 from pole C to pole A Move disk 1 from pole B to pole A Move disk 3 from pole C to pole B Move disk 1 from pole A to pole C Move disk 2 from pole A to pole B Move disk 1 from pole C to pole B WEEK-4: a) STATEMENT: The total distance traveled by vehicle in t seconds is given by distance=ut+1/2a*t*t where u and a are the initial velocity (m/sec) and acceleration(m/sec2).Write a c program to find out the
Kaushik college 17 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

distance traveled at regular intervals of time given the values u and a. The program should provide the flexibility to the user to select his own intervals and repeat the calculations for different values of u and a. ALGORITHM: STEP-1: declare a,u,d,t,n STEP-2: read a,u,t,n STEP-3: d=(float)(u*t+0.5*a*t*t) STEP-4: repeat step-3 until t<n STEP-5: print t,d

PROGRAM: #include<stdio.h> #include<conio.h> main() { float a,u,d; int t,n; clrscr(); printf("enter velocity, acceleration and timeinterval\n"); scanf("%f\n%f\n%d",&a,&u,&t); printf("\nenter no of intervals"); scanf("%d",&n); for(t=0;t<=n;t++) { d=(float)(u*t+0.5*a*t*t); printf("for the %d interval \t displacement =%f\n",t,d); } getch(); } DESCRIPTION: The theme of the program is to calculate the distance covered for the given values of u,a and t.inorder to find out this, we have
Kaushik college 18 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

S=u*t+0.5*a*t*t OUTPUT: enter velocity,acceleration and time interval 2 3 2 enter no of intervals 5 for the 1 interval displacement =10.000000 for the 2 interval displacement =28.000000 for the 3 interval displacement =54.000000 for the 4 interval displacement =88.000000 for the 5 interval displacement =130.000000

WEEK-4:b) STATEMENT: To implement a c Program ,which takes two integer operands and one operator from the user,performs the operation and then prints the result(Considering the operators +,-,*,/,% and use switch statement) ALGORITHM: STEP-1: declare a,b,c,d,op STEP-2: read a,b,op STEP-3: case + c=a+b STEP-4: case - c=a-b STEP-5: case * c=a*b STEP-6: case / c=a/b STEP-7: case % c=a%b STEP-8: print c

PROGRAM: #include<stdio.h> #include<conio.h> main()


Kaushik college 19 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

{ int a,b,c; float d; char op; clrscr(); printf("enter the two operands\n"); scanf("%d%d",&a,&b); printf("enter the operator"); scanf("%s",&op); switch(op) { case '+': c=a+b; printf("Addition =%d",c); break; case '-': c=a-b; printf("Subtraction=%d",c); break; case '*': c=a*b; printf("Multiplication=%d",c); break; case '/' : d=(float)(a/b); printf("division =%f",d); break; case '%': c=a%b; printf("Modulo=%d",c); break; default : printf("\ninvlaid selection of operator"); } getch(); } DESCRIPTION: The theme of the program is to find out the various operation results by considering the various cases including in the switch statement and the corresponding case will be executed according to the choice given b y the user. OUTPUT: Enter the two operands 3 4 Enter the operator +
Kaushik college 20 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

Addition=7 Enter the two operands 4 0 Enter the operator Subtraction=4

WEEK-5:a) STATEMENT: To implement a Program to find both the largest and smalest number in a list of integers.

ALGOITHM: STEP-1: Declare a[10],I,j,temp,n STEP-2: read n,a[i] STEP-3: if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } STEP-4: repeat step-3 until i<n and j<n STEP-5: print a[0],a[n-1] PROGRAM: #include<stdio.h> #include<conio.h> main() { int a[10],i,j,temp,n; clrscr(); printf("enter no of elements \n"); scanf("%d",&n); printf("\nenter the array elements in any order");
Kaushik college 21 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("the smallest element=%d\n",a[0]); printf("the largest element =%d",a[n-1]); getch(); } DESCRIPTION: In this program we are enetering the array elements and comparing each and every two elements. If first element is greater than second element then we have to go for swapping.finally all the elements are in ascending order.so the first element will be the lowest and the largest element will be the last. OUTPUT: Enter no of elements 5 Enter array element in any order 4 2 1 23 0 the smallest element =0 the largest element =23 WEEK-5:b) (i) STATEMENT: using functions. To implement a C Program that add to 2 matrices
Kaushik college 22 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

ALGORITHM: STEP-1: STEP-2: STEP-3: STEP-4: STEP-5: declare a[10][10],b[10][10],C[10][10],p,q,I,j read p,q,a[i][j],b[i][j] c[i][j]=a[i][j]+b[i][j] repeat step-3 until i<p,j<q print c[i][j]

PROGRAM: #include<stdio.h> #include<conio.h> int i,j,p,q; main() { int a[10][10],b[10][10],p,q,I,j; void matrixadd(int a[][],int b[][],int,int); clrscr(); printf("enter the order of matrix\n"); scanf("%d%d",&p,&q); printf("enter A array elements\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&a[i][j]); printf("enter B array elements\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]); matrixadd(a,b,p,q); getch(); } void matrixadd(int a[10][10],int b[10][10],int p,int q) { int c[10][10]={'\0'},i,j; for(i=0;i<p;i++) for(j=0;j<q;j++) c[i][j]=a[i][j]+b[i][j]; printf("the matrix addition is \n"); for(i=0;i<p;i++)
Kaushik college 23 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

{ for(j=0;j<q;j++) { printf("%3d",c[i][j]); } printf("\n"); } } DESCRIPTION: In this program first we are entering the order of matrices. Order of both the matrices should be equal inorder to calculate the addition of two matrices. Then we have to enter the matrix elements and calculating c[i][j]=a[i][j]+b[i] [j].finally printing the resultant matrix C OUTPUT: Enter the order of the matrix 2 2 Enter A array elements 1 2 2 4 1 2 3 4 Enter B array elements 2 4 The matrix addition is 4 8 WEEK-5: b) (ii) STATEMENT: To implement a C program to multiply two matrices using functions. ALGORITHM:
Kaushik college 24 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

STEP-1: STEP-2: STEP-3: STEP-4: STEP-5:

declare a[10][10],b[10][10],C[10][10],p,q,p1,q1,I,j read p,q,p1,q1,a[i][j],b[i][j] c[i][j]+=a[i][k]*b[k][j] repeat step-3 until i<p,j<q1,k<q print c[i][j]

PROGRAM: #include<stdio.h> #include<conio.h> int i,j,p,q,p1,q1; main() { int a[10][10],b[10][10],C[10][10],p,q,I,j; void matrixmul(int a[][],int b[][],int,int); clrscr(); printf("enter the order of first matrix\n"); scanf("%d%d",&p,&q); printf("enter the order of second matrix\n"); scanf("%d%d",&p1,&q1); if(q==p1) { printf("enter A array elements\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&a[i][j]); printf("enter B array elements\n"); for(i=0;i<p1;i++) for(j=0;j<q1;j++) scanf("%d",&b[i][j]); matrixmul(a,b,p,q1); } else printf("matrix multiplication is not possible"); getch(); } void matrixmul(int a[10][10],int b[10][10],int p,int q1) { int c[10][10]={'\0'},i,j,k; for(i=0;i<p;i++)
Kaushik college 25 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

{ for(j=0;j<q1;j++) { c[i][j]=0; for(k=0;k<q;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } printf("the matrix multiplication is \n"); for(i=0;i<p;i++) { for(j=0;j<q1;j++) { printf("%3d",c[i][j]); } printf("\n"); } }

DESCRIPTION: In this program first we are entering the order of matrices.matrix multiplication is possible if the columns of the first matrix equals rows of the second matrix.then we have to enter the matrix elements and calculating c[i][j]+=a[i][k]*b[k][j] and finally printing the resultant matrix c. OUTPUT: Matrix enter the order of first matrix 32 enter the order of second matrix 23 enter A array elements 1 2 3 4 5 6
Kaushik college 26 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

enter B array elements 1 0 0 0 1 1 the matrix multiplication is 1 2 2 3 4 4 5 6 6

WEEK-6: a) (i) STATEMENT: To implement a C program to insert a substring in to given main string from a given position ALGORITHM: STEP-1: Declare s1,s2,p,i STEP-2: read s1,s2,p STEP-3: creating the user-defined function insert STEP-4: called function defines s3[j]=s1[i] STEP-5: repeat step-4 until i<strlen(s1) STEP-6: s1[p++]=s2[x]; x++; STEP-7: repeat step-6 until s1[x]!=\0 STEP-8: strcat(s1,s3) STEP-9: print s1 PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> int p,i; main() { char s1[20],s2[20]; void insert(char s1[],char s2[],int p);
Kaushik college 27 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

clrscr(); printf("enter first string\n"); scanf("%s",s1); printf("Enter second string\n"); scanf("%s",s2); printf("enter the position to insert the second string\n"); scanf("%d",&p); insert(s1,s2,p); getch(); } void insert(char s1[20],char s2[20],int p) { char s3[30]; int x=0,j; for(i=p,j=0;i<strlen(s1);i++,j++) s3[j]=s1[i]; s3[j]='\0'; while(s1[x]!='\0') { s1[p++]=s2[x]; x++; } s1[p]='\0'; strcat(s1,s3); printf("\nthe resultant string is: %s",s1); } DESCRIPTION: This program deals with iserting a substring into main sting from the given position. OUTPUT: Enter first string beautiworld Enter second string ful enter the position to insert the second string 6 the resultant string is=beautifulworld

Kaushik college 28 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

WEEK-6: a) (ii) STATEMENT: To implement a C program to delete n characters from a given position in a given string using functions ALGORITHM: STEP-1: Declare s1,p,n STEP-2: read s1,n,p STEP-3: creating the user-defined function delete STEP-4: called function defines I=p+n STEP-5: s1[p++]=s1[i++] STEP-6: repeat step-5 until s1[i]!=\0 STEP-7: print s1 PROGRAM: #include<stdio.h> #include<conio.h> #include<conio.h> int p,n; main() { char s1[50]; void delete(char s1[],int p,int n); clrscr(); printf("Enter the string\n"); scanf("%s",s1); printf("Enter the no of characters to be deleted\n"); scanf("%d",&n); printf("Enter the position from which characters to be deleted\n"); scanf("%d",&p); delete(s1,p,n); getch(); } void delete(char s1[20],int p,int n)
Kaushik college 29 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

{ int i; i=p+n; while(s1[i]!='\0') s1[p++]=s1[i++]; s1[p]='\0'; printf("the resultant string:%s\n",s1); } DESCRIPTION: This program deals with deleting a substring into main sting from the given position. OUPUT: Enter the string beautifulworld Enter the no of characters to be deleted 3 Enter the position from which characters to be deleted 6 the resultant string:beautiworld

WEEK-6:b) STATEMENT: To implement a C program to determine if the given string is a Palindrome or not ALGORITHM: STEP-1: declare s1,s2,I,j,l,d STEP-2: read s1 STEP-3: l=strlen(s1) STEP-4: s2[j++]=s1[i] STEP-5: reapeat step-4 until i>0 STEP-6: d=strcmp(s1,s2) STEP-7: if(d==0) Print "The sting is Palindrome
Kaushik college 30 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

STEP-8: Print "The sting is not Palindrome PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> main() { char s1[30],s2[30]; int i,j=0,l,d; clrscr(); printf("enter the string\n"); scanf("%s",s1); l=strlen(s1); for(i=l-1;i>=0;i--) s2[j++]=s1[i]; s2[j]='\0'; d=strcmp(s1,s2); if(d==0) printf("The sting is Palindrome\n"); else printf("The string is not Palindrome\n"); getch(); } DESCRIPTION: In this program first we are entering the string1,and then we are scanning this string from the last character onwards.all this scanned characters are stored in string2.now we will compare string1 and string2. after comparing two strings,if we get the value equal to zero,then the string1 is a palindrome otherwise it is not. OUTPUT: 1.Enter the string nursesrun The sting is Palindrome 2. Enter the string welcome The string is not Palindrome
Kaushik college 31 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

WEEK-7: a) STATEMENT: To implement 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. ALGORITHM: STEP-1: declare s,t,x,I,c STEP-2: read s,t STEP-3: if(S[i]==S[i-1] && t==0) i--; t=1; STEP-4: if(T[x]==S[i]) c++; x++; continue; STEP-5: if(c>=strlen(T)) s=1; break; STEP-6: x=0 c=0 STEP-7: repeat 3,4,5,6 steps until i<strlen(s) STEP-8: if(s==1 || c>=strlen(T)) Print "the Index of the String S where the String T starts i-c STEP-9: print -1 PROGRAM: #include<stdio.h> #include<conio.h> #include<string.h> main()
Kaushik college 32 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

{ char S[20],T[20]; int x=0,i,c=0,s=0,t=0; clrscr(); printf("enter string S\n"); gets(S); printf("enter String T\n"); gets(T); for(i=0;i<strlen(S);i++) { if(S[i]==S[i-1] && t==0) { i--; t=1; } if(T[x]==S[i]) { c++; x++; continue; } if(c>=strlen(T)) { s=1; break; } else { x=0; c=0; } } if(s==1 || c>=strlen(T)) printf("the Index of the String S where the String T starts=%d",i-c); else printf("-1"); getch(); }

Kaushik college 33 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

DESCRIPTION: In this program first we are entering the string S and then we are entering the string T.if the string T contains the characters of the string S, then it will prints the position or index where it starts otherwise it prints -1 OUTPUT: enter string S wonderfulworld enter String T world the Index of the String S where the String T starts=9 enter string S independenceday enter String T republic -1 WEEK-7:b) STATEMENT: To implement a C program to count no of lines ,words and characters in a given text. ALGORITHM: STEP-1: declare line[90],ctr,I,c,ch,words,lines STEP-2: line[c++]=ctr STEP-3: repeat (ctr=getchar())!='\n' STEP-4: if(line[0]=='\0') Break STEP-5: words++ STEP-6: if(line[i]==' ' || line[i]=='\t') Words++ STEP-7: repeat step-6 until line[i]!=\0 STEP-8: lines=lines+1; ch=ch+strlen(line) STEP-9: print lines,words,characters PROGRAM: #include<stdio.h> #include<conio.h>
Kaushik college 34 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

#include<string.h> main() { char line[90],ctr; int i,c,ch=0,words=0,lines=0; clrscr(); while(1) { c=0; while((ctr=getchar())!='\n') line[c++]=ctr; line[c]='\0'; if(line[0]=='\0') break; else { words++; for(i=0;line[i]!='\0';i++) if(line[i]==' ' || line[i]=='\t') words++; } lines=lines+1; ch=ch+strlen(line); } printf("\n"); printf("no of lines=%d\n",lines); printf("no of words=%d\n",words); printf("no of characters=%d\n",ch); getch(); } DESCRIPTION: This program deals with counts the number of characters, words, lines OUTPUT: hai welcome to lab .please count no of characters no of lines and word in a given text no of lines=2
Kaushik college 35 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

no of words=19 no of characters=86 WEEK-8:a) STATEMENT: To implement a triangle. ALGORITHM: STEP-1: declare I,j,n STEP-2: read n STEP-3: print STEP-4: repeat step-3 until j>=i STEP-5: print j STEP-6: repeat step-5 until j<=i STEP-7: print j STEP-8: repeat step-7 until j>=i STEP-9: repeat step-3,4,5,6,7,8 until i<=n PROGRAM: #include<stdio.h> #include<conio.h> main() { int i,j,n; clrscr(); printf("enter n value\n"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=n-1;j>=i;j--) printf(" "); for(j=1;j<=i;j++) printf("%3d",j); for(j=i-1;j>=i;j--) printf("%3d",j); }
Kaushik college 36 of ngineering vizag

C Program to generate Pascals

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

printf("\n"); getch(); } DESCRIPTION: This program deals with printing the integer values in the form of pascal triangle OUTPUT: enter n value 7 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 4 5 6 5 4 3 2 1 1 2 3 4 5 6 7 6 5 4 3 2 1 WEEK-8:b) STATEMENT: To implement a C Program to Construct Pyramid of numbers ALGORITHM: STEP-1: declare I,j,n STEP-2: read n STEP-3: print STEP-4: repeat step-3 until j>=i STEP-5: print I STEP-6: repeat step-5 until j<=i STEP-7: print I STEP-8: repeat step-7 until j>=i STEP-9: repeat step-3,4,5,6,7,8 until i<=n PROGRAM: #include<stdio.h> #include<conio.h> main() { int i,j,n;
Kaushik college 37 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

clrscr(); printf("enter n value\n"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=n-1;j>=i;j--) printf(" "); for(j=1;j<=i;j++) printf("%3d",i); for(j=i-1;j>=i;j--) printf("%3d",i); printf("\n"); } getch(); } DESCRIPTION: This program desribes to construct pyramid of numbers where the first row consists of numerical value 1,second row consists of numerical value 2 three times,third row consists of numerical value 3 five times and so on continuously. OUTPUT: enter n value 7 1 2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 WEEK-9: STATEMENT: To implement a C program to read in two numbers x and n, and then compute the sum of geometric progression. 1+x2+x3+x4+x5+------------+xn For example if n is 3 and x is 5 the the program computes 1+5+25+125
Kaushik college 38 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

Print x ,n the sum Perform error checking .For example the formula does not make sense for negative exponents if n is less than 0.Have your program print an error message if n<0,then go back and read in the next pair of numbers of without computing the sum. ALGORITHM: STEP-1: declare x,n,I,sum STEP-2: read x,n STEP-3: if(n<0) Print "please take positive value STEP-4: sum=sum+pow(x,i) STEP-5: repeat step-4 until i<=n STEP-6: print sum PROGRAM: #include<stdio.h> #include<math.h> main() { int x,n,i; float sum=1; clrscr(); l:printf("enter x and n value\n"); scanf("%d%d",&x,&n); if(n<0) { printf("please take positive value\n"); goto l; } else { for(i=1;i<=n;i++) sum=sum+pow(x,i); } printf("x value=%d,n value=%d\n",x,n); printf("%f",sum); getch();
Kaushik college 39 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

} DESCRIPTION: This program deals with calculating the expression with sum of n terms where the series consists of 1+x+x2+x3+..+xn.inorder to calculate this we have sum+=pow(x,i) and finally printing the resultant sum value. OUTPUT: enter x and n value 45 x value=4,n value=5 1365.000000

WEEK-10:a) STATEMENT: 2s complement of a number is obtained by scanning if from right to left and complementing all the bits after the first appearance of a 1.Thus 2s complement of 11100 is 00100.Write a c program to find the 2s complement of a binary number. ALGORITHM: STEP-1: declare a[10],I,n STEP-2: read n,a[i] STEP-3: if(a[i]==0) a[i]=1 STEP-4: a[i]=0 STEP-5: repeat step-3 and step-4 until i<n STEP-6: if( a[i]==0) a[i]=1 break STEP-7: a[i]=0 STEP-8: if(a[i-1]==0) STEP-9: a[i-1]=1 Break STEP-10: repeat step-6,7,8,9 until i>=0 STEP-11: print a[i]
Kaushik college 40 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

STEP-12: repeat step-11 until i<n PROGRAM: include<stdio.h> #include<conio.h> main() { int a[10],i,n; clrscr(); printf("enter no of bits\n"); scanf("%d",&n); printf("enter binary number\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { if(a[i]==0) a[i]=1; else a[i]=0; } for(i=n-1;i>=0;i--) { if(a[i]==0) { a[i]=1; break; } else { a[i]=0; if(a[i-1]==0) { a[i-1]=1; break; } } } printf("the complement form is \n");
Kaushik college 41 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

for(i=0;i<n;i++) printf("%d",a[i]); getch(); } DESCRIPTION: In this program we are calculating the 2s complement of a binary number. Inorder to find out this first we are finding the 1s complement,and then we are adding 1 to the resultant 1s complement. OUTPUT: enter no of bits 6 enter binary number 101101 the complement form is 010011 WEEK-10:b) STATEMENT: To implementa C program to convert a roman number to its decimal equivalent ALGORITHM: STEP-1: declare s[10],s STEP-2: read s STEP-3: if(!strcmp(s,i) print the equivalent decimal is 1 STEP-4: if(!strcmp(s,ii) Print decimal is 2 STEP-5: if(!strcmp(s,v) Print decimal is 5 STEP-6: if(!strcmp(s,x) Print decimal is 10 STEP-7: if(!strcmp(s,L) Print decimal is 50 STEP-8: if(!strcmp(s,C) Print decimal is 100 PROGRAM:
Kaushik college 42 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

#include<stdio.h> #include<conio.h> main() { char s[10]; clrscr(); printf(enter roman numerical); scanf(%s,&s); if(!strcmp(s,i) printf(the equivalent decimal is 1); else if(!strcmp(s,ii) printf(decimal is 2); else if(!strcmp(s,v) printf(decimal is 5); else if(!strcmp(s,x) printf(decimal is 10); else if(!strcmp(s,L) printf(decimal is 50); else if(!strcmp(s,C) printf(decimal is 100); getch(); } } DESCRIPTION: In this program first we are entering the roman numeral S,then we will compare the roman numeral S with i.if it matches then it is equal to 1.else if the roman numeral matches with ii,then it is equal to 2,else if the roman numeral matches with v,then it is equal to 5, else if the roman numeral matches with X,then it is equal to 10, else if the roman numeral matches with L,then it is equal to 50, else if the roman numeral matches with C,then it is equal to 100 OUTPUT:
Kaushik college 43 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

enter roman numerical C decimal is 100 enter roman numerical decimal is 10

WEEK-11: STATEMENT: To implement a C program that uses functions to perform the following operations. i)reading a complex number ii)writing a complex number iii)addition of two complex numbers iv)multiplication of two complex numbers ALGORITHM: STEP-1: declare r,im,a,b,c,ch STEP-2: read a,b,ch STEP-3: case 1: add complex STEP-4: case 2: mul complex PROGRAM: #include<stdio.h> #include<math.h> struct complex1 { float r,im; }; a,b,c; main() { int ch; struct complex1 read_complex(); void print_complex(struct complex1); struct complex1 add_complex(struct complex1,struct complex1); struct complex1 mul_complex(struct complex1,struct complex1 );
Kaushik college 44 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

clrscr(); printf(enter first complex number); a=read_complex(); printf(enter second complex number); b=read_complex(); printf(the first complex number is ); print_complex(a); printf(the second complex number is ); print_complex(b); printf(1.add 2. mul 3. exit); printf(enter choice); scanf(%d,&ch); switch(ch) { case 1: c=add_complex(a,b); printf(the addition of two complex numbers is); break; case2 : c=mul_complex(a,b); printf(the multiplication of two complex numbers is); break; case 3: exit(0); } print_complex(c); getch(); } struct complex1 read_complex() { struct complex1 t; printf(real and imaginary part); scanf(%f %f,&t.r,&t.im); return t; } struct complex1 add_complex(struct complex1 a, struct complex1 b) { struct complex1 x; x.r=a.r+b.r; x.im=a.im+b.im; return x; }
Kaushik college 45 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

struct complex1 mul_complex(struct complex1 a, struct complex1 b) { struct complex1 x; x.r=a.r*b.r-(a.im*b.im); x.im=a.r*b.im+(a.im*b.r); return x; } void print_complex(struct complex 1x) { char sign=+; if(x.im<0) { sign= -; x.im=abs(x.im); } printf(%0.2f %c %0.2f,x.r,sign, x.im); } DESCRIPTION: This program deals with addition of two complex numbers and multiplication of two complex numbers.addition of two complex numbers given by considering real part x.r=a.r+b.r and imaginary part as x.im=a.im+b.im. multiplicationof two complex numbers given by considering real part x.r=a.r*b.r-(a.im*b.im);and imaginary part as x.im=a.r*b.im+(a.im*b.r) OUTPUT: enter first complex number real and imaginary part 3 4 enter second complex number real and imaginary part 1 2 the first complex number is 3.00+j4.00 the second complex number is 1.00+j2.00 1.add 2. mul 3. exit enter choice 2 the multiplication of complex number is -5.00+j10.00

WEEK-12:a)
Kaushik college 46 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

STATEMENT: To implement a C program which copies one file to another ALGORITHM: STEP-1: STEP-2: STEP-3: STEP-4: STEP-5: STEP-6: STEP-7: declare *f1,*f2,ch open f1 in r mode open f2 in w mode fputc(ch,f2) repeat step-4 until ((ch=fgetc(f1)!=EOF) close file f1 close file f2

PROGRAM: #include<stdio.h> #include<process.h> #include<string.h> main() { file *f1,*f2; char ch; f1=fopen(f1.txt,r); f2= fopen(f2.txt,w); if(f1==NULL) { printf(unable to open file); exit();} if(f2==NULL) { printf(unable to open file); exit();} while((ch=fgetc(f1)!=EOF) fputc(ch,f2); fclose(f1);
Kaushik college 47 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

fclose(f2); getch(); } DESCRIPTION: In this program we are considering the two files f1,f2.where we are opening the file f1 in read mode and file f2 in write mode. We are accessing all the characters of f1 using fgetc and storing in varaiable ch. All the characters stored in ch were written onto file f2 using fputc and then finally closing both the files f1 and f2. OUTPUT: copy con f1.txt this is bvc engg college type f2.txt this is bvc engg college WEEEK-12:b) STATEMENT: To implement a C program to revrese the first n characters in a file PROGRAM: #include<stdio.h> #include<process.h> main( int argc, char *argv[]) { FILE *fp; Char s[100]; Int n; Clrscr(); Fp=fopen(argv[1],r+); If(fp==NULL) { printf(file cannot open); exit(0); } n= a to i(argv[2]); fgets(s,n,fp); strrev(s);
Kaushik college 48 of ngineering vizag

DEARTMENT OF CSE PROGRAMMING LAB

COMPUTER

rewind(fp); fputs(s,fp); fclose(fp); } DESCRIPTION: This program deals with reversing the first n characters in a file using command line arguments concept.first we are opening the file in r+ mode.usingb the statement n= a to i(argv[2]) ,all the characters in argv[2] are copied onto n.from this n ,all characters are copied onto s using fgets(s,n,fp) .then we will reverse all characters using strrev(s). OUTPUT: goto command prompt c:\tc>exefile name input filename n hello c:\tc>exe file name input file name 2 ehllo

Kaushik college 49 of ngineering vizag

Вам также может понравиться