SlideShare uma empresa Scribd logo
1 de 31
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
?

Mais conteúdo relacionado

Semelhante a C-LOOP-Session-2.pptx

B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programsPrasadu Peddi
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentalsZaibi Gondal
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2Zaibi Gondal
 
Simple C programs
Simple C programsSimple C programs
Simple C programsab11cs001
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)Ashishchinu
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solutionKuntal Bhowmick
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)Make Mannan
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans incnayakq
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 SolutionHazrat Bilal
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
C Programming Lab.pdf
C Programming Lab.pdfC Programming Lab.pdf
C Programming Lab.pdfMOJO89
 

Semelhante a C-LOOP-Session-2.pptx (20)

B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
C file
C fileC file
C file
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
Simple C programs
Simple C programsSimple C programs
Simple C programs
 
C programs
C programsC programs
C programs
 
Progr3
Progr3Progr3
Progr3
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
 
c programing
c programingc programing
c programing
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans inc
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
Lab 2
Lab 2Lab 2
Lab 2
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
Programming egs
Programming egs Programming egs
Programming egs
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
C Programming Lab.pdf
C Programming Lab.pdfC Programming Lab.pdf
C Programming Lab.pdf
 

Último

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 

Último (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

C-LOOP-Session-2.pptx

  • 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
  • 11. *…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
  • 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
  • 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 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
  • 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 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
  • 20. MCQ
  • 21. 1. Choose odd one 1. while() 2. do..while() 3. for() 4. Goto
  • 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