0% found this document useful (0 votes)
28 views

Looping: Prepared by Sukanta Paul Lecturer, CSE, FSET

The document discusses looping in programming languages using for, do-while, while loops. It contains examples of nested loops printing the sum of iterations. The last example calculates the sum as 23.

Uploaded by

Kaisar Jamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Looping: Prepared by Sukanta Paul Lecturer, CSE, FSET

The document discusses looping in programming languages using for, do-while, while loops. It contains examples of nested loops printing the sum of iterations. The last example calculates the sum as 23.

Uploaded by

Kaisar Jamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Looping

Prepared by
Sukanta Paul
Lecturer, CSE, FSET
i Condition(i<20) Condition(i%5==0) x print
0 - 0 -

0 True True 0 0

1 True False - -
2 True False - -
3 True False - -
4 True False - -
5 True True 5 5
6 True False - -
7 True False - -
8 True False - -
9 True False - -
10 True True 15 15
11 True False - -
12 True False - -
13 True False - -
14 True False - -
15 True True 30 30
16 True False - -
17 True False - -
18 True False - -
19 True False - -
20 False - - -
2
x=30
i Condition(i<20) Condition(i%5==0) x print
0 - 0 -

0 True True 0 0

1 True False - -
2 True False - -
3 True False - -
4 True False - -
5 True True 5 5
6 True False - -
7 True False - -
8 True False - -
9 True False - -
10 True True 15 15
11 True False - -
12 True False - -
13 True False - -
14 True False - -
15 True True 30 30

Output: 16
17
True
True
False
False
-
-
-
-

0 5 15 30 18
19
True
True
False
False
-
-
-
-

3
x = 30 20 False - - -
x=30
#include<stdio.h>

main()
{
int i, x=0;
for(i=0;i<20;++i)
{
if(i%5==0)
For loop {
x+=i;
printf(“%d ”,x);
}
}
printf(“\nx = %d”, x);
}

4
#include<stdio.h>

main()
{
int i=0, x=0;
do
{
if(i%5==0)
Do-while loop {
x+=i;
printf(“%d ”,x);
}
++i;
}while(i<20);
printf(“\nx = %d”, x);
}

5
i Condition(i<5) j Condition(j<i) x print

- - - - 0 -
0 True 0 False - -
1 True 0 True 0 0
- - 1 False - -
2 True 0 True 1 1
- - 1 True 3 3
- - 2 False - -
3 True 0 True 5 5
- - 1 True 8 8
- - 2 True 12 12
- - 3 False - -
4 True 0 True 15 15
- - 1 True 19 19
- - 2 True 24 24
- - 3 True 30 30
- - 4 False - -
5 False - - - -
x = 30

6
i Condition(i<5) j Condition(j<i) x print

- - - - 0 -
0 True 0 False - -
1 True 0 True 0 0
- - 1 False - -
2 True 0 True 1 1
- - 1 True 3 3
- - 2 False - -
3 True 0 True 5 5
- - 1 True 8 8
- - 2 True 12 12
- - 3 False - -
4 True 0 True 15 15

Output: - - 1 True 19 19
- - 2 True 24 24
0 1 3 5 8 12 15 19 24 30 - - 3 True 30 30
- - 4 False - -
x = 30 5 False - - - -
x = 30

7
#include<stdio.h>

main()
{
int i=0,j, x=0;
do
{
j=0;
Do-while loop do
{
x+=(i+j-1);
printf(“%d ”,x);
++j;
}while(j<i);
++i;
}while(i<5);
printf(“\nx = %d”, x);
}

8
#include<stdio.h>

main()
{
int i=0,j, x=0;
while(i<5)
{
j=0;
While loop while(j<i)
{
x+=(i+j-1);
printf(“%d ”,x);
++j;
}
++i;
}
printf(“\nx = %d”, x);
}

9
i Condition(i<5) j Condition(j<i) i+j-1 x print
- - - - 0 -
0 True 0 False - -
1 True 0 True 0 1 1
- - 1 False - - -
2 True 0 True 1 3 3
- - 1 True 2 5 5
- - 2 False - - -
3 True 0 True 2 7 7
- - 1 True 3 9 9
- - 2 True 4 12 12
- - 3 False - - -
4 True 0 True 3 14 14
- - 1 True 4 17 17
- - 2 True 5 20 20
- - 3 True 6 23 23
- - 4 False - - -
5 False - - - - -
x = 23

10
i Condition(i<5) j Condition(j<i) i+j-1 x print
- - - - 0 -
0 True 0 False - -
1 True 0 True 0 1 1
- - 1 False - - -
2 True 0 True 1 3 3
- - 1 True 2 5 5
- - 2 False - - -
3 True 0 True 2 7 7
- - 1 True 3 9 9
- - 2 True 4 12 12
- - 3 False - - -
4 True 0 True 3 14 14
- - 1 True 4 17 17
- - 2 True 5 20 20
- - 3 True 6 23 23
- - 4 False - - -
5 False - - - - -
x = 23

Output:
1 3 5 7 9 12 14 17 20 23
11 x = 23
#include<stdio.h>

main()
{
int i=0, j, k, x=0;
while(i<5)
{
j=0;
while(j<i)
{
switch(i+j-1){
case -1:
While loop case 0:
x+=1;
break;
case 1:
case 2:
case 3:
x+=2;
break;
default:
x+=3;
}
printf(“%d ”,x);
++j;
}
++i;
}
12 printf(“\nx = %d”, x);
}
#include<stdio.h>

main()
{
int i=0, j, k, x=0;
do
{
j=0;
do
{
switch(i+j-1){
case -1:
Do-while loop case 0:
x+=1;
break;
case 1:
case 2:
case 3:
x+=2;
break;
default:
x+=3;
}
printf(“%d ”,x);
++j;
} while(j<i);
++i;
} while(i<5);
13 printf(“\nx = %d”, x);
}
Assignment(Convert each loop to other and calculate the output)

14
15
16
Write a Program to add up to n numbers/to calculate
1+2+3+… … … +n= ?.
Suppose, n=6
At first, sum=0
We need to add 1+2+3+4+5+6
Addition operation can be performed in this way:
0+1=1 sum
1+2=3
i
3+3=6
6+4=10
10+5=15
15+6=21
We can use looping because to get result more than one addition
operation is needed.
17
#include<stdio.h>
int main()
{
int i,n,sum=0;
printf("n = ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum+=i;
}
printf("\nTotal Sum from 1 to %d is %d",n,sum);
return 0;
}
Input:
n=6
Output:
18 Total Sum from 1 to 6 is 21
Task:
Write a Program to show the multiplication table depend on n
Input:
The length of multiplication table: 5
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50

19
n
Write a program to evaluate x where n is a nonnegative integer.
Remember program will be terminate iff x and n both are 0.
#include<stdio.h>
int main()
{
int count,n;
float x,y;
while(1)
{
printf("Enter the values of x and n: ");
scanf("%f %d",&x,&n);
if( x==0 && n==0)
break;
y=1.0;
count=1;
while(count<=n)
{
y=y*x;
count++;
}
printf("\nx = %f , n = %d , x to power n = %f\n",x,n,y);
}
return 0;
20 }
Write a program to evaluate the following investment equation and prints the table
which would give the value of V for various combination of value P, r & n where
P & n are integer numbers and r is a floating point number
V  P(1  r ) n

21
Task
 Write a Program to show all combinations of 1,2 and 3.
 Write a program to calculate the sum and average of n numbers.
 Write a program to calculate following
 1+3+5+… … … + 2n-1 = ?
 2+4+6+… … …+2n=?
 1+4+7+… … …+2n-2=?
 e^x=1+x/1!+x^3/2!+x^5/3! +..........+x^n/n!
 cos x =1-x^2/2!+x^4/4!-x^6/6!+..… +x^n/n!
 1/(1-x)=1+x^2+x^3+… … …+x^n
 Write a program to show the following depend upon n:
n=4
1
12
123
22 1234
 Write a program to show the following depend upon n:
n=4
1
22
333
4444

 Write a program to show the following depend upon n:


n= 4 and c= *
*
**
***
****
23
 Write a program to show the following depend upon n:
n= 4 and c= *
****
***
**
*
 Write a program to show diamond depend upon n:
n= 4 and c= *
*
* *
* * *
* * * *
* * *
* *
24 *
 Write a Program to produce the following output depend
upon n: suppose n=4.
*
**
***
****
• Write a Program to produce the following output depend
upon n: suppose n=4.
1
2 3
4 5 6
7 8 9 10

25

You might also like