Loops PDF
Loops PDF
1
Try to solve it using if-else statement
• Example program that continuously asks for
positive number as input:
int n;
printf (“Please enter a positive number:”);
scanf(“%d”,&n);
if (n < 0){
printf (“Enter positive number, BE POSITIVE!\n”);
scanf(“%d”, &n);
}
if (n < 0){
printf (“Enter positive number, BE POSITIVE!\n”);
scanf(“%d”, &n);
}
………
………
2
while Loop
যতkন যিদ
while if ( condition )
statement;
Logic of an if statement
condition
evaluated
true
false
statement
3
Logic of a while Loop
if logic The while Loop
condition condition
evaluated evaluated
true false
true
false
statement statement
4
The while Statement
• Example program that continuously asks for
positive number as input:
int n;
Some examples
• Print “The sky is the limit!” 10 times.
main(){
printf (“The sky is the limit”);
}
5
Some examples
• Print “The sky is the limit!” 10 times.
main(){
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
Some examples
• Print “The sky is the limit!” 100 times.
main(){
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
printf (“The sky is the limit”);
6
Some examples
• Print “The sky is the limit!” n times. n will be user
input
scanf(“%d”,&n);
int count = 1;
while (count <= n)
{
printf (“The sky is the limit”);
count++;
}
• If the condition of a while loop is false initially, the
statement is never executed
• Therefore, the body of a while loop will execute
zero or more times
Some examples
• Print first n natural numbers.
q Upwards
q Downwards
• Print odd numbers up to n.
• Print even numbers up to n.
• Print summation of first n numbers.
• Print summation of all odd numbers up to n.
• Print summation of all even numbers up to n.
• Print second largest of a series of natural numbers
(at least two) given as input. STOP when the user
enters 0. Natural numbers are 1, 2, 3, 4……
7
x y x
4 21 36 14 62 91 8 22 7 81 77 10
x x x
secL = 4 largest = 21
secL largest
Third element
x
initialization;
while ( condition )
{
statement;
increment;
}
8
initialization;
while ( condition )
{
statement;
increment;
}
initialization
condition
evaluated
false
true
statement
increment
9
The for Statement
1 ≤ count ≤ n
• An example of a for loop: 1 ≤ count and count ≤ n
for (count=1; count <= n; count++)
printf (“%d\n”, count);
10
The break and coninue Statement
• Sometimes we need:
§ to skip some statements inside the loop (continue)
§ or terminate the loop immediately without checking the
test condition (break).
11
Example: break Statement
//
Program
to
calculate
the
sum
of
maximum
of
10
numbers
//
If
negative
number
is
entered,
loop
terminates,
sum
is
displayed
main(
)
{
int
i;
double
number,
sum
=
0.0;
for(i=1;
i
<=
10;
++i)
{
printf("Enter
n%d:
",i);
scanf("%lf",
&number);
//
If
user
enters
negative
number,
loop
is
terminated
if(number
<
0.0)
{
break;
}
sum
+=
number;
}
printf("Sum
=
%.2lf",sum);
}
12
Example: continue Statement
//
Program
to
calculate
the
sum
of
maximum
of
10
+ve
numbers
//
If
negative
number
is
entered,
it
is
ignored
main(
)
{
int
i;
double
number,
sum
=
0.0;
for(i=1;
i
<=
10;
++i)
{
printf("
Enter
n%d:
",
i);
scanf("%lf",
&number);
//
If
user
enters
negative
number,
skip
it
if(number
<
0.0)
{
continue;
}
sum
+=
number;
}
printf("Sum
=
%.2lf",sum);
}
1 + 2 + 3 + 4 + ...... + up to n
i =1 i =2 i =n
int main(){
int i, n,t,s = 0;
t=i scanf("%d",&n);
for(i = 1; i <= n; i++){
t = i;
s = s + t;
}
printf("%d",s);
}
13
Some example problems
• Write down a program to find the summation of the following
series:
t1 t2 tn
12 − 2 2 + 32 − 4 2 + ......up to n 2
i =1 i =2 i =n
int main(){
int i, n,t,s = 0;
t = i2 when i is odd scanf("%d",&n);
for(i = 1; i <= n; i++){
t = -i2 when i is even if(i%2 == 0)
t = i*i;
else
t = -i*i;
s = s + t;
}
printf("%d",s);
}
…………….. N terms
i =n
int main(){
i =1 i =2 int i, n;
float x,t,r,s = 0;
scanf("%f%d",&x, &n);
Power of x à 2i+1 x = 22.0*x/(7*180);
s = t = x;
r à -x2 / (2i×(2i+1)) for(i = 1; i < n; i++){
r = -x*x/(2*i*(2*i+1));
tnew à r×tprev t = r*t;
s = s + t;
}
printf("%f",s);
}
14
Some example problems
• Print factorial of n:
t1 t2 tn
n! = 1 × 2 × 3 × 4 × ...... × up to n
i =1 i =2 i =n
int main(){
int i, n,t,p = 1;
t=i scanf("%d",&n);
for(i = 1; i <= n; i++){
t = i;
p = p * t;
}
printf("%d",p);
}
t1 t2 tn
i =1 i =2 i =n
int main(){
int i,x,n,t,p = 1;
t=x scanf("%d%d",&x,&n);
for(i = 1; i <= n; i++){
t = x;
p = p * t;
}
printf("%d",p);
}
15
Some example problems
• Show all factors of a number n
• Candidates 1, 2, 3, 4 ………. n
int main(){
int i,n;
scanf("%d",&n);
for(i = 1; i <= n; i++){
if(n%i == 0)
printf("%d ",i);
}
}
int main(){
int i,n;
scanf("%d",&n);
for(i = 2; i <= n; i++){
if(n%i == 0){
printf("%d",i);
break;
}
}
}
16
Some example problems
• Show largest factor of a number n (other than n)
• Candidates 1, 2, 3, 4 ………. n
• Break on first candidate that becomes a factor
• Number = largest factor * smallest factor
• largest factor = Number/smallest factor
• Example 28 à factors 2, 4, 7, 14, smallest 2, largest 14
int main(){
int i,n;
scanf("%d",&n);
for(i = 2; i <= n; i++){
if(n%i == 0){
printf("%d",n/i);
break;
}
}
}
int main(){
int i,n,c=0;
scanf("%d",&n);
for(i = 1; i <= n; i++){
if(n%i == 0)
c++;
}
printf("Number of factors: %d",c);
}
17
Some example problems
• Primality testing: determine whether a number n is prime or
not
• Candidates 1, 2, 3, 4 ………. n
• Increment a counter whenever you get a candidate
which is a factor
• Prime numbers always have two factors.
int main(){
int i,n,c=0;
scanf("%d",&n);
for(i = 1; i <= n; i++){
if(n%i == 0)
c++;
}
if(c == 2)
printf("Prime Number");
else printf("Not a Prime Number");
}
18
Some example problems
• Perfect number testing: determine whether a number n is perfect
or not
• If a number can be made out of its factors
• For example 6 à 1, 2, 3 à 1+ 2 +3 = 6
• Another example 28 à 1,2,4,7,14 à 1+2+4+7+14
• Candidates 1, 2, 3, 4 ………. n
• Add to sum whenever you get a candidate which is a factor
int main(){
int i,n,s=0;
scanf("%d",&n);
for(i = 1; i < n; i++){
if(n%i == 0)
s = s + i;
}
if(s == n)
printf("Perfect Number");
else printf("Not a Perfect Number");
}
19
Some example problems
• GCD of two numbers (Efficient way)
• gcd(a,b) = gcd (b, a%b) for b > 0
• gcd(54,24) à gcd(24,6) à gcd(6,0) à 6
int main(){
int i,a,b;
scanf("%d%d",&a,&b);
while(b != 0){
c = a%b;
a = b;
b = c;
}
printf("GCD: %d", a);
}
Fibonacci Series
20
Fibonacci Series Generation
1 2 3 4 5 6 7 8 9
int main(){
int p1,p2,next,n;
scanf("%d",&n);
p1 = 1;
p2 = 1;
next = p1 + p2;
nextnext = p2 + next;
nextnextnext = next + nextnext;
…..
….
}
21
p1 p2 next
p1 p2 next
int main(){
int p1,p2,next,n;
scanf("%d",&n);
p1 = 1;
p2 = 1;
for(i = ; i <= ; i++){
next = p1 + p2;
p1 = p2;
p2 = next;
}
p1 p2 next
p1 p2 next
int main(){
int p1,p2,next,n;
scanf("%d",&n);
p1 = 1;
p2 = 1;
for(i = 3; i <= n; i++){
next = p1 + p2;
p1 = p2;
p2 = next;
}
if(n <= 2) printf("%d", p1);
else printf("%d", next);
}
22
The for Statement
• Each expression in the header of a for loop is
optional
Infinite Loops
• The body of a while loop eventually must make
the condition false
23
Infinite Loops
• An example of an infinite loop:
int count = 1;
while (1 == 1){
printf (“%d\n”, count);
count = count - 1;
}
int count = 1;
for(; ;){
printf (“%d\n”, count);
count = count - 1;
}
The do Statement
• A do statement has the following syntax:
do{
statement;
}
while ( condition );
24
Logic of a do-while Loop
statement
true
condition
evaluated
false
The do Statement
• An example of a do loop:
int count = 1;
do{
printf(“%d\n”, count);
count++;
} while (count <= 5);
25
The do Statement
• An example of a do loop:
int n;
do{
printf(“Enter a positive number: ”);
scanf(“%d”,&n);
} while (n < 0);
statement
condition
evaluated
true
condition
true false
evaluated
statement
false
26
Example: Printing reverse of a number
• Write down a program that prints the digits of a
number in reverse.
27
Nested Loops
Nested Loops
• Similar to nested if statements, loops can be
nested as well
28
Nested Loops
• What will be the output?
Outer loop
for(i=1; i <= 3; i++){
for(j=1; j <= 2; j++){
Inner loop printf(“Sky is the limit\n”);
}
printf(“The world is becoming smaller\n”);
}
Output
for(i=1; i <= 3; i++){
for(j=1; j <= 2; j++){
printf(“Sky is the limit\n”);
}
printf(“The world is becoming smaller\n”);
}
29
Output
for(i=1; i <= 3; i++){
for(j=1; j <= 2; j++){
printf(“Sky is the limit\n”);
}
printf(“The world is becoming smaller\n”);
}
Output
for(i=1; i <= 3; i++){
for(j=1; j <= 2; j++){
printf(“Sky is the limit\n”);
}
printf(“The world is becoming smaller\n”);
}
30
Output
for(i=1; i <= 3; i++){
for(j=1; j <= 2; j++){
printf(“%d %d\n”,i,j);
}
}
1 1
1 2
2 1
2 2
3 1
3 2
Nested Loops
• How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
printf ("Here \n");
count2++;
}
count1++;
} 10 * 20 = 200
31
Analogy for Nested Loops
Inner Loop
Outer Loop
32
Example: Stars
• Write a program that prints the following. The total
number of lines will be input to your program.
*
**
***
****
*****
******
*******
********
*********
**********
Example: Stars
• Write a program that prints the following. The total
number of lines will be input to your program.
*
**
***
****
*****
33