Professional Documents
Culture Documents
GTU # 3110003
USING
{C}
Looping Programming
“Hello” 5
Output
printf("Hello\n"); Hello
printf("Hello\n"); Hello loop(condition)
{
printf("Hello\n"); Hello //statements
printf("Hello\n"); Hello }
printf("Hello\n"); Hello
False False
condition condition
True True
… …
Program Output
1 #include <stdio.h> Enter n:10
2 void main() 1
3 { 2
4 int i,n; 3
5 i=1; 4
6 printf("Enter n:"); 5
7 scanf("%d",&n); 6
8 while(i<=n) 7
9 { 8
10 printf("%d\n",i); 9
11 i=i+1; 10
12 }
13 }
Program Output
1 #include<stdio.h> Enter n for multiplication table:5
2 void main() 5 * 1 = 5
3 { 5 * 2 = 10
5 * 3 = 15
4 int i=1,n;
5 * 4 = 20
5 printf("Enter n for multiplication table:"); 5 * 5 = 25
6 scanf("%d",&n); 5 * 6 = 30
7 while(i<=10) 5 * 7 = 35
8 { 5 * 8 = 40
9 printf("%d * %d = %d\n",n,i,n*i); 5 * 9 = 45
10 i=i+1; 5 * 10 = 50
11 }
12 }
Program Output
1 #include<stdio.h> Enter a number=10
2 void main() Enter a number=20
3 { Enter a number=30
Enter a number=40
4 int sum=0, i=1,n;
Enter a number=50
5 while(i<=5) Sum is=150
6 {
7 printf("Enter a number=");
8 scanf("%d",&n);
9 sum=sum+n;
10 i=i+1;
11 }
12 printf("Sum is=%d",sum);
13 }
loop (repeat 50
10 times)
{
printf("*");
}
Syntax Logic
while(condition) int i = 1;
{ while (i <= 5)
// Body of the while {
// true part printf("%d\n", i);
} i=i+1;
}
From this we can infer that loop variable i starts with 1 and incremented by one
for next iteration then ends at value n.
Consider n=10, factors are 1,2,5,10
Consider n=11, factor is 1,11
From this we can infer that 1 and number itself are always factors of any number n.
i=i+1
Stop
Prof. Nilesh Gambhava #3110003 (PPS) – Looping 16
How to build logic? Step-4
Step 4: Writing Pseudo-code
Pseudo-code is an informal way to express the design of a computer program or
an algorithm.
It does not require any strict programming language syntax.
Pseudo-code
Initialize i=1 integer
Declare n as integer
Input n
while i<n
if n%i
print i
end if
increment i=i+1
end while
Program Output
1 #include <stdio.h> Enter n to find factors=12
2 void main() 1,2,3,4,6,12,
3 {
4 int i=1,n;
5 printf("Enter n to find factors=");
6 scanf("%d",&n);
7 while(i<=n)
8 {
9 if(n%i==0)
10 printf("%d,",i);
11 i=i+1;
12 }
13 }
Program Output
1 #include <stdio.h> Enter a number=1234
2 void main() 4321
3 {
4 int n;
5 printf("Enter a number=");
6 scanf("%d",&n);
7 while(n!=0)
8 {
9 printf("%d",n%10);
10 n=n/10;
11 }
12 }
Program Output
1 #include<stdio.h> Enter a number:5
2 void main() 1
3 { 2
4 int i,n; 3
5 printf("Enter a number:"); 4
6 scanf("%d",&n); 5
7 for(i=1;i<=n;i++)
8 {
9 printf("%d\n",i);
10 }
11 }
Program Output
1 #include <stdio.h> Enter n to find factors=12
2 void main() 1,2,3,4,6,12,
3 {
4 int i,n;
5 printf("Enter n to find factors=");
6 scanf("%d",&n);
7 for(i=1;i<=n;i++)
8 {
9 if(n%i==0)
10 printf("%d,",i);
11 }
12 }
Program Output
1 void main() Enter a number:5
2 { 1,3,5
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 if(i%2!=0)
9 {
10 printf("%d,",i);
11 }
12 i=i+1;
13 }
14 while(i<=n);
15 }
Program Output
1 void main() Enter a number:6
2 { 1,2,3,6,
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 if(n%i==0)
9 {
10 printf("%d,",i);
11 }
12 i=i+1;
13 }
14 while(i<=n);
15 }
Program Output
1 void main() Enter a number=1234
2 { 4321
3 int n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 printf("%d",n%10);
9 n=n/10;
10 }
11 while(n!=0);
12 }
The label is an identifier. When the goto statement is encountered, the control of
the program jumps to label: and starts executing the code.
Program Output
1 void main() Enter a number:5
2 { 1,3,5
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 odd:
7 if(i%2!=0)
8 {
9 printf("%d,",i);
10 }
11 i=i+1;
12 if(i<=n)
13 {
14 goto odd;
15 }
16 }
Program Output
1 void main() Enter a number:6
2 { 1,2,3,6,
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 odd:
7 if(n%i==0)
8 {
9 printf("%d,",i);
10 }
11 i=i+1;
12 if(i<=n)
13 {
14 goto odd;
15 }
16 }
False
Loop Body Label Statement
condition
True True
condition condition
Loop Body
False True False
goto
1 1 1 *
11 12 23 * *
111 123 456 * * *
1111 1234 78910 * * * *
11111 12345 * * *
* *
*
Starting: *
Starting: 1