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
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
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
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
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.
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
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
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.
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).