SlideShare a Scribd company logo
1 of 93
Structured Programming Language
Iteration
Mohammad Imam Hossain,
Lecturer, CSE, UIU
• for
• while
• do-while
• break
• continue
Why Looping???
Write “I will
not talk” 100
times
Why Looping???
What will you do?
Write printf(“I will not talkn”); statement 100 times!!!!!!
Write “I will
not talk” 100
times
Why Looping???
What will you do?
Write printf(“I will not talkn”); statement 100 times!!!!!!
Write “I will
not talk” 100
times
L
O
O
P
Looping Statements
• Loops are used for performing repetitive tasks.
• A loop statement allows us to execute a statement or
group of statements multiple times.
• Mainly 3 types of looping statement:
1. while loop
2. for loop
3. do-while loop
Flow Chart
Initialize counter
variables
Condition
Checking
Body of loop
(repetitive part)
Update counter
variables
Exit from loop
True
False
Syntax of for Loop
for( expression 1 ; expression 2 ; expression 3 )
{
/// body of loop
}
• expression 1 is used to initialize some parameter that
controls the looping action
• expression 2 represents a condition that must be true for
the loop to continue execution
• expression 3 is used to alter the value of the parameter
initially assigned by expression 1
Syntax of for Loop
for( expression 1 ; expression 2 ; expression 3 )
{
/// body of loop
}
• expression 1 is used to initialize some parameter that
controls the looping action
• expression 2 represents a condition that must be true for
the loop to continue execution
• expression 3 is used to alter the value of the parameter
initially assigned by expression 1
Assignment expression
Logical expression
Unary/assignment
expression
Control Flow of for Loop
for( initialization ; condition checking ; increment/decrement )
{
/// body of loop
}
/// statement/s after for loop
Control Flow of for Loop
for( initialization ; condition checking ; increment/decrement )
{
/// body of loop
}
/// statement/s after for loop
Control Flow of for Loop
for( initialization ; condition checking ; increment/decrement )
{
/// body of loop
}
/// statement/s after for loop
Control Flow of for Loop
for( initialization ; condition checking ; increment/decrement )
{
/// body of loop
}
/// statement/s after for loop
False
Control Flow of for Loop
for( initialization ; condition checking ; increment/decrement )
{
/// body of loop
}
/// statement/s after for loop
True
False
Control Flow of for Loop
for( initialization ; condition checking ; increment/decrement )
{
/// body of loop
}
/// statement/s after for loop
True
False
Control Flow of for Loop
for( initialization ; condition checking ; increment/decrement )
{
/// body of loop
}
/// statement/s after for loop
True
False
Control Flow of for Loop
for( initialization ; condition checking ; increment/decrement )
{
/// body of loop
}
/// statement/s after for loop
True
False
Control Flow of for Loop
for( initialization ; condition checking ; increment/decrement )
{
/// body of loop
}
/// statement/s after for loop
True
False
Problem
• Write a program to show the numbers from 1 to 100
Solution
#include <stdio.h>
int main()
{
return 0;
}
Solution
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++){
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++){
printf("%d ",i);
}
printf("nExitn");
return 0;
}
for vs while Loop
for(expression 1;expression 2; expression 3)
{
///body of loop
}
expression 1;
while(expression 2){
///body of loop
expression 3;
}
Control Flow of while Loop
initialization;
while(condition check)
{
///body of loop
increment/decrement;
}
///statement/s after while loop
Control Flow of while Loop
initialization;
while(condition check)
{
///body of loop
increment/decrement;
}
///statement/s after while loop
Control Flow of while Loop
initialization;
while(condition check)
{
///body of loop
increment/decrement;
}
///statement/s after while loop
Control Flow of while Loop
initialization;
while(condition check)
{
///body of loop
increment/decrement;
}
///statement/s after while loop
False
Control Flow of while Loop
initialization;
while(condition check)
{
///body of loop
increment/decrement;
}
///statement/s after while loop
True
False
Control Flow of while Loop
initialization;
while(condition check)
{
///body of loop
increment/decrement;
}
///statement/s after while loop
True
False
Control Flow of while Loop
initialization;
while(condition check)
{
///body of loop
increment/decrement;
}
///statement/s after while loop
True
False
Control Flow of while Loop
initialization;
while(condition check)
{
///body of loop
increment/decrement;
}
///statement/s after while loop
True
False
Control Flow of while Loop
initialization;
while(condition check)
{
///body of loop
increment/decrement;
}
///statement/s after while loop
True
False
Problem
• Write a program to show the even numbers from 0
to 100
Solution#include <stdio.h>
int main()
{
return 0;
}
Solution#include <stdio.h>
int main()
{
int i=0;
while(i<=100){
}
return 0;
}
Solution#include <stdio.h>
int main()
{
int i=0;
while(i<=100){
printf("%d ",i);
}
return 0;
}
Solution#include <stdio.h>
int main()
{
int i=0;
while(i<=100){
printf("%d ",i);
i+=2;
}
return 0;
}
Solution#include <stdio.h>
int main()
{
int i=0;
while(i<=100){
printf("%d ",i);
i+=2;
}
printf("nDonen");
return 0;
}
for vs while vs do-while Loop
for(expression 1;expression 2; expression 3)
{
///body of loop
}
expression 1;
while(expression 2){
///body of loop
expression 3;
}
expression 1;
do{
///body of loop
expression 3;
}while(expression 2);
Control Flow of do-while Loop
initialization;
do{
///body of loop
increment/decrement;
}while(condition check);
///statement/s after do-while loop
Control Flow of do-while Loop
initialization;
do{
///body of loop
increment/decrement;
}while(condition check);
///statement/s after do-while loop
Control Flow of do-while Loop
initialization;
do{
///body of loop
increment/decrement;
}while(condition check);
///statement/s after do-while loop
Control Flow of do-while Loop
initialization;
do{
///body of loop
increment/decrement;
}while(condition check);
///statement/s after do-while loop
Control Flow of do-while Loop
initialization;
do{
///body of loop
increment/decrement;
}while(condition check);
///statement/s after do-while loop
Control Flow of do-while Loop
initialization;
do{
///body of loop
increment/decrement;
}while(condition check);
///statement/s after do-while loop
False
Control Flow of do-while Loop
initialization;
do{
///body of loop
increment/decrement;
}while(condition check);
///statement/s after do-while loop
False
True
Problem
• Write a program that will continuously take input a
number from the user until the number is less than
or equal to zero.
Solution#include <stdio.h>
int main()
{
int num;
do{
printf("Enter the number: ");
scanf("%d",&num);
printf("You entered %dn",num);
}while(num>0);
printf("nExitn");
return 0;
}
Problem
• Write a program to show the numbers from 100 to 1.
• Write a program to show the odd numbers from 1 to
100
• Write a program to show the even numbers from 100
to 1
• Write a program to show the multiplication table of n
where the range of multiplication is from 1 to 10.
Problem
• Write a program to show the following series
0, 1, 0, 1, 0, 1, 0, 1, … … … upto n th term
• Write a program to show the following series
1, 3, 6, 10, 15, … … … upto n th term
• Write a program to show the following series
1, 1, 2, 3, 5, 8, … … … upto n th term
Problem
• Write a program to calculate the sum of 1st n natural
numbers
i.e.
1+2+3+4+… … … +n
Solution
#include <stdio.h>
int main()
{
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
int i;
for(i=1;i<=n;i++){
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
int sum=0;
int i;
for(i=1;i<=n;i++){
sum=sum+i;
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
int sum=0;
int i;
for(i=1;i<=n;i++){
sum=sum+i;
}
printf("The result is %dn",sum);
return 0;
}
Problems
• Write a program to calculate the sum of the following
series i.e.
2+4+6+… … +2n
• Write a program to calculate the sum of squares of 1st n
natural numbers
i.e.
12 +22+32+42+… … … +n2
• Write a program to calculate the sum of cubes of 1st n
natural numbers
i.e.
13 +23+33+43+… … … +n3
Problems
• Write a program to calculate the sum of the
following series i.e.
3+32+33+34+… … … +3n
• Write a program to calculate the sum of the
following series i.e.
1
3
+
1
32 +
1
33 +
1
34 + ⋯ ⋯ +
1
3 𝑛
Problem
• Write a program to calculate the average of a list of n
numbers.
Solution
#include <stdio.h>
int main()
{
return 0;
}
Solution
#include <stdio.h>
int main()
{
int list_size;
printf("Enter the list size : ");
scanf("%d",&list_size);
return 0;
}
Solution
#include <stdio.h>
int main()
{
int list_size;
printf("Enter the list size : ");
scanf("%d",&list_size);
int count;
for(count=1;count<=list_size;count++){
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int list_size;
printf("Enter the list size : ");
scanf("%d",&list_size);
float sum=0;
float number;
int count;
for(count=1;count<=list_size;count++){
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int list_size;
printf("Enter the list size : ");
scanf("%d",&list_size);
float sum=0;
float number;
int count;
for(count=1;count<=list_size;count++){
printf("Enter the %d th number: ",count);
scanf("%f",&number);
sum=sum+number;
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int list_size;
printf("Enter the list size : ");
scanf("%d",&list_size);
float sum=0;
float number;
float average;
int count;
for(count=1;count<=list_size;count++){
printf("Enter the %d th number: ",count);
scanf("%f",&number);
sum=sum+number;
}
average=sum/list_size;
return 0;
}
Solution
#include <stdio.h>
int main()
{
int list_size;
printf("Enter the list size : ");
scanf("%d",&list_size);
float sum=0;
float number;
float average;
int count;
for(count=1;count<=list_size;count++){
printf("Enter the %d th number: ",count);
scanf("%f",&number);
sum=sum+number;
}
average=sum/list_size;
printf("The average of inputted numbers is %.2fn",average);
return 0;
}
Problem
• Write a program to calculate the factorial of n.
Input: n(an integer)
Output: n!
We know that,
n!= 1*2*3*… … … … *n
Solution
#include <stdio.h>
int main()
{
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
if(n<0) printf("Invalidn");
else if(n==0) printf("%d ! = %d n",n,1);
else{
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
if(n<0) printf("Invalidn");
else if(n==0) printf("%d ! = %d n",n,1);
else{
unsigned long int prod=1;
int i;
for(i=1;i<=n;i++){
}
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
if(n<0) printf("Invalidn");
else if(n==0) printf("%d ! = %d n",n,1);
else{
unsigned long int prod=1;
int i;
for(i=1;i<=n;i++){
prod=prod*i;
}
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
if(n<0) printf("Invalidn");
else if(n==0) printf("%d ! = %d n",n,1);
else{
unsigned long int prod=1;
int i;
for(i=1;i<=n;i++){
prod=prod*i;
}
printf("%d ! = %lu n",n,prod);
}
return 0;
}
Problem
• Write a program to calculate nPr where r<=n and both
r , n are positive.
Input: n,r
Output: nPr
We know that,
nPr = n *(n-1)*(n-2)*… … … *(n-r+1)
ex. 5P3= 5.4.3=60
Solution
#include <stdio.h>
int main()
{
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n,r;
printf("Enter the value of n and r: ");
scanf("%d %d",&n,&r);
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n,r;
printf("Enter the value of n and r: ");
scanf("%d %d",&n,&r);
if(n<0 || r<0 || r>n) printf("Invalidn");
else{
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n,r;
printf("Enter the value of n and r: ");
scanf("%d %d",&n,&r);
if(n<0 || r<0 || r>n) printf("Invalidn");
else{
int i;
for(i=n;i>=(n-r+1);i--){
}
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n,r;
printf("Enter the value of n and r: ");
scanf("%d %d",&n,&r);
if(n<0 || r<0 || r>n) printf("Invalidn");
else{
int prod=1;
int i;
for(i=n;i>=(n-r+1);i--){
prod=prod*i;
}
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int n,r;
printf("Enter the value of n and r: ");
scanf("%d %d",&n,&r);
if(n<0 || r<0 || r>n) printf("Invalidn");
else{
int prod=1;
int i;
for(i=n;i>=(n-r+1);i--){
prod=prod*i;
}
printf("%dP%d = %dn",n,r,prod);
}
return 0;
}
Problem
• Write a program to calculate nCr where r<=n and both
r , n are positive.
Input: n,r
Output: nCr
We know that,
nCr
=
nPr
r!
= n ∗(n−1)∗(n−2)∗… … … ∗(n−r+1)
1∗2∗3∗⋯ ⋯∗𝑟
ex. 5C3=
5.4.3
1.2.3
=10
continue Statement
• The continue statement is used to bypass the
remainder of the current pass through a loop.
• The loop does not terminate when a continue
statement is encountered.
• The remaining loop statements are skipped and the
computation proceeds directly to the next pass
through the loop.
• Syntax:
continue;
Problem
• Write a program that will show all the numbers from
1 to 100 except the numbers that are divisible by 5 or
7.
Solution
#include <stdio.h>
int main()
{
return 0;
}
Solution
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++){
}
printf("nDone");
return 0;
}
Solution
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++){
if(i%5==0) continue;
}
printf("nDone");
return 0;
}
Solution
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++){
if(i%5==0) continue;
else if(i%7==0) continue;
}
printf("nDone");
return 0;
}
Solution
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++){
if(i%5==0) continue;
else if(i%7==0) continue;
printf("%d ",i);
}
printf("nDone");
return 0;
}
Output?????
#include <stdio.h>
int main()
{
float x;
do{
scanf("%f",&x);
if(x<0){
printf("Error Negative value for xn");
continue;
}
}while(x<=100);
return 0;
}
break Statement
• The break statement is used to terminate loops or to
exit from a switch statement.
• Syntax:
break;
Guessing Game Problem
• Initially your program will guess some fixed number.
• Then your program will prompt the user to guess
that number.
• If the user can’t guess the number then notify
him(too small or too big).
• If the user guess the number accurately then show a
message containing no of attempts he has made to
guess that predetermined number.
Solution#include <stdio.h>
int main()
{
int pre_value=100;
int attempts_count=0;
int input;
do{
printf("Enter a number (range 1 to 200): ");
scanf("%d",&input);
attempts_count++;
if(input==pre_value){
printf("You won with %d attemptsn",attempts_count);
break;
}
else if(input<pre_value) printf("too smalln");
else printf("too largen");
}while(1);
return 0;
}
Problems to Practice
• Write a program to show all the factors of a number.
• Write a program to check a number is prime or not.
• Write a program to check a number is perfect or not.
• Write a program to find out the gcd(Greatest
Common Divisor) between two number.
• Write a program to find out the lcm(Least Common
Multiplier) between two number.
Problems to Practice
• Write a program to count the number of digits of a
given number.
• Write a program to convert a decimal number to
binary (using only loop).

More Related Content

What's hot

8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointersMomenMostafa
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)yap_raiza
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
C Programming by Süleyman Kondakci
C Programming by Süleyman KondakciC Programming by Süleyman Kondakci
C Programming by Süleyman KondakciSüleyman Kondakci
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programsPrasadu Peddi
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
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 .
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in CJeya Lakshmi
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 

What's hot (19)

C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
C programms
C programmsC programms
C programms
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
For Loop
For LoopFor Loop
For Loop
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
3. control statement
3. control statement3. control statement
3. control statement
 
C Programming by Süleyman Kondakci
C Programming by Süleyman KondakciC Programming by Süleyman Kondakci
C Programming by Süleyman Kondakci
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
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 ...
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 

Similar to Looping Statements in C

Similar to Looping Statements in C (20)

Control structure of c
Control structure of cControl structure of c
Control structure of c
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
Looping
LoopingLooping
Looping
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
 
Bsit1
Bsit1Bsit1
Bsit1
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
Loops
LoopsLoops
Loops
 
Loops in c
Loops in cLoops in c
Loops in c
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Loops c++
Loops c++Loops c++
Loops c++
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
Code optimization
Code optimization Code optimization
Code optimization
 

More from Mohammad Imam Hossain

DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchMohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionMohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaMohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaMohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckMohammad Imam Hossain
 

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 

Recently uploaded

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 

Recently uploaded (20)

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 

Looping Statements in C

  • 1. Structured Programming Language Iteration Mohammad Imam Hossain, Lecturer, CSE, UIU • for • while • do-while • break • continue
  • 2. Why Looping??? Write “I will not talk” 100 times
  • 3. Why Looping??? What will you do? Write printf(“I will not talkn”); statement 100 times!!!!!! Write “I will not talk” 100 times
  • 4. Why Looping??? What will you do? Write printf(“I will not talkn”); statement 100 times!!!!!! Write “I will not talk” 100 times L O O P
  • 5. Looping Statements • Loops are used for performing repetitive tasks. • A loop statement allows us to execute a statement or group of statements multiple times. • Mainly 3 types of looping statement: 1. while loop 2. for loop 3. do-while loop
  • 6. Flow Chart Initialize counter variables Condition Checking Body of loop (repetitive part) Update counter variables Exit from loop True False
  • 7. Syntax of for Loop for( expression 1 ; expression 2 ; expression 3 ) { /// body of loop } • expression 1 is used to initialize some parameter that controls the looping action • expression 2 represents a condition that must be true for the loop to continue execution • expression 3 is used to alter the value of the parameter initially assigned by expression 1
  • 8. Syntax of for Loop for( expression 1 ; expression 2 ; expression 3 ) { /// body of loop } • expression 1 is used to initialize some parameter that controls the looping action • expression 2 represents a condition that must be true for the loop to continue execution • expression 3 is used to alter the value of the parameter initially assigned by expression 1 Assignment expression Logical expression Unary/assignment expression
  • 9. Control Flow of for Loop for( initialization ; condition checking ; increment/decrement ) { /// body of loop } /// statement/s after for loop
  • 10. Control Flow of for Loop for( initialization ; condition checking ; increment/decrement ) { /// body of loop } /// statement/s after for loop
  • 11. Control Flow of for Loop for( initialization ; condition checking ; increment/decrement ) { /// body of loop } /// statement/s after for loop
  • 12. Control Flow of for Loop for( initialization ; condition checking ; increment/decrement ) { /// body of loop } /// statement/s after for loop False
  • 13. Control Flow of for Loop for( initialization ; condition checking ; increment/decrement ) { /// body of loop } /// statement/s after for loop True False
  • 14. Control Flow of for Loop for( initialization ; condition checking ; increment/decrement ) { /// body of loop } /// statement/s after for loop True False
  • 15. Control Flow of for Loop for( initialization ; condition checking ; increment/decrement ) { /// body of loop } /// statement/s after for loop True False
  • 16. Control Flow of for Loop for( initialization ; condition checking ; increment/decrement ) { /// body of loop } /// statement/s after for loop True False
  • 17. Control Flow of for Loop for( initialization ; condition checking ; increment/decrement ) { /// body of loop } /// statement/s after for loop True False
  • 18. Problem • Write a program to show the numbers from 1 to 100
  • 20. Solution #include <stdio.h> int main() { int i; for(i=1;i<=100;i++){ } return 0; }
  • 21. Solution #include <stdio.h> int main() { int i; for(i=1;i<=100;i++){ printf("%d ",i); } printf("nExitn"); return 0; }
  • 22. for vs while Loop for(expression 1;expression 2; expression 3) { ///body of loop } expression 1; while(expression 2){ ///body of loop expression 3; }
  • 23. Control Flow of while Loop initialization; while(condition check) { ///body of loop increment/decrement; } ///statement/s after while loop
  • 24. Control Flow of while Loop initialization; while(condition check) { ///body of loop increment/decrement; } ///statement/s after while loop
  • 25. Control Flow of while Loop initialization; while(condition check) { ///body of loop increment/decrement; } ///statement/s after while loop
  • 26. Control Flow of while Loop initialization; while(condition check) { ///body of loop increment/decrement; } ///statement/s after while loop False
  • 27. Control Flow of while Loop initialization; while(condition check) { ///body of loop increment/decrement; } ///statement/s after while loop True False
  • 28. Control Flow of while Loop initialization; while(condition check) { ///body of loop increment/decrement; } ///statement/s after while loop True False
  • 29. Control Flow of while Loop initialization; while(condition check) { ///body of loop increment/decrement; } ///statement/s after while loop True False
  • 30. Control Flow of while Loop initialization; while(condition check) { ///body of loop increment/decrement; } ///statement/s after while loop True False
  • 31. Control Flow of while Loop initialization; while(condition check) { ///body of loop increment/decrement; } ///statement/s after while loop True False
  • 32. Problem • Write a program to show the even numbers from 0 to 100
  • 34. Solution#include <stdio.h> int main() { int i=0; while(i<=100){ } return 0; }
  • 35. Solution#include <stdio.h> int main() { int i=0; while(i<=100){ printf("%d ",i); } return 0; }
  • 36. Solution#include <stdio.h> int main() { int i=0; while(i<=100){ printf("%d ",i); i+=2; } return 0; }
  • 37. Solution#include <stdio.h> int main() { int i=0; while(i<=100){ printf("%d ",i); i+=2; } printf("nDonen"); return 0; }
  • 38. for vs while vs do-while Loop for(expression 1;expression 2; expression 3) { ///body of loop } expression 1; while(expression 2){ ///body of loop expression 3; } expression 1; do{ ///body of loop expression 3; }while(expression 2);
  • 39. Control Flow of do-while Loop initialization; do{ ///body of loop increment/decrement; }while(condition check); ///statement/s after do-while loop
  • 40. Control Flow of do-while Loop initialization; do{ ///body of loop increment/decrement; }while(condition check); ///statement/s after do-while loop
  • 41. Control Flow of do-while Loop initialization; do{ ///body of loop increment/decrement; }while(condition check); ///statement/s after do-while loop
  • 42. Control Flow of do-while Loop initialization; do{ ///body of loop increment/decrement; }while(condition check); ///statement/s after do-while loop
  • 43. Control Flow of do-while Loop initialization; do{ ///body of loop increment/decrement; }while(condition check); ///statement/s after do-while loop
  • 44. Control Flow of do-while Loop initialization; do{ ///body of loop increment/decrement; }while(condition check); ///statement/s after do-while loop False
  • 45. Control Flow of do-while Loop initialization; do{ ///body of loop increment/decrement; }while(condition check); ///statement/s after do-while loop False True
  • 46. Problem • Write a program that will continuously take input a number from the user until the number is less than or equal to zero.
  • 47. Solution#include <stdio.h> int main() { int num; do{ printf("Enter the number: "); scanf("%d",&num); printf("You entered %dn",num); }while(num>0); printf("nExitn"); return 0; }
  • 48. Problem • Write a program to show the numbers from 100 to 1. • Write a program to show the odd numbers from 1 to 100 • Write a program to show the even numbers from 100 to 1 • Write a program to show the multiplication table of n where the range of multiplication is from 1 to 10.
  • 49. Problem • Write a program to show the following series 0, 1, 0, 1, 0, 1, 0, 1, … … … upto n th term • Write a program to show the following series 1, 3, 6, 10, 15, … … … upto n th term • Write a program to show the following series 1, 1, 2, 3, 5, 8, … … … upto n th term
  • 50. Problem • Write a program to calculate the sum of 1st n natural numbers i.e. 1+2+3+4+… … … +n
  • 52. Solution #include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d",&n); return 0; }
  • 53. Solution #include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d",&n); int i; for(i=1;i<=n;i++){ } return 0; }
  • 54. Solution #include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d",&n); int sum=0; int i; for(i=1;i<=n;i++){ sum=sum+i; } return 0; }
  • 55. Solution #include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d",&n); int sum=0; int i; for(i=1;i<=n;i++){ sum=sum+i; } printf("The result is %dn",sum); return 0; }
  • 56. Problems • Write a program to calculate the sum of the following series i.e. 2+4+6+… … +2n • Write a program to calculate the sum of squares of 1st n natural numbers i.e. 12 +22+32+42+… … … +n2 • Write a program to calculate the sum of cubes of 1st n natural numbers i.e. 13 +23+33+43+… … … +n3
  • 57. Problems • Write a program to calculate the sum of the following series i.e. 3+32+33+34+… … … +3n • Write a program to calculate the sum of the following series i.e. 1 3 + 1 32 + 1 33 + 1 34 + ⋯ ⋯ + 1 3 𝑛
  • 58. Problem • Write a program to calculate the average of a list of n numbers.
  • 60. Solution #include <stdio.h> int main() { int list_size; printf("Enter the list size : "); scanf("%d",&list_size); return 0; }
  • 61. Solution #include <stdio.h> int main() { int list_size; printf("Enter the list size : "); scanf("%d",&list_size); int count; for(count=1;count<=list_size;count++){ } return 0; }
  • 62. Solution #include <stdio.h> int main() { int list_size; printf("Enter the list size : "); scanf("%d",&list_size); float sum=0; float number; int count; for(count=1;count<=list_size;count++){ } return 0; }
  • 63. Solution #include <stdio.h> int main() { int list_size; printf("Enter the list size : "); scanf("%d",&list_size); float sum=0; float number; int count; for(count=1;count<=list_size;count++){ printf("Enter the %d th number: ",count); scanf("%f",&number); sum=sum+number; } return 0; }
  • 64. Solution #include <stdio.h> int main() { int list_size; printf("Enter the list size : "); scanf("%d",&list_size); float sum=0; float number; float average; int count; for(count=1;count<=list_size;count++){ printf("Enter the %d th number: ",count); scanf("%f",&number); sum=sum+number; } average=sum/list_size; return 0; }
  • 65. Solution #include <stdio.h> int main() { int list_size; printf("Enter the list size : "); scanf("%d",&list_size); float sum=0; float number; float average; int count; for(count=1;count<=list_size;count++){ printf("Enter the %d th number: ",count); scanf("%f",&number); sum=sum+number; } average=sum/list_size; printf("The average of inputted numbers is %.2fn",average); return 0; }
  • 66. Problem • Write a program to calculate the factorial of n. Input: n(an integer) Output: n! We know that, n!= 1*2*3*… … … … *n
  • 68. Solution #include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d",&n); return 0; }
  • 69. Solution #include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d",&n); if(n<0) printf("Invalidn"); else if(n==0) printf("%d ! = %d n",n,1); else{ } return 0; }
  • 70. Solution #include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d",&n); if(n<0) printf("Invalidn"); else if(n==0) printf("%d ! = %d n",n,1); else{ unsigned long int prod=1; int i; for(i=1;i<=n;i++){ } } return 0; }
  • 71. Solution #include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d",&n); if(n<0) printf("Invalidn"); else if(n==0) printf("%d ! = %d n",n,1); else{ unsigned long int prod=1; int i; for(i=1;i<=n;i++){ prod=prod*i; } } return 0; }
  • 72. Solution #include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d",&n); if(n<0) printf("Invalidn"); else if(n==0) printf("%d ! = %d n",n,1); else{ unsigned long int prod=1; int i; for(i=1;i<=n;i++){ prod=prod*i; } printf("%d ! = %lu n",n,prod); } return 0; }
  • 73. Problem • Write a program to calculate nPr where r<=n and both r , n are positive. Input: n,r Output: nPr We know that, nPr = n *(n-1)*(n-2)*… … … *(n-r+1) ex. 5P3= 5.4.3=60
  • 75. Solution #include <stdio.h> int main() { int n,r; printf("Enter the value of n and r: "); scanf("%d %d",&n,&r); return 0; }
  • 76. Solution #include <stdio.h> int main() { int n,r; printf("Enter the value of n and r: "); scanf("%d %d",&n,&r); if(n<0 || r<0 || r>n) printf("Invalidn"); else{ } return 0; }
  • 77. Solution #include <stdio.h> int main() { int n,r; printf("Enter the value of n and r: "); scanf("%d %d",&n,&r); if(n<0 || r<0 || r>n) printf("Invalidn"); else{ int i; for(i=n;i>=(n-r+1);i--){ } } return 0; }
  • 78. Solution #include <stdio.h> int main() { int n,r; printf("Enter the value of n and r: "); scanf("%d %d",&n,&r); if(n<0 || r<0 || r>n) printf("Invalidn"); else{ int prod=1; int i; for(i=n;i>=(n-r+1);i--){ prod=prod*i; } } return 0; }
  • 79. Solution #include <stdio.h> int main() { int n,r; printf("Enter the value of n and r: "); scanf("%d %d",&n,&r); if(n<0 || r<0 || r>n) printf("Invalidn"); else{ int prod=1; int i; for(i=n;i>=(n-r+1);i--){ prod=prod*i; } printf("%dP%d = %dn",n,r,prod); } return 0; }
  • 80. Problem • Write a program to calculate nCr where r<=n and both r , n are positive. Input: n,r Output: nCr We know that, nCr = nPr r! = n ∗(n−1)∗(n−2)∗… … … ∗(n−r+1) 1∗2∗3∗⋯ ⋯∗𝑟 ex. 5C3= 5.4.3 1.2.3 =10
  • 81. continue Statement • The continue statement is used to bypass the remainder of the current pass through a loop. • The loop does not terminate when a continue statement is encountered. • The remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop. • Syntax: continue;
  • 82. Problem • Write a program that will show all the numbers from 1 to 100 except the numbers that are divisible by 5 or 7.
  • 84. Solution #include <stdio.h> int main() { int i; for(i=1;i<=100;i++){ } printf("nDone"); return 0; }
  • 85. Solution #include <stdio.h> int main() { int i; for(i=1;i<=100;i++){ if(i%5==0) continue; } printf("nDone"); return 0; }
  • 86. Solution #include <stdio.h> int main() { int i; for(i=1;i<=100;i++){ if(i%5==0) continue; else if(i%7==0) continue; } printf("nDone"); return 0; }
  • 87. Solution #include <stdio.h> int main() { int i; for(i=1;i<=100;i++){ if(i%5==0) continue; else if(i%7==0) continue; printf("%d ",i); } printf("nDone"); return 0; }
  • 88. Output????? #include <stdio.h> int main() { float x; do{ scanf("%f",&x); if(x<0){ printf("Error Negative value for xn"); continue; } }while(x<=100); return 0; }
  • 89. break Statement • The break statement is used to terminate loops or to exit from a switch statement. • Syntax: break;
  • 90. Guessing Game Problem • Initially your program will guess some fixed number. • Then your program will prompt the user to guess that number. • If the user can’t guess the number then notify him(too small or too big). • If the user guess the number accurately then show a message containing no of attempts he has made to guess that predetermined number.
  • 91. Solution#include <stdio.h> int main() { int pre_value=100; int attempts_count=0; int input; do{ printf("Enter a number (range 1 to 200): "); scanf("%d",&input); attempts_count++; if(input==pre_value){ printf("You won with %d attemptsn",attempts_count); break; } else if(input<pre_value) printf("too smalln"); else printf("too largen"); }while(1); return 0; }
  • 92. Problems to Practice • Write a program to show all the factors of a number. • Write a program to check a number is prime or not. • Write a program to check a number is perfect or not. • Write a program to find out the gcd(Greatest Common Divisor) between two number. • Write a program to find out the lcm(Least Common Multiplier) between two number.
  • 93. Problems to Practice • Write a program to count the number of digits of a given number. • Write a program to convert a decimal number to binary (using only loop).