Professional Documents
Culture Documents
Objective : 1
To find the sum of individual digits of a given number
Description:
Sum of the individual digits means adding all the digits of a number
Ex: 123
sum of digits is 1+2+3=6
Algorithm:
Step
Step
Step
Step
Step
Step
1: start
2: read n
3: initialize the s=O
4: if n<O goto Step 7
5: if n!=O goto Step 6 else goto step 7
6: store n%10 value in p
Add p value to s
Assign n/10 value ton
Goto Step 5
Step 7: print the output
Step 8:stop
Flowchart:
START
S=O
TRUE
FALSE
P=N%10
S=S+P
N=N/10
Program:
#include<stdio.h>
maim)
{
int n,s,p;
clrscrt);
printf("enter the vaue for n:\n");
scanf("%d" ,&n);
s=O;
if(n<O)
printf("The given number is not valid");
else
{
while(n!=O)
/* check the given value =O or not */
p=n%10;
n=n/10;
s=s+p;
}
printf("sum of individual digits is %d",s);
}
getch();
}
Ouqrnt:
l .Enter the value for n: 333
Sum of individual digits is 9
2.Enter the value for n: 4733
Sum of individual digits is 17
3. Enter the value for n: -111
The given number is not valid
Conclusion : The program is error free
Objective:2
To print the Fibonacci series for 1 to n value
Description
A fibonacci series is defined as follows
The first term in the sequence is O
The second term in the sequence is 1
The sub sequent terms 1 found by adding the preceding two terms in the sequence
Formula: let tl,t2,
to be terms in fibinacci sequence
tl=O, t2=1
tn=tn-2+tn-1
where n>2
ali:orithm:
Step 1: start
Step 2: initialize the a=O, b=l
Step 3: read n
Step 4: if n= 1 print a go to step 7. else goto step 5
Step 5: ifn= 2 print a, b go to step 7 else print a,b
Step 6: initialize i=3
i)
if i<= n do as follows. If not goto step 7
c=a+b
print c
a=b
b=c
increment I value
goto step 6(i)
Step 7: stop
Flowchart:
Gp
A=O,b=l
Readn
False
True
False
Output a
Output a,b
False
1=2
i-++
l<n
C = a+b
Output
Stop
A=b
B=c
Program:
#include<stdio.h>
void main()
{
int a,b,c,n,i;
clrscr();
printf("enter n value");
scanf("%d" ,&n);
a=O;
b=l;
if(n=l)
printf("%d" ,a);
else if(n=2)
printf("%d%d" ,a,b );
else
{
printf("%d%d" ,a,b );
//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS
PRINTED IN ADVANCE
for(i=2;i<n;i++)
{
c=a+b;
printf("%d",c);
a=b;
b=c;
}
getch();
}
}
Output:
1. Enter n value : 5
01 1 2 3
2. Enter n value : 7
1 1 2 3 5 8
3. Enter n value : -6
VIVA OUESATIONS:
ll_ What is Fibonacci series ?
Ans: A fibonacci series is defined as follows
The first term in the sequence is O
The second term in the sequence is 1
The sub sequent terms 1 found by adding the preceding two terms in the sequence
Formula: let tl,t2,
tn be terms in fibinacci sequence
tl=O, t2=1
tn=tn-2+tn-1
where n>2
2) What are the various types of unconditional statements?
Ans: goto,Break and continue
3)What are the various types of conditional statements?
Ans: if, if else ,switch statements
4) Expand <STDIO.H >?
Ans: standard input output header file
Objective :3
To print a prime numbers up to 1 to n
Description:
Prime number is a number which is exactly divisible by one and itself only
Ex: 2, 3,5,7,
;
Ali:orithm:
Step 1: start
Step 2: read n
Step 3: initialize i=l,c=O
Step 4:if i<=n goto step 5
If not goto step 10
Step 5: initialize j=l
Step 6: if j<=l do as the follow. If no goto step 7
i)if i%j=O increment c
ii) increment j
iii) goto Step 6
Step 7: if c= 2 print i
Step 8: increment i
Step 9: goto step 4
Step 10: stop
Flow chart:
Start
Readn
l<=n
I= 1
false
I++
false
= 1
J<=i
false
Output
fact
stop
J++
l
Fact++
true
10
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,factJ;
clrscr();
printf("enter the number:");
scanf("%d" ,&n);
for(i= 1 ;i<=n;i++)
{
fact=O;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
for(j= I ;j<=i;j++)
{
if(i%j=O)
fact++;
}
if(fact=2)
printf("\n %d",i);
}
getch( );
}
Output:
Enter the number : 5
2 3 5
Enter the number : IO
2 3 5 7
Enter the number : 12
2 3 5 711
Conclusion : The program is error free
VIVA OUESA TIONS:
1)_ What is prime number ?
Ans: Prime number is a number which is exactly divisible by one and itself only
2)What is an algorithm?
Ans : A step by step procedure is called algorithm
3)What is flow chart?
Ans: A pictorial representation an algorithm is called a flow chart
4)What is program?
Ans : A collection of statements is called
Objective:4
To find the roots of the quadratic equation
Description:
Nature of roots of quadratic equation can be known from the quadrant 6 = b2-4ac
If b2-4ac >O then roots are real and unequal
If b2 -4ac =O then roots are real and equal
If b2 -4ac <O then roots are imaginary
Algorithm:
Step 1: start
Step 2: read the a,b,c value
Step 3: ifb*b-4ac>O then
Root 1= (-b+ pow((b*b-4*a*c),0.5))/2*a
Root 2= (-b-pow((b*b-4*a*c),0.5))/2*a
Step 4: if b*b-4ac=O then
Rootl = Root2 = -b/(2*a)
Step 5: Otherwise Print Imaginary roots. Goto step 7.
Step 6: print roots
Step 7: stop
Flowchart:
Start
Read a,b,c
D = pow(b*b-4*a*c),0.5
false
Rl
Rl =-b I (2
* a)
R2= -b I (2
* a)
((-b+D) I (2*a))
R2 = ((-b-D) /(2*a))
Output
Rl,R2
Imaginary
roots
Stop
Program:
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,rl ,r2,d;
clrscr();
printf("Enter the values for equation:");
scanf("%f:O/of%it'' ,&a,&b,&c );
/* check the condition */
if(a=O)
printf("Enter value should not be zero ");
else
d=b*b-4*a*c;
I* check the condition * I
if(d>O)
rl =(-b+sqrt(d)/(2*a));
{
r2=(-b-sqrt( d)/(2*a));
printf("roots are real and unequal\n");
printf("%f\n%f\n" ,r 1,r2);
}
else
if(d=O)
{
}
else
}
rl=-b/(2*a);
r2=-b/(2*a);
printf("roots are real and equal\n");
printf("root=%f\n",rl); printf("root=
%f\n" ,r2);
printf("roots are imaginary");
getch();
}
Output:
1. Enter the values for equation: 1, 6, 9
Roots are real and equal
Root= -3.0000
Root= -3.0000