Iterative statements:
In C programming, loops are a fundamental concept
used to execute a block of code multiple times as long
as a certain condition is true.
C supports three types of loops:
[Link]
[Link] –while
[Link]
Construction of loops:
Initialization: Assign the value to a variable
Ex: i=1
Condition: Apply the condition with input variable
Ex: i<=n, i<n, n>0, n==0, x==y
updation: update the variable value
Ex: i++ , i-- , ++i, --i
While statement
while(condition)
{
Statements; Repeat
}
F While
(condition)
T Repeat
statements
stop
Examples
[Link] print horizontal asterisk symbols
[Link] print 1to n numbers.
[Link] print sum of n numbers
[Link] print even number between 1 to n
5. to print odd numbers between 1 ton
[Link] print multiplication table of the any given number.
[Link] find factorial of any given number.
[Link] generate Fibonacci series.
[Link] convert decimal to binary number.
[Link] find sum of the digits of given number.
11. Sum of set of given numbers
[Link] print the given number in reverse order
[Link] find power of n value
[Link] find sqrt of the any given number
[Link] find GCD of two numbers
[Link] check whether given no perfect or not
[Link] number or not
[Link] number or not
[Link] number or not
[Link] number or not
[Link] print horizontal asterisk symbols
while(i<=n) => i=1;
{
printf(“*”);
i++;
}
[Link] print 1to n numbers.
While(i<=n) i=1;
{
Printf(“%d”,i)
i++;
}
[Link] print sum of n numbers
while(i<=n) i=1,sum=0,n;
{
sum=sum+i;
i++;
}
[Link] print even number between 1 to n
While(i<=n)
{
if(i%2==0)
{
printf("%d\n", i);
}
i++;
}
Even numbers without if condition
While(i<=n) => i=2
{
printf("%d\n",i);
i+=2;
}
[Link] numbers from 1 to n
while(i<=n) => i=1
{
if(i%2!=0)
{
printf("%d\n", i);
}
i++;
}
Odd numbers without if condition
while(i<=n) => i=1
{
printf("%d\t",i);
i=i+2;
}
[Link] print multiplication table of the any
given number
while(i<=10)
{
m=n*i;
printf(“%d*%d=%d”,n,i,m);
i++;
}
[Link] find factorial of any given number
While(i<=n)
{
fact=fact*i;
i++;
}
Printf(“fact=%d”,fact);
getch();
[Link] generate Fibonacci series
printf(“%d%d”,f0,f1); i=1,n,f0=0,f1=1,f2
while(i<=n)
{
f2=f0+f1;
Printf(“%d”,f2);
f0=f1;
f1=f2;
i++;
}
[Link] convert decimal to binary number
while(n>0) r,bi=0,n,d=1;
{
r=n%2;
bi=bi+r*d;
n=n/2;
d=d*10;
}
Printf(“bi=%d”,bi);
getch();
[Link] find sum of the digits of given number
while(n>0) n,sum=0,r;
{
r=n%10;
Sum=sum+r;
n=n/10;
}
Printf(“sum=%d”,sum);
getch();
}
[Link] of set of given numbers
while(i<=m)
{
scanf("%d",&n);
sum=sum+n;
i++;
}
printf("sum=%d",sum);
[Link] print the given number in reverse order
While(n>0) n,r,rev=0,n;
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
Printf(“rev=%d”,rev);
getch();
[Link] find power of n value
while(i<=p)
{
sum=sum*n;
i++;
}
printf("sum=%d",sum);
[Link] find sqrt of the any given number
while(x<n) i=1,n,x=0,c=0;
{
x=x+i;
c++;
i+=2;
}
printf("sqrt=%d",c);
[Link] find GCD of two numbers
while(n1!=n2) i/p: n1=28 n2=20
{
if(n1 > n2) o/p:=4
{
n1=n1-n2;
}
else
{
n2=n2-n1;
}
}
The factors of 28 are 1, 2, 4, 7, 14, and 28.
Factors of 24 are 1, 2, 3, 4, 6, 8, 12 and 24.
[Link] common divisor of any two numbers
#include <stdio.h>
int main()
{
int n1, n2, i = 2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
while (i <= n1 && i <= n2)
{
if (n1 % i == 0 && n2 % i == 0)
{
printf("Smallest common divisor (greater than 1) is: %d\n", i);
break;
}
i++;
}
if (i > n1 || i > n2)
{
printf("No common divisor greater than 1. So output is: 1\n");
}
return 0;
}
17. perfect or not
b=n;
While(i<n) i=1,n,c=0
{
if(n%i==0)
{
c=c+i;
}
i++;
}
if(b==c)
{
Perfect number;
}
else
{
not a perfect number;
}
getch();
}
[Link] number or not
While(i<=n) Declare: i=1,n,c=0
{
if(n%i==0)
{
c=c+1;
}
i++;
}
if(c==2)
{
prime number;
}
else
{
not a prime number;
}
getch();
}
[Link] number or not
b=n; Declare: b,n,r,rev=0
while(n>0)
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
if(b==rev)
{
palindrome number;
}
else
{
not a palindrome;
}
getch();
}
[Link] number
Armstrong number is a number that is equal to the
sum of cubes of its digits.
Example: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407,
1634, 8208, 9474, 54748
Armstrong number
[Link] number or not
n1=n2=n; Declare: n,n1,n2,digit=0,r,sum=0;
while(n1>0)
{
digit=digit+1;
n1=n1/10;
}
while(n2>0)
{
r=n2%10;
sum=sum+pow(r,digit);
n2=n2/10;
}
if(sum==n)
{
Armstrong number;
else
{
Not a Armstrong number;
}
[Link] number
A number can be said as a strong number when the sum
of the factorial of the individual digits is equal to the
number.
Examples:
1
2
145
40585
[Link] number or not
b=n; Declare: b,n,r,fact,sum=0;
While(n>0)
{
r=n%10;
i=1,fact=1;
while(i<=r)
{
fact=fact*i;
i++;
}
sum=sum+fact;
n=n/10;
}
if(sum==b)
{
strong number;
}
else
{
not a strong number;
}
getch();
}
Infinite while loop
If the condition to be tested is always true, the loop will run
forever i.e. infinite times.
while (1)
{
printf ("This is infinite loop");
}
To stop an infinite loop, break statement can be used.
For example,
while (1)
{
printf("This loop will run only once");
break;
}
1.#include<stdio.h>
void main()
{
while(1)
printf("Hello");
}
2.#include<stdio.h>
void main()
{
int num=20;
while(num>10)
{
printf("Hello");
}
}
3.#include<stdio.h>
void main()
{
while('A')
printf("Hello");
}
do -while
Syntax:
do
{
statements;
}while(condition);
The do..while loop is similar to the while loop with one
important difference. The body of do...while loop is
executed once, before checking the test expression. Hence,
the do...while loop is executed at least once.
Factorial of the given number
do Declare:i=1,n,fact=1;
{
fact=fact*i;
i++;
}while(i<=n);
Printf(“fact=%d”,fact);
Sum of n numbers
do Declare : int i=1,n,sum=0
{
sum=sum+n;
i++;
}while(i<=n);
Printf(“sum=%d”,sum);
Armstrong number or not
n1=n2=n;
do Declare: n,n1,n2,digit=0,r,sum=0;
{
digit=digit+1;
n1=n1/10;
} while(n1>0);
do
{
r=n2%10;
sum=sum+pow(r,digit);
n2=n2/10;
} while(n2>0);
if(sum==n)
{
Armstrong number;
else
{
Not a Armstrong number;
}
Infinite do-while loop
In do-while loop condition is always true. Then the loop
will run infinite times.
For example:
do
{
printf("This is infinite loop");
}while(1);
To stop an infinite loop, break statement can be used.
For example:
do
{
printf("This loop will run only once");
break;
}while (1);
For loop
Syntax:
for( initialization ; condition ; increment/decrement)
{
body of the loop;
(or)
statements;
}
Factorial of the given number
for(i=1;i<=n;i++) Declare: i,n,fact=1;
{
fact=fact*i;
}
Printf(“fact=%d”,fact);
Sum of n numbers
for(i=1;i<=n;i++) Declare:
i,sum=0,n;
{
sum=sum+i;
}
Printf(“sum=%d”,sum);
Even numbers 1 to n
for(i=1; i<=n; i++)
{
if(i%2==0)
{
printf("%d\n", i);
}
}
for(i=2; i<=n; i+=2)
{
printf("%d\n",i);
}
Odd numbers from 1 to n
for(i=1; i<=n; i++)
{
if(i%2!=0)
{
printf("%d\n", i);
}
}
Reverse order
In while:
While(n>0) declare: n,rev=0,r;
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
Printf(“rev=%d”,rev);
In do-while:
do
{
r=n%10;
rev=rev*10+r;
n=n/10;
}while(n>0);
Printf(“rev=%d”,rev);
In for loop:
for(rev=0;n>0;n=n/10) declare:rev , n , r;
{
r=n%10;
rev=(rev*10)+r;
}
printf(“rev=%d”,rev);
Infinite for loop
In for loop the condition is always true. Then the loop will
run infinite times.
For example:
for (i=0; i>0; i++)
{
printf("This is infinite loop");
}
[Link] the condition part is left blank, the C compiler will
also treat it as true and the loop will run infinite times.
for (i=0; ; i++)
{
printf("This is infinite loop");
}
for (;;)
{
printf("This is infinite loop");
}
Break statements in Infinite loop:
We can use break statement to exit from an infinite loop.
For example:
for (;;)
{
printf("This loop will run only once");
break;
}
[Link](i=3;i<15;i+=3)
printf(“%d”,i);
o/p:
[Link](i=1;i<15;i-=3)
printf(“%d”,i);
o/p:
[Link](i=1;i<15;i*=3)
printf(“%d”,i);
o/p:
4. for(i=0;i<=3; )
printf("%d ",i++);
o/p:
5. for(i=1,j=10;i<6;++i,--j)
printf("%d%d",i,j);
o/p:
[Link](i=3;i<15;i+=3)
printf(“%d”,i);
++i;
o/p:
7. for(i=0,j=2,k=1;i<=4;i++)
printf("%d",i+j+k);
o/p:
8. int i,j=2;
for(i=0;i<=5,j>=0;i++)
printf("%d",i+j);
j--;
o/p:
[Link](i=0,j=0,k=0;i<=5,j<=4,k<=3;i++,++j,k+=2)
printf("%d",i+j+k);
o/p:
10. j=0;
for(i=0;i<=3;++i,i++,++j )
printf("%d %d ",i,j);
o/p:
Answers
1.3 6 9 12
[Link] loop
3.13 9
4.0 1 2 3
5. 1 10 2 9 3 8 4 7 5 6
6. 36912
7. 3 4 5 6 7
8.2 2 2
9.0 4
10.0 0 2 1
Unconditional statements
Unconditional statements in C are the control flow
statements that do not depend on any condition and
execute independently to alter the program’s flow.
goto
break
continue
goto
Transfer the control
label
syntax
Reverse order
z: r=n%10;
rev=rev*10+r;
n=n/10;
if(n>0)
{
goto z;
}
printf(“%d”,rev);
getch();
factorial of the given number
if(n<0)
{
goto z;
}
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“fact=%d”,fact);
z:getch();
break
break is a Keyword
The keyword break is used in while, do-while , for
and Switch statement
Syntax:
break;
break in while loop
Syntax:
while(condition)
{
statements;
if(condition)
{
break;
}
statements;
}
Example : To print 1 to n numbers
while(i<=n) Declare: i=1,n=5;
{
if(i==3)
{
break;
}
printf(“%d”,i);
i++;
}
break in do-while loop
Syntax:
do
{
statements;
if(condition)
{
break;
}
statements;
}while(condition);
Example :
do declare :i=1,n=5;
{
if(i==3)
{
break;
}
printf(“%d”,i);
i++;
}while(i<=n);
break in for loop
Syntax:
for ( initialization ; condition ; increment/decrement)
{
statements;
if(condition)
{
break;
}
statements;
}
Example :
for(i=1;i<=n;i++) Declare: i,n=5;
{
if(i==3)
{
break;
}
Printf(“%d”,i);
}
continue
The keyword continue is used in while , do-while
and for loop.
The purpose of the continue keyword is to skip the
current iteration and continue to the next iteration
Syntax:
statements
continue;
continue in while loop
Syntax:
while(condition)
{
statements;
if(condition)
{
statements;
continue;
}
statements;
}
Example :
while(i<=n) Declare :i=1;n=5
{
if(i==3)
{
i++;
continue;
}
printf(“%d”,i);
i++;
}
continue in do-while loop
Syntax:
do
{
statements;
if(condition)
{
statements;
continue;
}
statements;
}while(condition);
Example :
do declare :i=1,n=5;
{
if(i==3)
{
i++;
continue;
}
printf(“%d”,i);
i++;
}while(i<=n);
continue in for loop
Syntax:
for ( initialization ; condition ; increment/decrement)
{
statements;
if(condition)
{
statements;
continue;
}
statements;
}
In for loop:
for(i=1;i<n;i++) Declare: i,n=5;
{
if(i==3)
{
i++;
continue;
}
Printf(“%d”,i);
}
To print multiplication table
while(i<=10)
{
m=n*i;
if(i==0)
{
i++;
continue;
}
printf("%d*%d=%d\n",n,i,m);
i++;
}
What is o/p of this code?
for (i = 1; i <= 10; i++)
{
if ( i == 6 || i == 9)
{
i++;
continue;
}
printf(“%d”,i);
}
o/p:
for(i=1;i<5;i++)
{
if(i==3)
{
i++;
continue;
}
else
{
printf(“%d”,i);
}
}
o/p:
Nested for loop
Syntax:
for(initialization ; condition ; increment/decrement)
{
for(initialization ; condition ; increment/decrement)
{
statements;
}
statements;
}
[Link] print the output following format
1
22
333
4444
[Link] print the output following format
1
23
4 5 6
7 8 9 10
[Link] print the output following format
****
****
****
****
[Link] print the output following format
*
**
***
****
5. To print the output following format .
1
01
101
0101
[Link] print * in equilateral triangle
*
***
*****
*******
[Link] print pyramid of a numbers
[Link] print prime numbers from 1 to n.
[Link] print perfect numbers from 1 to n.
[Link] print palindrome numbers from 1 to n.
[Link] print Armstrong numbers from 1 to n.
[Link] print strong numbers from 1 to n.
Examples
1. for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d”,i);
}
printf(“\n”);
}
2. for(i=1;i<=4;i++) =>k=0
{
for(j=1;j<=i;j++)
{
printf(“%d”,k=k+1);
}
printf(“\n”);
}
3. for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
printf(“*”);
}
printf(“\n”);
}
4. for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
Printf(“\n”);
}
5. for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d”,(i+j+1)%2);
}
printf(“\n”);
}
[Link](i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(“ “);
}
for(k=1;k<=(2*i)-1;k++)
{
printf(“ * ”);
}
printf(“\n”);
}
7. for(i=1; i<=n; i++)
{
sum = 0;
for(j=1; j<i; j++)
{
if(i % j == 0)
{
sum += j;
}
}
if(sum == i)
{
printf("%d, ", i);
}
}
8. for(i=1;i<=n;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c=c+1;
}
}
if(c==2)
{
printf("%d,",i);
}
}
9. for(i=1;i<=n;i++)
{
t=i;
n1=0;
while(t)
{
r=t%10;
t=t/10;
n1=n1*10+r;
}
if(i==n1)
printf("%d\t",i);
}
10. for(i=1; i<=n; i++)
{
n1=i;
for(sum=0;n1>0;n1=n1/10)
{
r=n1%10;
sum=sum+(r*r*r);
}
if(i==sum)
printf("%d,", i);
}
11. for(i=1; i<=n; i++)
{
n1 = i;
for(sum=0;n1>0;n1=n1/10)
{
fact = 1;
r = n1 % 10;
for( j=1; j<=r; j++)
{
fact = fact * j;
}
sum += fact;
}
if(sum == i)
{
printf("%d, ", i);
}
}