2. Coding using Loops – while(),do..while(), for()
Write a program to find the N Factorial value
Algorithm
1. Start program
2. Read N value to find the factorial Value
3. assign fact=1
4. assign count =1
5. open the loop
6. compute count =count +1
7. compute fact = fact * count;
8. repeat the loop till count is <= n is true
9. display the fact value
10. Stop program
3. /*……Factorial value using while()…….*/
#include <stdio.h>
#include <conio.h>
main()
{
int c,n,fact;
printf(“n Enter the N value”;
scanf(“%d”,&n);
c=0; fact =1;
while (c<n)
{
c++;
fact=fact*c;
}
printf(“nThe Factorial Value =%d”,fact);
getch();
}
5! = 1 x 2 x 3 x 4 x 5=120
Output
Enter the N value 5
The Factorial Value 120
Enter the N value 6
The Factorial Value 620
4. /*…Factorial value using do..while()..*/
#include <stdio.h>
#include <conio.h>
main()
{
int c,n,fact;
printf(“n Enter the N value”;
scanf(“%d”,&n);
c=0; fact =1;
do
{
c++;
fact=fact*c;
}
while (c<n);
printf(“nThe Factorial Value =%d”,fact);
getch();
}
5! = 1 x 2 x 3 x 4 x 5=120
Output
Enter the N value 5
The Factorial Value 120
Enter the N value 6
The Factorial Value 620
5. 5! = 1 x 2 x 3 x 4 x 5=120
Output
Enter the N value 5
The Factorial Value 120
Enter the N value 6
The Factorial Value 620
*…Factorial value using for()..*/
#include <stdio.h>
#include <conio.h>
main()
{
int c,n,fact;
printf(“n Enter the N value”;
scanf(“%d”,&n);
c=0; fact =1;
for(c=1;c<=5;c++)
{
fact=fact*c;
}
printf(“nThe Factorial Value =%d”,fact);
getch();
}
6. Write a program to print the following
Tables using for()
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x = 50
Algorithm
In this program
The table value to be printed upto
10 in the given format
1. Open a loop
2. Compute c = I * 5
3. Display result
4. Repeat the loop
5. Stop Program
7. *…Table Printing using for()..*/
#include <stdio.h>
#include <conio.h>
main()
{
int c,I;
for(i=1;c<=10;i++)
{
c=c*5;
printf(“n %d x 5 = %d”,I,c);
}
getch();
}
Output
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x = 50
8. Write a program to print the following
Mth Tables upto N times using for()
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
“”
“”
“”
“”
N X m = nm
Algorithm
In this program
The table value to be printed upto
N times in the given format
1. Read N and M value
2. Open a loop
3. Compute c = I * m
4. Display result
5. Repeat the loop
9. *…Table Printing using for()..*/
#include <stdio.h>
#include <conio.h>
main()
{
int I,m,n,c;
printf(“n Enter the N value:”);
scanf(“%d”,&n);
printf(“n Which table value:”);
scanf(“%d”,&m);
for(i=1;c<=n;i++)
{
c=c*m;
printf(“n %d x %d = %d”,I,m,c);
}
getch();
}
Output
Enter the N value: 5
Which table value: 7
1 x 7 = 7
2 x 7 = 14
3 x 7 = 21
4 x 7 = 28
5 x 7 = 35
10. Write a program to print the following
number series using for()
Output
• 1 2 3 4 5 6 7 8 9 10
• 10 9 8 7 6 5 4 3 2 1
• Odd Numbers: 1 3 5 7 9
• Even Numbers: 0 2 4 6 8 10
12. Write a program to print the following
number series using Nested for()
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Algorithm
In this program The number
sequence 1 2 3 4 5 Should be
printed upto 7 times So
Open two for loops
Loop 1 (Outer loop) for 7 iterations
Loop 2 (inner loop) for
number printing
Loop 2 close
Loop 1 close
Stop Program
14. Write a program to print the following
number series using Nested for()
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Algorithm
In this program The number
sequence 1 2 3 4 5 Should be
printed upto 5 times. Each time
numbers to increased So
Open two for loops
Loop 1 (Outer loop) for 5 iterations
Loop 2 (inner loop) for
number printing
Loop 2 close
Loop 1 close
Stop Program
16. Write a program to print the Fibonacci
number series using for()
1 1 2 3 5 7 12 19 ………..
How to generate this series: We have many logic to print numbers. Here one
algorithm is given
1. Assume a = 1 and b= 1
2. Initially print a and b i.e ( 1 1)
3. open the loop
4. compute c = a + b ( c= 2)
5. print c
6. re-assign a=b => a = 1
7. re-assign b=c => b = 2
8. repeat the loop ( Now c=3…)
9. stop program
17. *…Fibonacci Series….*/
#include <stdio.h>
#include <conio.h>
main()
{
int I,a,b,n;
printf(“n Enter the N value:”);
scanf(“%d”,&n);
a=0;b=1;
printf(“ %d %d”,a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf(“ %d “,c);
a=b;
b=c;
}
}
getch();
}
Output
Enter the N value:10
1 1 2 3 5 8 13 21 34 55
18. Write a program to print the following
numbers series
1 -2 3 -4 5 -6 7 -8…….+/-n
Here lternate Number should be +ve and –ve
Algorithm
1. open a loop
2. if the I value is odd I,e +ve else –ve
3. find even number using % (modelus) division
and multiply with -1
1. display numbers
2. repeat the loop
3. stop program
19. *…+ve and –ve Series….*/
#include <stdio.h>
#include <conio.h>
main()
{
int i;
printf(“n Enter the N value:”);
scanf(“%d”,&n);
Printf(“n The Result is: n”);
for(i=1;i<=n;i++)
{
if(i%2==0)
{
i = i*(-1);
}
printf(“ %d “,i);
}
getch();
}
Output
Enter the N value: 10
The Result is:
1 -2 3 -4 5 -6 7 -8 9 -10
22. 2. Which of the following iterative loop
1. while()
2. do..while()
3. for()
4. break
23. 3.Which of the following loop checks
first then execute the statement
1. while()
2. do.. While()
3. for()
4. break;
24. 4.Which of the following statement is True
1. while(exp);
{ }
2. do
{ } while;
3. for(initial;exp;inc/dec)
{ }
4 All the above are true
25. 5.What is the output of the coding
i=0;k=1
do
{
i++;
printf(“%d”,i);
}
While(k<=5);
1. 1 2 3 4
2. 1 2 3 4 5
3. 1 2 3 4 5 6
4. 1 2 3 4 5 6 ……….
5. None of the above
26. 6.What is the output of the coding
i=0;
While(i<10)
{
i++;
if(i==5)
continue;
printf(“%d”,i);
}
1. 1 2 3 4 5
2. 1 2 3 4 5 6 7 8 9
3. 1 2 3 4 5 6 7 8 9 10
4. 1 2 3 4 6 7 8 9 10……….
5. None of the above
27. 7.What is the output of the coding
i=0;
While(i<10)
{
i++;
if(i==5)
continue;
printf(“%d”,i);
if(i==7)
break;
}
1. 1 2 3 4 5 6 7
2. 1 2 3 4 5 6 7 8 9
3. 1 2 3 4 6 7
4. 1 2 3 4 6 7 8 9 10
5. None of the above
28. 8.What is the output of the coding
i=0;
for(i=0;i<=5;i++)
{
printf(“%d”,i);
}
1. 0 1 2 3 4 5
2. 0 1 2 3 4
3. 1 2 3 4 5
4. None of the above
29. 9.What is the output of the coding
Int I,j
For(i=0;i<3;i++)
{
for(j=I;j<3;j++)
{
printf(“%d “,j);
}
}
1. 0 1 2 0 1 0
2. 0 1 2 0 1 2
3. 1 2 3 0 1 2
4. None of the above
30. Which of the following statement is
not true
• Each program must have one loop
• More than one loop / Nested loop can be
given
• Do..while() must ended with ;
• Exit and entry loop can have unconditional
statement
• None of the above