0 Yn 6 Tes
0 Yn 6 Tes
0 Yn 6 Tes
EXERCISE-1:
a) AIM: Write a c program to find the sum of individual digits of the given positive integer
ALGORITHM:
Step 1: Start.
Step 2: Take variables namely sum, n.
Step 3: Read n value.
Step 4: Initialize sum=0.
Step 5: While(n>0) then
sum=sum+(n%10);
n=n/10;
Step 6: Write sum of the digits.
Step 7: Stop.
FLOWCHART:
Start
Sum = 0
Read n value
True False
While(n>0)
Sum=sum+(n%10)
n = n/10
Write sum
Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,n;
clrscr();
printf("Enter any n value:");
scanf("%d",&n);
while(n>0)
{
sum=sum+(n%10);
n=n/10;
}
printf("Sum of the digits of given number is:%d",sum);
getch();
}
OUTPUT:
b) AIM: A Fibonacci series is defined as follows: the first and second terms in the sequence are 0 and
1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a c program
to generate the first n terms of the sequence.
ALGORITHM:
Step 1: Start.
t=t1+t2.
Display t value.
t1=t2.
t2=t.
Step 7: Stop.
FLOWCHART:
Start
Read n value
t1 =0
t2=1
Display t1
and t2
i =1
False True
i <=n-2
t = t1+t2
Display t
value.
t1 = t2
i++
Stop t2 = t
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,t1=0,t2=1,t,i;
clrscr();
printf("Enter any n value:");
scanf("%d",&n);
printf("Fibonancci series is:");
printf("%5d%5d",t1,t2);
for(i=1;i<=n-2;i++)
{
t=t1+t2;
printf("%5d",t);
t1=t2;
t2=t;
}
getch();
}
OUTPUT:
c) AIM: Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.
ALGORITHM:
Step 1: Start.
count=0.
if(i%j==0)
count++
if(count==2)
Display i value.
Step 5: Stop.
FLOWCHART:
Start
Read n
value.
i = 1
False True
i <= n
count = 0
i ++
j = 1
True False
j <= i
count++
Display
i
Stop
j ++
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,count,i,j;
clrscr();
printf("Enter any n value:");
scanf("%d",&n);
printf("Prime numbers from 1 to %d are:",n);
for(i=1;i<=n;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%5d",i);
}
getch();
}
OUTPUT:
WEEK-2:
ALGORITHM:
Step 1: Start.
Step 2: Take variables namely n,x,i,p;
Step 3: Initialize p to 0 and sum to 0
Step 4: Read n and x values.
Step 5: for i=1 to i<=n then i++
Step 6: if(i%2!=0) then
sum=sum+pow(x,p)/fact(p)
goto step9.
Step 7: else
sum=sum-pow(x,p)/fact(p)
goto step9.
p=p+2
step 8: Display the sum value.
Step 9: fact(int x)
if(x<=1) then
Return 1 to step5
else
Return x*fact(x-1) to step 5.
Step 10: Stop.
FLOWCHART:
Start
p=0
x=0
Read n,x
values
i=1
False
i<=n
True
True False
i%2!=0
sum=sum+pow(x,p)/fact(p) sum=sum-pow(x,p)/fact(p)
Display
sum True False
x<1
p=p+2
i++
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
unsigned long fact(int x)
{
if(x<=1)
return 1;
else
return x*fact(x-1);
}
void main()
{
int n,x,i,p=0;
float sum=0;
clrscr();
printf("Enter numer of terms:");
scanf("%d",&n);
printf("Enter x value:");
scanf("%d",&x);
for(i=1;i<=n;i++)
{
if(i%2!=0)
sum=sum+pow(x,p)/fact(p);
else
sum=sum-pow(x,p)/fact(p);
p=p+2;
}
printf("Sum of given series is:%.2f",sum);
getch();
}
OUTPUT:
Enter numer of terms:4
Enter x value:1
Sum of given series is:0.54
b) AIM:
ALGORITHM:
Step 1: Start.
Step 2: Take variables namely a,b,c,x1,x2.
Step 3: Read a,b and c values.
Step 4: if b2-4ac<0 then
s=abs(b*b-4*a*c)
Find real part r=-b/(2*a) value
Find imaginary part i=s/(2*a)
Display the First root value is (r,i)
Display the Second root value is(r,-i)
else
Find first root value x1=(-b+sqrt(b*b-4*a*c))/(2*a)
Find the second root valuex2=(-b-sqrt(b*b-4*a*c))/(2*a)
Display x1 and x2.
Step 5: Stop.
FLOWCHART:
Start
Read a and b
values
True False
b2-4ac<0
S=abs(b*b-4*a*c) x1=(float)(-b+sqrt(b*b-(4*a*c)))/(2*a)
r=-b/(2*a) X2=(float)(-b-sqrt(b*b-(4*a*c)))/(2*a)
i=s/(2*a)
Display x1,x2
Display (r,i)
Display (r,-i)
Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float x1,x2,s,r,i;
clrscr();
printf("Enter a,b and c values:");
scanf("%d%d%d",&a,&b,&c);
if((b*b-4*a*c)<0)
{
s=abs(b*b-4*a*c);
r=-b/(2*a);
i=s/(2*a);
printf("First root value is:(%f,%f)",r,i);
printf("\nSecond root value is:(%f,%f)",r,-i);
}
else
{
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("\nFirst root value is:%f",x1);
printf("\nSecond root value is:%f",x2);
}
getch();
}
OUTPUT:
WEEK-3:
a) AIM
The total distance travelled by vehicle in„t‟ seconds is given by distance = ut+1/2at 2 where „u‟
and „a‟ are the initial velocity (m/sec.) and acceleration (m/sec 2). Write C program to find the distance
travelled at regular intervals of time given the values of „u‟ and „a‟. The program should provide the
flexibility to the user to select his own time intervals and repeat the calculations for different values of
„u‟ and „a‟.
ALGORITHM:
Step 1: Start.
Step 2: Take variables namely u, a, t and s.
Step 3: Read u, a, and t.
Step 4: s=u*t+ ½ *a*square(t)
Step 5: Write s
Step 6: Stop.
FLOWCHART:
Start
Read u, a and t
s=u*t+ ½ *a*square(t)
display s
Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float u,a,t,dis;
clrscr();
printf("Enter initial velocity(u):");
scanf("%f",&u);
printf("Enter acceleration(a):");
scanf("%f",&a);
printf("Enter travelled time(t):");
scanf("%f",&t);
dis=u*t+((float)1/2*a*pow(t,2));
printf("Distance travelled is:%f",dis);
getch();
}
OUTPUT:
b) AIM: Write a C program, which takes two integer operands and one operator form the user,
performs the operation and then prints the result. (Consider the operators +,- ,*, /, % and use Switch
Statement)
ALGORITHM:
Step 1: Start.
Step 5: Stop.
FLOWCHART:
Start
True
Case „+‟ Write a+b
False
True
Case „-„ Write a-b
False
True
Case „*‟ Write a*b
False
True
Case „/‟ Write a/b
False
True
Case „%‟ Write a%b
False
Invalid option
Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
char opt;
clrscr();
printf("Enter a and b values:");
scanf("%d%d",&a,&b);
printf("Enter any option:");
flushall();
scanf("%c",&opt);
switch(opt)
{
case '+':
printf("Addition of given two values is:%d",a+b);
break;
case '-':
printf("Subtraction of given two values is:%d",a-b);
break;
case '*':
printf("Multiplication of given two values is:%d",a*b);
break;
case '/':
printf("Division of given two values is:%d",a/b);
break;
case '%':
printf("Modulus of given two values is:%d",a%b);
break;
default:
printf("Invalid option");
}
getch();
}
OUTPUT:
WEEK-3:
i) AIM:
Write a c program to find the factorial of given number using recursive function
ALGORITHM:
Step 1: Start.
Step 2: Take variables namely n,f.
Step 3: Read n value.
Step 4: f=fact(n)
Step 5: if(n<=1) then
Step 6: return 1 to step4
Step 7: else return x*fact(x-1) to step 4.
Step 8: Display f value.
Step 9: Stop
Start
FLOWCHART:
Read n
f=fact(n)
Stop False
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned long fact(int);
int n;
unsigned long f;
clrscr();
printf("Enter any n value:"); OUTPUT
scanf("%d",&n);
f=fact(n); Enter any n value:5
printf("Factorial of given value is:%lu",f); Factorial of given value is:120
getch();
}
unsigned long fact(int n)
{
if(n<=1)
return 1;
else
return n*fact(n-1);
Department of Computer Science & Engineering 19
C PROGRAMMING LAB MANUAL
}
i) AIM: Write a c program to find the factorial of given number using non-recursive function
ALGORITHM:
Step 1: Start.
Step 2: Take variables namely n and f.
Step 3: Read n value
Step 4: f=factorial(n)
Step 5: Take variable fa=1.
Step 6: while(n>=1) then
fa=fa*n
n—
step 7: Return fa value to step 4
f=factorial(n)
fa=1
return fa
Display f n>=1
fa=fa*n
Stop n-- True False
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned long factorial(int);
int n;
unsigned long f;
clrscr();
printf("Enter any n value:");
scanf("%d",&n);
f=factorial(n);
printf("Factorial of given value is:%lu",f);
getch();
}
OUTPUT
ii) AIM: Write a c program to find the GCD (greatest common divisor)of two given integers using
non-recursive functions.
ALGORITHM:
Step 1: Start.
Step 2: Take variables namely a,b and g.
Step 3: Read two variables namely a ,b
Step 4: g=gcd(a,b)
Step 5: Take vraibles namely m, i and gc
Step 6: m=(a>b)?a:b;
Step 7: for i=1 to i<=m then i++
if(a%i==0 and b%i==0) then
gc=i.
step 8: return gc to step 3
step 9: Display g value
Step 10: Stop.
FLOWCHART:
Start
Read a and
b values
g=gcd(a,b)
m=(a>b)?a:b
g = i
Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int gcd(int,int);
int a,b,g;
clrscr();
printf("Enter any two values:");
scanf("%d%d",&a,&b);
g=gcd(a,b);
printf("G.C.D of given two values is:%d",g);
getch();
}
OUTPUT:
Enter any two values: 4 8
G.C.D of given two values is: 4
ii) AIM: Write a c program to find the GCD (greatest common divisor)of two given integers using
recursive functions.
ALGORITHM:
Step 1: Start
Step 2: Take variables namely a,b and g.
Step 3: Read a and b values
Step 4: g=gcd(a,b)
Step 5: Take variables namely m,i=1,g
Step 6: find minimum among a and b
m=(a>b)?a:b
step 7: if(a%i==0&&b%i==0)
g=i
step 8: i++
step 9: if(i<=m)
goto step 4
step 10: return g value to step4
step 11: Display g value
step 12: Stop.
FLOWCHART:
Start
Read a and
b values
g=gcd(a,b)
m=(a>b)?a:b
False True
for(i=1;i<=m;i++)
g = i
Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int gcd(int,int);
int a,b,g;
clrscr();
printf("Enter any two values:");
scanf("%d%d",&a,&b);
g=gcd(a,b);
printf("G.C.D of given two values is:%d",g);
getch();
}
int gcd(int a,int b)
{
static int m,i=1,g;
m=(a<b)?a:b;
if(a%i==0&&b%i==0)
g=i; OUTPUT:
i++; Enter any two values: 4 8
if(i<=m) G.C.D of given two values is: 4
gcd(a,b);
return g;
}
WEEK 8
ALGORITHM:
Step 1: Start.
Step 2: Take variables namely n, i,j,s,ncr.
Step 3: Read n value.
Step 4: s=n*3.
Step 5: for i=0 to i<=n then i++
Display s spaces.
for j=0 to j<=i then j++
ncr=fact(i)/fact(j)*fact(i-j).
Display ncr.
s=s-3.
Display „\n‟
Step 6: Stop.
FLOWCHART:
Start
Read n value
s = n*3
Display s spaces
ncr=fact(i)/fact(j)*fact(i-j)
Display ncr
s=s-3
Display „\n‟
Stop
Department of Computer Science & Engineering 25
C PROGRAMMING LAB MANUAL
PROGRAM:
#include<stdio.h>
#include<conio.h>
unsigned long fact(int x)
{
if(x<=1)
return 1;
else
return x*fact(x-1);
}
void main()
{
int n,i,j,s,ncr;
clrscr();
printf("Enter any n value:");
scanf("%d",&n);
s=n*3;
for(i=0;i<=n;i++)
{
printf("%*c",s,32);
for(j=0;j<=i;j++)
{
ncr=fact(i)/(fact(j)*fact(i-j));
printf("%5d",ncr);
}
s=s-3;
printf("\n");
}
getch();
}
OUTPUT:
ALGORITHM:
Step 1: Start.
Step 2: Take variables namely i, j, m, n,s.
Step 3: Read n value.
Step 4: s=n*4.
Step 5: for i=1 to i<=n then i++
5.1 Display s spaces.
5.2 m=i.
5.3 for j=1 to j<2*i then j++
5.3.1 if(j<i) then
5.3.1.1 Display m++ value.
5.3.2 else then
5.3.2.1 Display m-- value
FLOWCHART:
Start
Read n value
s=n*4
False True
for i=1 to i<=n i++
Display s spaces
m=i
True
if(j<i)
Display „\n‟
s=s-4
Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,m,j,n,s;
clrscr();
printf("Enter any n value:");
scanf("%d",&n);
s= n*4;
for(i=1;i<=n;i++)
{
printf("%*c",s,32);
m=i;
for(j=1;j<2* i;j++)
{
if(j<i)
printf("%4d",m++);
else
printf(“%4d”,m--);
}
printf("\n\n");
s=s-4;
}
getch();
}
OUTPUT:
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
WEEK 9
AIM: Write a C function to read in two numbers, x and n, and then compute the sum of
this geometric progression:
1+x+x2+x3+………….+xn
ALGORITHM:
Step 1: Start
Step 2: Take variables namely n,i,x,sum=0
Step 3: Read n value
Step 4: if(n<0)
Display number of terms must be a positive value
Goto Step
Step 5: Read x value
Step 6: for(i=0;i<=n;i++)
Sum=sum+power(x,i)
Step 7: Display the sum value
Step 8: Stop
FLOWCHART:
Start
sum=0
Read n value
False
if(n<0)
True
n must be
positive
Read x value
for(i=0;i<=n;i++)
Display sum
sum=sum+pow(x,i)
Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i;
float x,sum=0;
clrscr();
lb:
printf("\nEnter number of terms value:");
scanf("%d",&n);
if(n<0)
{
printf("Number of terms value must be positive");
goto lb;
}
printf("Enter any x value:");
scanf("%f",&x);
for(i=0;i<=n;i++)
{
sum=sum+pow(x,i);
}
printf("Sum of the given series is:%.2f",sum);
getch();
}
OUTPUT: