SlideShare a Scribd company logo
1 of 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
?

More Related Content

Similar to 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
 

Similar to 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
 

Recently uploaded

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
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
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
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
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
 

Recently uploaded (20)

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.
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
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...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
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