You are on page 1of 7

Loops

‘for’ loop
 General form:
for (initialization; condition; increment/decrement)
{
// code
}

 Example:
for (i = 1; i<=100; i++)
cout<<i<<“ “;

• What will be the output?

Dr. Md. Rakib Hassan


Examples
 Example:
for (i = 0; i<=100; i = i + 5)
cout<<i<<“ “;

 Output?
• What will be the output?

Dr. Md. Rakib Hassan


Exercise

 Write the outputs for each of the following:


• for (i = 100; i<=1; i = i + 5) cout<<i<<“ “;

• for (i = 100; i>1; i = i + 5) cout<<i<<“ “;

• for (i = 100; i>1; i = i - 10) cout<<i<<“ “;

• for (i = 1; i>= 10; i = i++) cout<<i<<“ “;

• for (i = 20; i<=1; i--) cout<<i<<“ “;

• for (x = 20; x>=5; x = x + 5) cout<<i<<“ “;

• for (x = 20; x>=5; x = x - 5) cout<<i<<“ “;

Dr. Md. Rakib Hassan


Programming Exercise
 Find the summation from 1 to 100.

int i, sum=0;
for (i=1; i<=100; i++)
sum = sum + I;
cout<<sum;

 Write a program to find the summation from 1 to 1000 and their


average.
 Write a program to find the summation for the series (2, 4, 6, 8, 10,
12, …. , 100) and their average.

Dr. Md. Rakib Hassan


Programming Exercise
 Write a program to print the multiplication table for any number.
• For example:
9x1=9
9 x 2 = 18
9 x 3 = 27
.
.
.
9 x 10 = 90

 Write a program to find the factorial of a number.


 Write a program to print the first N terms of the Fibonacci series.
 Write a program to calculate power of a number using ‘for’ loop.
• X^Z

Dr. Md. Rakib Hassan


Questions?

Dr. Md. Rakib Hassan

You might also like