LOOPING STATEMENT
PART - II
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
/*……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
/*…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! = 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();
}
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
*…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
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
*…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
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
*…Number Series Printing for()….*/
#include <stdio.h>
#include <conio.h>
main()
{
int c,n=10;
for(c=1;c<n;c++)
printf(“ %d ”,c);
printf(“n”);
for(c=n;c>=1;c--)
printf(“ %d ”,c);
printf(“nOdd Numbers:”);
for(c=1;c<10;c=c+2)
printf(“ %d ”,c);
printf(“nOEven Numbers:”);
for(c=0;c<10;c=c+2)
printf(“ %d ”,c);
getch()
}
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
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
*…Nested Loop - for()….*/
#include <stdio.h>
#include <conio.h>
main()
{
int o,i;
for(o=1;o<=7;o++)
{
for(i=1;i<=5;i++)
{
printf(“ %d “,i);
}
printf(“n”);
}
getch();
}
Where
o – variable for outer loop
I – Variable for inner loop
Output
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
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
*…Nested Loop - for()….*/
#include <stdio.h>
#include <conio.h>
main()
{
int I,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“ %d “,j);
}
printf(“n”);
}
getch()
}
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
In this program if printf()
printf(“ %d ”,i)
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
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
*…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
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
*…+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
MCQ
1. Choose odd one
1. while()
2. do..while()
3. for()
4. Goto
2. Which of the following iterative loop
1. while()
2. do..while()
3. for()
4. break
3.Which of the following loop checks
first then execute the statement
1. while()
2. do.. While()
3. for()
4. break;
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
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
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
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
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
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
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
QUESTIONS
?

C-LOOP-Session-2.pptx

  • 1.
  • 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 usingwhile()…….*/ #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 usingdo..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! = 1x 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 programto 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 usingfor()..*/ #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 programto 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 usingfor()..*/ #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 programto 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
  • 11.
    *…Number Series Printingfor()….*/ #include <stdio.h> #include <conio.h> main() { int c,n=10; for(c=1;c<n;c++) printf(“ %d ”,c); printf(“n”); for(c=n;c>=1;c--) printf(“ %d ”,c); printf(“nOdd Numbers:”); for(c=1;c<10;c=c+2) printf(“ %d ”,c); printf(“nOEven Numbers:”); for(c=0;c<10;c=c+2) printf(“ %d ”,c); getch() } 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 programto 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
  • 13.
    *…Nested Loop -for()….*/ #include <stdio.h> #include <conio.h> main() { int o,i; for(o=1;o<=7;o++) { for(i=1;i<=5;i++) { printf(“ %d “,i); } printf(“n”); } getch(); } Where o – variable for outer loop I – Variable for inner loop Output 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
  • 14.
    Write a programto 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
  • 15.
    *…Nested Loop -for()….*/ #include <stdio.h> #include <conio.h> main() { int I,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf(“ %d “,j); } printf(“n”); } getch() } Output 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 In this program if printf() printf(“ %d ”,i) 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
  • 16.
    Write a programto 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 programto 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 –veSeries….*/ #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
  • 20.
  • 21.
    1. Choose oddone 1. while() 2. do..while() 3. for() 4. Goto
  • 22.
    2. Which ofthe following iterative loop 1. while() 2. do..while() 3. for() 4. break
  • 23.
    3.Which of thefollowing loop checks first then execute the statement 1. while() 2. do.. While() 3. for() 4. break;
  • 24.
    4.Which of thefollowing 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 theoutput 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 theoutput 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 theoutput 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 theoutput 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 theoutput 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 thefollowing 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
  • 31.