You are on page 1of 31

Regulation – R10 C PROGRAMMING LAB MANUAL

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

Department of Computer Science & Engineering 1


C PROGRAMMING LAB MANUAL

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:

Enter any n value: 783

Sum of the digits of given number is: 18

Department of Computer Science & Engineering 2


C PROGRAMMING LAB MANUAL

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.

Step 2: Take variables namely n, i,t1, t2,t.

Step 3: Initialize variables t1=0 and t2=1.

Step 4: Read n value.

Step 5: Display t1 and t2.

Step 6: for i=1 to i<=n-2 then i++

t=t1+t2.

Display t value.

t1=t2.

t2=t.

Step 7: Stop.

Department of Computer Science & Engineering 3


C PROGRAMMING LAB MANUAL

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

Department of Computer Science & Engineering 4


C PROGRAMMING LAB MANUAL

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:

Enter any n value: 8

Fibonacci series is: 0 1 1 2 3 5 8 13

Department of Computer Science & Engineering 5


C PROGRAMMING LAB MANUAL

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.

Step 2: Take variables namely n,m,i,j,count.

Step 3: Read n value.

Step 4: for i=1 to i<=n then i++

count=0.

for j=1 to j<=i then j++

if(i%j==0)

count++

if(count==2)

Display i value.

Step 5: Stop.

Department of Computer Science & Engineering 6


C PROGRAMMING LAB MANUAL

FLOWCHART:

Start

Read n
value.

i = 1

False True
i <= n

count = 0

i ++
j = 1

True False
j <= i

False True True False


i%j= =0 count= =2

count++
Display
i
Stop

j ++

Department of Computer Science & Engineering 7


C PROGRAMMING LAB MANUAL

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:

Enter any n value: 15

Prime numbers from 1 to 15 are: 2 3 5 7 11 13

Department of Computer Science & Engineering 8


C PROGRAMMING LAB MANUAL

WEEK-2:

a) AIM: Write a C program to calculate the following Sum: Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!

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.

Department of Computer Science & Engineering 9


C PROGRAMMING LAB MANUAL

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

Stop f= x*fact(x-1) return f

p=p+2

i++

Department of Computer Science & Engineering 10


C PROGRAMMING LAB MANUAL

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

Department of Computer Science & Engineering 11


C PROGRAMMING LAB MANUAL

b) AIM:

Write a c program to find the roots of a quadratic equation.

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

Department of Computer Science & Engineering 12


C PROGRAMMING LAB MANUAL

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:

Enter a,b and c values: 1 2 1


First root value is: -1.000000
Second root value is: -1.000000

Department of Computer Science & Engineering 13


C PROGRAMMING LAB MANUAL

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

Department of Computer Science & Engineering 14


C PROGRAMMING LAB MANUAL

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:

Enter initial velocity (u): 5


Enter acceleration (a): 2
Enter traveled time: 3
Distance traveled is: 24.000000

Department of Computer Science & Engineering 15


C PROGRAMMING LAB MANUAL

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 2: Take three variables namely a, b and opt.

Step 3: Read a, b and opt.

Step 4: Switch (opt)

Case „+‟ then write a+b

Case „-„then write a-b

Case „*‟ then write a*b

Case „/‟ then write a/b

Case „%‟ then write a%b

Default then invalid option

Step 5: Stop.

Department of Computer Science & Engineering 16


C PROGRAMMING LAB MANUAL

FLOWCHART:

Start

Read a, b and opt

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

Department of Computer Science & Engineering 17


C PROGRAMMING LAB MANUAL

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:

Enter a and b values: 10 5


Enter any option: +
Addition of given two values is: 15

Department of Computer Science & Engineering 18


C PROGRAMMING LAB MANUAL

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)

return 1 return x*fact(x-1)

Display f n<=1 True

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

Step 8: Display f value


Step 9: Stop Start
FLOWCHART:
Read n

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

Department of Computer Science & Engineering 20


C PROGRAMMING LAB MANUAL

unsigned long factorial(int n)


{
unsigned long fa=1;
while(n>=1)
{
fa=fa*n;
n--;
}
return fa;
}

OUTPUT

Enter any n value:5


Factorial of given value is: 120

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.

Department of Computer Science & Engineering 21


C PROGRAMMING LAB MANUAL

FLOWCHART:

Start

Read a and
b values

g=gcd(a,b)

m=(a>b)?a:b

False for(i=1;i<=m;i++) True

Display False True


if(a%i==0)&&
g value (b%i==0)

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

Department of Computer Science & Engineering 22


C PROGRAMMING LAB MANUAL

int gcd(int a,int b)


{
int i,m,gc;
m=(a<b)?a:b;
for(i=1;i<=m;i++)
{
if(a%i==0&&b%i==0)
gc=i;
}
return gc;
}

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.

Department of Computer Science & Engineering 23


C PROGRAMMING LAB MANUAL

FLOWCHART:
Start

Read a and
b values

g=gcd(a,b)

m=(a>b)?a:b

False True
for(i=1;i<=m;i++)

Display False True


if(a%i==0)&&
g value (b%i==0)

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

Department of Computer Science & Engineering 24


C PROGRAMMING LAB MANUAL

WEEK 8

a) AIM: Write a program to generate Pascal‟s triangle.

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

for i=0 to i<=n ;i++

Display s spaces

for j=0 to j<=i ;j++

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:

Enter any n value:5


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Department of Computer Science & Engineering 26


C PROGRAMMING LAB MANUAL

b) AIM: Write a c program to construct a pyramid of numbers

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

5.4 Display „\n‟.


5.5 s=s-4.
Step 6: Stop.

Department of Computer Science & Engineering 27


C PROGRAMMING LAB MANUAL

FLOWCHART:

Start

Read n value

s=n*4

False True
for i=1 to i<=n i++

Display s spaces

m=i

False for j=1 to j<=i i++

True

if(j<i)

Display m-- Display m++

Display „\n‟

s=s-4

Stop

Department of Computer Science & Engineering 28


C PROGRAMMING LAB MANUAL

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:

Enter any n value:5


1

2 3 2

3 4 5 4 3

4 5 6 7 6 5 4

5 6 7 8 9 8 7 6 5

Department of Computer Science & Engineering 29


C PROGRAMMING LAB MANUAL

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

Department of Computer Science & Engineering 30


C PROGRAMMING LAB MANUAL

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:

Enter number of terms value: 5


Enter any x value: 1
Sum of the given series is: 6.00

Department of Computer Science & Engineering 31

You might also like