You are on page 1of 41

COMPUTER PROGRAMMING LABORATORY

(Programs)

14CPL 16 / 14CPL26

Program 1
Design, develop a flowchart or an algorithm that takes three co-efficients (a,b,c) of a
quadratic equation (ax2+bx+c=0) as input and compute all possible roots. Implement a c
program for the developed flowchart/algorithm and execute the same to output the
possible roots for a given set of coefficients with appropriate messages.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
float a,b,c,disc,x1,x2,p,q;
clrscr();
printf("Enter the values for a, b and c\n");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
x1=x2=-b/(2*a);
printf("The real and equal roots are:\n");
printf("x1=x2=%f",x1);
}
else if(d>0)
{
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("The real and distinct roots are:\n");
printf("x1=%f\tx2=%f",x1,x2);
}
else
{
p=-b/(2*a);
q=sqrt(fabs(d))/(2*a);
printf("The complex roots are:\n");
printf("x1=%f+i%f\t",p,q);
printf("x2=%f-i%f",p,q);
}
}
DEPT OF CSE/ISE SJCIT

21

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

getch();
}

Output
1.

Enter the values for a, b and c


111
The complex roots are:
x1=-0.500000+i0.866025
x2=-0.500000-i0.866025

2.

Enter the values for a, b and c


121
The real and equal roots are:
x1=x2=-1.000000

3.

Enter the values for a, b and c


151
The real and distinct roots are:
x1=-0.208712
x2=-4.791288

Algorithm
Step 1 : Start
Step 2 : [Enter the coefficients]
Read a,b,c
Step 3: [assign b2-4ac to variable d]
d=b*b-4*a*c;
Step 4 :
case 1:[real and equal roots]
if(d==0) /* if true execute the statements otherwise goto step 6 */
begin
x1=x2=-b/(2*a);
print "The real and equal roots
print "x1and x2
end if goto step 8
Step 5 :

DEPT OF CSE/ISE SJCIT

22

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

case 2: [real and distinct roots]


if (d>0) /* if true execute the statements otherwise goto step 7*/
begin
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
print "The real and distinct roots "
print x1 and x2
end if
Step 6 :
case 3: [real and imaginary(complex) roots]
if(d<0)
begin
p=-b/(2*a);
q=sqrt(fabs(d))/(2*a);
printf("The complex roots are:\n");
print "x1=p+iq
print "x2=p-iq
end if
Step 7: Stop

Program 2
DEPT OF CSE/ISE SJCIT

23

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Design and develop an algorithm to find the reverse of an integer number num and
check whether it is palindrome or not.Implement a C program for the developed algorithm
that takes an integer number as input and output the reverse of the same with suitable
messages. Ex: Num:2014,reverse:4102,not a palindrome.
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,rev,digit;
clrscr();
printf("Enter a number:\n");
scanf("%d",&n);
m=n;
rev=0;
while(n!=0)
{
digit=n%10;
n=n/10;
rev=rev*10+digit;
}
printf("the reverse of %d is %d\n",m,rev);
if(m==rev)
printf("The number %d is a palindrome",m);
else
printf("The number %d is not a palindrome",m);
getch();
}

Output
1.

Enter a number:
1221
the reverse of 1221 is 1221
The number 1221 is a palindrome

2.

Enter a number:
1234
the reverse of 1234 is 4321

DEPT OF CSE/ISE SJCIT

24

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

The number 1234 is not a palindrome

Algorithm
Step 1 : Start
Step 2 : [Enter a number]
Read n
Step 3: [Assign values]
m=n;
rev=0;
Step 4: [ compute reverse]
while(n!=0) /* if true execute the statements below otherwise terminate */
digit=n%10;
n=n/10;
rev=rev*10+digit;
end while
Step 5: [print reverse]
Print rev
Step 6: [check for palindrome]
if(m==rev) /* if true print palindrome otherwise not palindrome */
print "The number is a palindrome
otherwise
print "The number is not a palindrome.
end if
Step 7: Stop

Program 3
a. Design and develop a flowchart to find the square root of a given number N. Implement
a C program for the same and execute for all possible inputs with appropriate

DEPT OF CSE/ISE SJCIT

25

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

messages. Note: Dont use library function sqrt(n).


#include<stdio.h>
#include<conio.h>
double sqrt(double n)
{
double lb,ub,t;
int ite=35;
lb=0.0;
ub=n;
while(ite>0)
{
t=((lb+ub)/2);
if((t*t)==n)
return t;
else if((t*t)>n)
ub=t;
else
lb=t;
ite--;
}
return ((lb+ub)/2);
}
void main()
{
double n,result;
clrscr();
printf("enter the number\n");
scanf("%lf",&n);
if(n<0)
{
printf("not possible to find square root\n");
getch();
exit(0);
}
result=sqrt(n);
printf("square root of %lf is=%lf",n,result);
getch();

DEPT OF CSE/ISE SJCIT

26

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

OUTPUT
1. enter the number
5
square root of 5.000000 is=2.236068
2. enter the number
15
square root of 5.000000 is=3.87298

Algorithm
Step 1 : Start
Step 2 : [Enter a number]
Read n
Step 3: [Check validity]
If(n<0)
Print invalid input and goto step 6
Else goto step 4
Step 4: [Compute square root of n]
result=sqrt(n);
Step 5: [print square root ]
Print result
Step 6: Stop

Algorithm for sqrt(n)


Step 1 : Start
Step 2 : [Initialization]

DEPT OF CSE/ISE SJCIT

27

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

int ite=35;
lb=0.0;
ub=n;
Step 3: [Computation]
while(ite>0)
{
t=((lb+ub)/2);
if((t*t)==n)
return t;
else if((t*t)>n)
ub=t;
else
lb=t;
ite--;
}
Step 4: [return result]
return ((lb+ub)/2);

3b. Design and develop a C program to read a year as an input and find whether it is leap
year or not. Also consider end of the centuries.
#include<stdio.h>
#include<conio.h>
void main()
{

DEPT OF CSE/ISE SJCIT

28

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

int year;
clrscr();
printf("enter any year\n");
scanf("%d",&year);
if((year%4==0 && year%100!=0)||year%400==0)
printf("the year %d is a leap year\n",year);
else
printf("the year %d is not a leap year\n",year);
getch();
}

OUTPUT
1. enter any year
2010
the year 2010 is not a leap year
2. enter any year
2000
the year 2000 is a leap year

Algorithm
Step 1 : Start
Step 2 : [Read year]
Step 3: [Check for leap year and end of century]
if((year%4==0 && year%100!=0)||year%400==0)
print the year is a leap year
else
print the year is not a leap year
Step 4: Stop

Program 4
Design, develop and execute a program in C to evaluate the given polynomial
f(x) = a4x4 + a3x3 + a2x2 + a1x + a0 for given value of x and the coefficients using Horners
method.
#include<stdio.h>

DEPT OF CSE/ISE SJCIT

29

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

#include<conio.h>
void main()
{
int a[100],sum=0,x,n,i;
clrscr();
printf("Enter the degree of the polynomial:\n");
scanf("%d",&n);
printf("Enter %d coefficients into array:\n",n+1);
for(i=0;i<=n;i++)
scanf("%f",&a[i]);
printf("Enter value of x:\n");
scanf("%d",&x);
sum=a[n]*x;
for(i=n-1;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf("The sum of polynomial = %.d\n",sum);
getch();
}

Output
1. Enter the degree of the polynomial:
3
Enter 4 coefficients into array:
1
2
3
4
Enter value of x:2
The value of f(2.00) = 26.00

DEPT OF CSE/ISE SJCIT

30

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

2. Enter the degree of the polynomial:


2
Enter 3 coefficients into array:
3
5
7
Enter value of x : 1
The sum of polynomial = 41

Algorithm
Step 1 : Start
Step 2 : [Enter the degree of polynomial ]
Read n
Step 3: [read the co-efficients ]
for i=0 to n
Read a[i]
end for
Step 4 : [Input the value of x]
Read x
Step 5 : [Inmitialise sum]
sum=a[n]*x;
step 6:[compute polynomial]
for i=n-1 down to 1
sum=(sum+a[i])*x;
end for
step 7 : [Finally add the first term]
sum=sum+a[0];
step 8 : [print the result]
print sum
Step 9: Stop

DEPT OF CSE/ISE SJCIT

31

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Program 5
Draw the flowchart and write a c program to compute sin(x) using taylor series
approximation given by sin(x)=x-(x3/3!)+(x5/5!)-(x7/7!)+.. compare your results with the
built-in library function. print both the results with appropriate messages.
#include<stdio.h>
#include<conio.h>
#include<math.h>
double fact(double a)

DEPT OF CSE/ISE SJCIT

32

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

{
double i,f=1;
for(i=1;i<=a;i++)
f=f*i;
return f;
}
void main()
{
double sin_x=0,x,j,r,neg=-1;
clrscr();
printf("enter the value of x: \n");
scanf("%lf",&x);
r=((x*3.14)/180);
printf("converted value=%lf\n",r);
for(j=1;j<=15;j=j+2)
{
neg=neg*(-1);
sin_x=sin_x+((pow(r,j)/fact(j))*neg);
printf("built-in is %lf and calculated value is %lf\n",sin(r),sin_x);
getch();
}
getch();
}

OUTPUT
enter the value of x:
90
converted value=1.570000
built-in is 1.000000 and calculated value is 1.570000
built-in is 1.000000 and calculated value is 0.925018
built-in is 1.000000 and calculated value is 1.004509
built-in is 1.000000 and calculated value is 0.999843
built-in is 1.000000 and calculated value is 1.000003
built-in is 1.000000 and calculated value is 1.000000

DEPT OF CSE/ISE SJCIT

33

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

built-in is 1.000000 and calculated value is 1.000000


built-in is 1.000000 and calculated value is 1.000000

Algorithm
Step 1: Start
Step 2: [Initialize]
Sin_x= 0
Step 3: Read the value of x
Step 4: Convert x to radians (x*3.14/180)
Step 5:[Compute sin(r)
for(j=1;j<=15;j=j+2)
{
neg=neg*(-1);
sin_x=sin_x+((pow(r,j)/fact(j))*neg);
print built-in value and calculated value
}
Step 6: Stop

Program 6
Develop an algorithm ,implement and execute a C program that reads N integer
numbers and arrange them in ascending order using bubble sort technique.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,temp,a[100];
clrscr();
DEPT OF CSE/ISE SJCIT

34

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

printf("Enter the value for n:\n");


scanf("%d",&n);
printf("Enter %d elements into array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The original elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=1;i<n;i++)
{
for(j=0 ; j< n-i ; j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("\nThe sorted array is:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Output
1.

Enter the value for n:


5
Enter 5 elements into array:
2
13
8
9
5
The original elements are:
2
13
8
9
5
The sorted array is:

DEPT OF CSE/ISE SJCIT

35

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

13

Algorithm
Step 1 : Start
Step 2 : [Enter a number]
Read n
Step 3: [Enter the elements ]
for i=0 to n
read a[i]
Step 4: [print the elements ]
for i=0 to n
print a[i]
Step 5: [sort the elements ]
/* terminate the inner loop first then update the outer loop]
for i=1 to n [number of passes required]
for j=0 to n-i [access the element in each pass]
if(a[j]>a[j+1])
then
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
end if
end for
end for
Step 6: [ print the sorted elements ]
for i=0 to n
print a[i]
Step 7 : Stop

DEPT OF CSE/ISE SJCIT

36

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Program 7
Develop, implement and execute a C program that reads two matrices A (M x N)
and B (P x Q) and compute product of matrices A and B. Read matrix A matrix B in row
major order and in column major order respectively. print both the input matrices and
the resultant matrix with suitable headings and output should be in matrix format only.
Program must check the compatibility of orders of the matrices for multiplication.
Report appropriate message in case of incompatibility.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;

DEPT OF CSE/ISE SJCIT

37

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

clrscr();
printf("enter the order of the matrix A\n");
scanf("%d%d",&m,&n);
printf("enter the order of the matrix B\n");
scanf("%d%d",&p,&q);
if(n!=p || m!=q)
{
printf("matrix multiplication not possible\n");
getch();
exit(0);
}
printf("enter the elements of matrix A\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("enter the elements of matrix A\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
}
printf("Matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("Matrix B\n");
DEPT OF CSE/ISE SJCIT

38

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
printf("Matrix C\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}

Output
enter the order of the matrix A
2
2
enter the order of the matrix B
2
2
enter the elements of matrix A
2
2
2
2
enter the elements of matrix B
2
2
2
2
Matrix A
2
2
2
2
Matrix B
2
2
2
2
Matrix C

DEPT OF CSE/ISE SJCIT

39

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

8
8

14CPL 16 / 14CPL26

8
8

Algorithm
Step 1 : Start
Step 2 : [Enter the order of matrix A]
Read m, n
Step 3: [Enter the order of matrix B]
Read p,q
Step 4 : [check for matrix multiplication is possible or not]
if(n!=p)
print "matrix multiplication not possible.
Exit
If true terminate otherwise goto step7
Step 5: [Enter the elements of matrix A ]
for i=0 to m
for j=0 to n
read a[i][j]
Step 6: [Enter the elements of matrix B ]
for i=0 to p
for j=0 to q
read b[i][j]
Step 7: [ Compute matrix multiplication]
For i=0 to m If true compute the following steps otherwise goto step 8
For j=0 to q
If true compute the following steps otherwise increment the i loop.
c[i][j]=0;
for(k=0;k<n;k++) If true compute the following steps otherwise
increment the j loop.
c[i][j]+=a[i][k]*b[k][j]; //equation to compute multiplication.

DEPT OF CSE/ISE SJCIT

40

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Step 8: [print the elements of Matrix A B C ]


print a[i][j],b[i][j],c[i][j]
Step 9 : Stop

Program 8:
Develop, implement and execute a c program to search a name in a list of names
using binary searching technique.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int n,i,low,high,mid;
char key[20],a[50][50];
clrscr();
printf("Enter the number of names:\n");
scanf("%d",&n);
printf("\nEnter the names in ascending order:\n",n);

DEPT OF CSE/ISE SJCIT

41

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

for(i=0;i<n;i++)
scanf("%s",a[i]);
printf("\nEnter the name to be searched:\n");
scanf("%s",key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(key,a[mid]) == 0)
{
printf("\nkey is found at location %d\n",mid+1);
getch();
exit(0);
}
if(strcmp(key,a[mid]) > 0)
{
high=high;
low=mid+1;
}
else
{
low=low;
high=mid-1;
}
}
printf("name is not found in the list");
getch();
}

OUTPUT:Enter the number of names:


5
Enter the names in ascending order:
anusha

DEPT OF CSE/ISE SJCIT

42

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

bishal
harish
kunal
kushal
Enter the name to be searched:
kunal
key is found at location 4
2. Enter the number of names:
5
Enter the names in ascending order:
anusha
bishal
harish
kunal
kushal
Enter the name to be searched:
disha
name is not found in the list

Algorithm
Step 1: Start
Step 2: [Read n]
Read the number of strings, n
Step 3:[Read strings]
Read n strings
Step 4:[Read key]
Read the key string, key, to search
Step 5: [Initialize]
low=0 and high=n-1
Step 6: Do the following till low<=high else go to step 10
Step 7: Find mid=(low+high)/2
Step 8: Check if key = a[mid] then go to step 9, or If key > a[mid] then take low = high+1,
Else if key < a[mid] then take high = mid 1 and go to step 6.
Step 9: Print that The key is found at position mid+1 and go to last step
Step 10: Print that The key is not found
Step 11: Stop
DEPT OF CSE/ISE SJCIT

43

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Program 9:
write and execute a c program that
i. implements string copy operations STRCOPY(str1,str2) that copies a string str1 to
another string str2 without using library function.
ii. reads a sentence and prints frequency of each of the vowels and total count of
consonants.
#include<stdio.h>
#include<conio.h>
void strcopy(char *s1,char *s2)
{
int i;
for(i=0;s1[i]!='\0';i++)
s2[i]=s1[i];
s2[i]='\0';
}
void vow_cons(char *s)
{
int v[5]={0,0,0,0,0},c=0;
for(i=0;s[i]!='\0';i++)

DEPT OF CSE/ISE SJCIT

44

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

if(isalpha(s[i]))
switch(tolower(s[i]))
{
case 'a': v[0]+=1;
break;
case 'e': v[1]+=1;
break;
case 'i': v[2]+=1;
break;
case 'o': v[3]+=1;
break;
case 'u': v[4]+=1;
break;
case ' ': break;
default: c++;
}
else continue;
printf("given sentence is=%s\n",s);
printf("vowels are\n a=%d\n e=%d\n i=%d\n o=%d\n d\n",v[0],v[1],v[2],v[3],v[4]);
printf("total no. of consonants are=%d\n",c++);
}
void main()
{
int ch;
char s1[30],s2[30],s[30];
clrscr();
while(1)
{
printf("\n 1. to copy string 2. count vowels and consonanats 3. exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter string 1\n");
flushall();
gets(s1);
strcopy(&s1,&s2);
printf("copied string is =%s\n",s2);
DEPT OF CSE/ISE SJCIT

45

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

break;
case 2:printf("enter the sentence\n");
flushall();
gets(s);
vow_cons(&s);
break;
case 3:exit(0);
default: printf("choice is invalid\n");
}
}
getch();
}

OUTPUT:1. to copy string 2. count vowels and consonanats 3. exit


enter your choice
1
enter string 1
we are indians
copied string is =we are indians
1. to copy string 2. count vowels and consonanats 3. exit
enter your choice
2
enter the sentence
we are indians
given sentence is=we are indians
vowels are
a=2
e=2
i=2
o=0
u=0
total no. of consonants are=6
1. to copy string 2. count vowels and consonanats 3. exit
enter your choice
3

DEPT OF CSE/ISE SJCIT

46

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Algorithm
Step 1: Start
Step 2: Read a string to copy
Step 3: [Initialization]
i=0, pointing to the first character of the string read
Step 4: If the character is not \0 then copy it to second string else go to step 6
Step 5: Increment i by one go to step 4
Step 6: Read a string to count vowels and consonants
Step 7: Initialize i=0, pointing to the first character of the string read
Step 8: If the character is \0 then go to step 10 else
Step 9: If the character is a vowel then increment the vowel counter else increment consonant
counter
Step 10: Increment i by one go to step 8
Step 11: Print the copied string and print the number of vowels and consonants
Step 12: Stop

Program 10
10a. Design and develop a C function rightshift (x, n) that takes two integers x and n as
input and returns value of the integer x rotated to the right by n bit positions. Assume the
integers are unsigned integer. Write a C program that invokes this function from the main
with different values for x and n and tabulate the results with suitable headings.
#include<stdio.h>
#include<conio.h>
unsigned int rightrot(unsigned x, unsigned n)
{
int i;
for(i=1;i<=n;i++)
{
if(x%2==0)
x=x>>1;
else
{
x=x>>1,x+=32768;
}
}
return x;
}
void main()
{

DEPT OF CSE/ISE SJCIT

47

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

unsigned int x,result;


int i,n;
clrscr();
printf("Enter the value of x\n");
scanf("%u",&x);
printf("Enter the value of n\n");
scanf("%u",&n);
for(i=1;i<=n;i++)
{
result=rightrot(x,i);
printf("\nright rotated(%u,%d)=%u",x,i,result);
}
getch();
}

OUTPUT:Enter the value of x


1
Enter the value of n
2
right rotated(1,1)=32768
right rotated(1,2)=16384

Algorithm for main() function:


Step 1: Start
Step 2: Read an unsigned integer that should be less than or equal to 65535.
Step 3: Read the value of x i.e. how many times to rotate.
Step 4: Print the output that the function rightrot(x,n) call returns
Step 5: Stop

Algorithm for rightrot(x,n) function:


Step 1: Start
Step 2: Initialize i=1
Step 3: Check if i is less than or equal to n. If true, then go to Step 4 else go to Step 8
Step 4: Divide x by 2. If remainder is 0 then go to Step 5 else, go to Step 7
Step 5: x=x>>1
Step 6: increment i, go to step 3
Step 7: x=x>>1 and x+=32768;

DEPT OF CSE/ISE SJCIT

48

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Step 8: Increment i, go to step 3


Step 9: Return x

10 b. Design and develop a c function isprime(num) that accepts an integer argument and
return 1 if the argument is prime, 0 otherwise. write a c program that invokes the function
to generate prime numbers between the given range.
#include<stdio.h>
#include<conio.h>
int isprime(int m)
{
int i;
for(i=2;i<=m/2;i++)
{
if(m%i==0)
{
return 0; /* not prime*/
}
}
return 1;
}
void main()
{
int i,p,q;
clrscr();
printf("enter the lower and upper limit\n");
scanf("%d%d",&p,&q);
printf("prime numbers between %d and %d are :\n",p,q);
for(i=p;i<=q;i++)

DEPT OF CSE/ISE SJCIT

49

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

if(isprime(i))
printf(" %d",i);
getch();
}

OUTPUT:enter the lower and upper limit


2
20
prime numbers between 2 and 20 are :
2 3 5 7 11 13 17 19

Algorithm
Step 1: Start
Step 2: Read the limit, m,n
Step 3: Initialize i=m,
Step 4: Do the following till i<=n else go to step 13
Step 5: Call the sub function isprime on value of i, which is as follows
Step 6: If i is en even number return 0 indicating i is not a prime number, else
Step 7: Initialize j=2
Step 8: Do the following till j<=sqrt(i) else go to step 15
Step 9: If i % j == 0 then return 0, and go to step 15
Step 10: Increment j by 1 go to step 8
Step 11: Return 1 by printing i is prime and go to step 12
Step 12: Increment i by 1 and go to step 4
Step 13: Stop

DEPT OF CSE/ISE SJCIT

50

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Program 11:
Draw the flowchart and write a recursive C function to find the factorial of a number, n!,
defined by fact(n)=1, if n=0. Otherwise fact(n)=n*fact(n-1). Using this function, write a C
program to compute the binomial coefficient nCr. Tabulate the results for different values
of n and r with suitable messages.
#include<stdio.h>
#include<conio.h>
#include<math.h>
long int fact(long int k)
{
if(k==0)
return 1;
else
return (k*fact(k-1));
}
void main()
{
long int n,r,i,j,ncr=0;
clrscr();
printf("\n enter the values of n and r:");
scanf("%ld%ld",&n,&r);
if(n<0||r<0)
printf("\n please enter positive numbers");
else
{

DEPT OF CSE/ISE SJCIT

51

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

for(i=1;i<=n;i++)
for(j=1;j<=r;j++)
if(i>=j)
{
ncr=fact(i)/(fact(j)*fact(i-j));
printf("\n for n=%ld and r=%ld, %ldC%ld=%ld",i,j,i,j,ncr);
}
}
getch();
}

OUTPUT:enter the values of n and r:


3
2
for n=1 and r=1, 1C1=1
for n=2 and r=1, 2C1=2
for n=2 and r=2, 2C2=1
for n=3 and r=1, 3C1=3
for n=3 and r=2, 3C2=3

Algorithm
Step 1: Start
Step 2: Read the values of n and r
Step 3: Check if n<0 or r<0 print Enter positive nos, go to step 15
Step 4: Initialize nCr=0
Step 5: Initialize i=1
Step 6: Till i<=n, do the following steps, else go to step 14
Step 7: Initialize j=1
Step 8: Till j<=r, do the following steps, else go to step 14
Step 9: Check if i>= j do the following steps else go to step 6
Step 10: To assign nCr = (fact(i) * fact(j)) / fact(i-j), call fact(k) function
Step 11: Check whether k=0, if so return(1) and go to step 13, else go to step 12
Step 12: Call fact with (k * fact(k-1)), go to step 11
Step 13: Print the value of nCr
Step 14: Stop

DEPT OF CSE/ISE SJCIT

52

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Program 12
Given two university information files studentname.txt and usn.txt that contains
students Name and USN respectively. write a c program to create a new file called
output.txt and copy the contents of files studentname.txt and usn.txt into output
file in the sequence shown below. Display the contents of output file output on to the
screen.
Student Name
Name1
Name2
..

USN
USN1
USN2
.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main( )
{
FILE *fp1,*fp2,*fp3;
char usn[20],name[20];
clrscr( ) ;
fp1=fopen("studname.txt","r");
if (fp1==NULL)
{
printf("studname.txt not found\n");
exit( 0);
}
fp2=fopen ("studusn.txt" ,"r");
if (fp2==NULL)
DEPT OF CSE/ISE SJCIT

53

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

{
printf("studusn.txt not found\n");
exit( 0);
}
fp3=fopen ("output.txt" ,"w");
if (fp3==NULL)
{
printf("output.txt not found\n");
exit( 0);
}
while (!feof(fp1) && !feof(fp2) )
{
fscanf(fp1,"%s",name);
fscanf(fp2,"%s",usn);
fprintf(fp3,"%s
%s\n",name,usn);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp3=fopen("output.txt","r");
printf("\n---------------------------------------------------------\n");
printf("name
usn\n");
printf("\n---------------------------------------------------------\n");
while(!feof(fp3))
{
fscanf(fp3,"%s",name);
fscanf(fp3,"%s \n",usn);
printf(" %15s
%10s \n", name,usn);
}
fclose(fp3);
getch( );
}

DEPT OF CSE/ISE SJCIT

54

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Output:--------------------------------------------------------name
usn
--------------------------------------------------------Avni
11
Samarth
12
Prathiba
13

Algorithm
Step1: Start
Step 2: [Declaration]
Declare three file pointers fp1,fp2,fp3
Step 3: [Open studname.txt file]
fp1=fopen(studname.txt,r);
Step 4: [Open studusn.txt file]
fp2=fopen(studusn.txt,r);
Step 5: [Open output.txt file]
fp3=fopen(output.txt,r);
Step 6: while (!feof(fp1) && !feof(fp2) )
{
Read name from fp1;
Read usn from fp2;
Write Name and usn onto fp3;
}
Step 7: Close all files
Step 8:[Open output.txt file]
Read name and usn from fp3 and write them onto the standard ouput
screen,console
Step 9: Stop

DEPT OF CSE/ISE SJCIT

55

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Program 13
Write a C program to maintain a record of n student details using an array of
structures with four fields (Roll number, Name, Marks, and Grade). Each field is of an
appropriate data type. Print the marks of the student given student name as input.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int rollno,marks;
char name[20],grade[1];
};
void main()
{
int i,n,found=0;
struct student s[10];
char sname[20];
clrscr();
printf("enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter %d students details\n",i+1);
printf("enter name, rollno, marks and grade of student %d \n",i+1);
scanf("%s%d%d%s", s[i].name, &s[i].rollno, &s[i].marks, s[i].grade);
}
printf("\n student details are \n");
DEPT OF CSE/ISE SJCIT

56

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

printf("\n rollno \t name \t marks \t grade \n");


for(i=0;i<n;i++)
printf("\t%d\t %s\t %d\t %s\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
printf("enter the student name to print the marks\n");
scanf("%s",sname);
for(i=0;i<n;i++)
{
if(strcmp(sname,s[i].name)==0)
{
printf("\n marks of the student is= %d\n",s[i].marks);
found=1;
}
}
if(found==0)
printf("\n student is not found");
getch();
}
OUTPUT:1. enter the number of students
2
enter 1 students details
enter name, rollno, marks and grade of student 1
ajith
1
10
A
enter 2 students details
enter name, rollno, marks and grade of student 2
akash
2
20
B
student details are
rollno
1
2

name marks grade


ajith
10
A
akash 20
B

DEPT OF CSE/ISE SJCIT

57

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

enter the student name to print the marks


ajith
marks of the student is= 10
2. enter the number of students
2
enter 1 students details
enter name, rollno, marks and grade of student 1
disha
1
20
A
enter 2 students details
enter name, rollno, marks and grade of student 2
diya
2
30
B
student details are
rollno
1
2

name marks grade


disha 20
A
diya
30
B

enter the student name to print the marks


akash
student is not found

Algorithm
Step 1: Start
Step 2: Read the size of the students array, n
Step 3: Initialize i=1
Step 4: Till i<=n do the following into the std structure
Step 5: Read ith std[i].students roll no, std[i].name, std[i].marks and std[i].grade
Step 6: Increment I by 1, go to step 4
Step 7: Read the name of the student to search, Name
Step 8: Initialize i=1
Step 9: Check if Name = std[i].name, print the details of the student, go to step 11
Step 10: Increment I, go to step 9
Step 11: Stop
DEPT OF CSE/ISE SJCIT

58

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

Program 14
Write a C Program using pointers to compute e the sum, mean and standard
deviation of all elements stored in an array of n real numbers.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[20],mean,sum=0,*p,sd,var;
int n,i;
clrscr();
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%f",&x[i]);
p=x;
for(i=0;i<n;i++)
{
sum=sum+(*p);
p=p++;
}
mean=sum/n;
printf("sum of %d elements=%f\n",n,sum);
printf("mean of %d elements=%f\n",n,mean);
sum=0;
p=x/* updating the pointer to point to the first element*/
for(i=0;i<n;i++)
{
sum=sum+pow((*p-mean),2);
p=p++;
}
DEPT OF CSE/ISE SJCIT

59

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

var=sum/n;
printf("varience=%f\n",var);
sd=sqrt(var);
printf("standard deviation of %d elements=%f\n",n,sd);
getch();
}

OUTPUT:enter the size of array


5
enter 5 elements
1
2
3
4
5
sum of 5 elements=15.000000
mean of 5 elements=3.000000
varience=7.400000
standard deviation of 5 elements=2.720294

Algorithm
Step 1: Start
Step 2: [Read n]
Read the size of the array
Step 3: Read the array elements]
Read the n elements of the array
Step 4: [Initialization]
Assign p=x, i.e, the base address of the array should be assigned to the pointer
Step 5:[Compute sum]
for(i=0;i<n;i++)
{
sum=sum+(*p);
p=p++;
}
Step 6:[Compute mean]

DEPT OF CSE/ISE SJCIT

60

2015

COMPUTER PROGRAMMING LABORATORY


(Programs)

14CPL 16 / 14CPL26

mean=sum/n;
Step 7:initialize sum=0, p=x;
Step 8:[Compute Standard deviation]
for(i=0;i<n;i++)
{
sum=sum+pow((*p-mean),2);
p=p++;
}
var=sum/n;
sd=sqrt(var);
Step 9: Stop

DEPT OF CSE/ISE SJCIT

61

2015

You might also like