SlideShare a Scribd company logo
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).
Ad

More Related Content

What's hot (18)

C programms
C programmsC programms
C programms
Mukund Gandrakota
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
MomenMostafa
 
For Loop
For LoopFor Loop
For Loop
Ghaffar Khan
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 
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 C
Bilal Mirza
 
3. control statement
3. control statement3. control statement
3. control statement
Shankar Gangaju
 
C Programming by Süleyman Kondakci
C Programming by Süleyman KondakciC Programming by Süleyman Kondakci
C Programming by Süleyman Kondakci
Süleyman Kondakci
 
Cpds lab
Cpds labCpds lab
Cpds lab
praveennallavelly08
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
Prasadu Peddi
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
Haard Shah
 
Stack prgs
Stack prgsStack prgs
Stack prgs
Ssankett Negi
 
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 C
Jeya Lakshmi
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
Lakshmi Sarvani Videla
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
JAYA
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
MomenMostafa
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 
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 C
Bilal Mirza
 
C Programming by Süleyman Kondakci
C Programming by Süleyman KondakciC Programming by Süleyman Kondakci
C Programming by Süleyman Kondakci
Süleyman Kondakci
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
Prasadu Peddi
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
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 C
Jeya Lakshmi
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
JAYA
 

Similar to SPL 8 | Loop Statements in C (20)

Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
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
Hemantha Kulathilake
 
Looping
LoopingLooping
Looping
Kathmandu University
 
04-Looping( For , while and do while looping) .pdf
04-Looping( For , while and do while looping) .pdf04-Looping( For , while and do while looping) .pdf
04-Looping( For , while and do while looping) .pdf
nithishkumar2867
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
NishmaNJ
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
baabtra.com - No. 1 supplier of quality freshers
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
baabtra.com - No. 1 supplier of quality freshers
 
Looping statements c language basics ppt with example
Looping statements c language basics ppt with exampleLooping statements c language basics ppt with example
Looping statements c language basics ppt with example
aryann1217z
 
Looping statements.pptx for basic in c language
Looping statements.pptx for basic in c languageLooping statements.pptx for basic in c language
Looping statements.pptx for basic in c language
aryann1217z
 
Lecture 13 Loops1 with C++ programming.PPT
Lecture 13 Loops1 with C++ programming.PPTLecture 13 Loops1 with C++ programming.PPT
Lecture 13 Loops1 with C++ programming.PPT
SamahAdel16
 
Bsit1
Bsit1Bsit1
Bsit1
jigeno
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
Introduction to C Programming -Lecture 3
Introduction to C Programming -Lecture 3Introduction to C Programming -Lecture 3
Introduction to C Programming -Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
Loops
LoopsLoops
Loops
Kamran
 
Loops in c
Loops in cLoops in c
Loops in c
shubhampandav3
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
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
Hemantha Kulathilake
 
04-Looping( For , while and do while looping) .pdf
04-Looping( For , while and do while looping) .pdf04-Looping( For , while and do while looping) .pdf
04-Looping( For , while and do while looping) .pdf
nithishkumar2867
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
NishmaNJ
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
Looping statements c language basics ppt with example
Looping statements c language basics ppt with exampleLooping statements c language basics ppt with example
Looping statements c language basics ppt with example
aryann1217z
 
Looping statements.pptx for basic in c language
Looping statements.pptx for basic in c languageLooping statements.pptx for basic in c language
Looping statements.pptx for basic in c language
aryann1217z
 
Lecture 13 Loops1 with C++ programming.PPT
Lecture 13 Loops1 with C++ programming.PPTLecture 13 Loops1 with C++ programming.PPT
Lecture 13 Loops1 with C++ programming.PPT
SamahAdel16
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
 
Ad

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
Mohammad Imam Hossain
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
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
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 Search
Mohammad Imam Hossain
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
Mohammad 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 Introduction
Mohammad Imam Hossain
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
Mohammad 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 Schema
Mohammad Imam Hossain
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
Mohammad 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 Schema
Mohammad Imam Hossain
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
Mohammad Imam Hossain
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
Mohammad 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 Check
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 Search
Mohammad 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 Introduction
Mohammad 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 Schema
Mohammad 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 Schema
Mohammad 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 Check
Mohammad Imam Hossain
 
Ad

Recently uploaded (20)

antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
GENERAL QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 4 MARCH 2025 .pdf
GENERAL QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 4 MARCH 2025 .pdfGENERAL QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 4 MARCH 2025 .pdf
GENERAL QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 4 MARCH 2025 .pdf
Quiz Club of PSG College of Arts & Science
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
Quiz Club of PSG College of Arts & Science
 
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDM & Mia eStudios
 
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & ConfigurationsBipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
GS Virdi
 
How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18
Celine George
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
PUBH1000 Slides - Module 11: Governance for Health
PUBH1000 Slides - Module 11: Governance for HealthPUBH1000 Slides - Module 11: Governance for Health
PUBH1000 Slides - Module 11: Governance for Health
JonathanHallett4
 
Cyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top QuestionsCyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top Questions
SONU HEETSON
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
The History of Kashmir Lohar Dynasty NEP.ppt
The History of Kashmir Lohar Dynasty NEP.pptThe History of Kashmir Lohar Dynasty NEP.ppt
The History of Kashmir Lohar Dynasty NEP.ppt
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERSIMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
rajaselviazhagiri1
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
ArkaDas54
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDM & Mia eStudios
 
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & ConfigurationsBipolar Junction Transistors (BJTs): Basics, Construction & Configurations
Bipolar Junction Transistors (BJTs): Basics, Construction & Configurations
GS Virdi
 
How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18
Celine George
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
PUBH1000 Slides - Module 11: Governance for Health
PUBH1000 Slides - Module 11: Governance for HealthPUBH1000 Slides - Module 11: Governance for Health
PUBH1000 Slides - Module 11: Governance for Health
JonathanHallett4
 
Cyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top QuestionsCyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top Questions
SONU HEETSON
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERSIMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
rajaselviazhagiri1
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
ArkaDas54
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 

SPL 8 | Loop 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).
  翻译: