Nested looping:
Placing a loop with in another loop is called nested
looping.
Inner loop executes completely once for every outer loop
iteration until the outer loop condition is false.
Syntax:
for( <initialization> ; <condition> ; <incr/decr> )
{
for(
<initialization> ; <condition> ; <incr/decr> )
{
----------------------------------------------
----------------------------------------------
}
}
Specification26:
Accept any number and print its equal single digit number.
Logic:
1. We sum individual digits of a number using n!=0, n%10 and
n=n/10.
2. If sum is not a single digit number then we sore "sum"
into "n".
3. We continue step 1 and 2 until we get a single digit
number (n>9)
Program:
#include<stdio.h>
void main()
{
int n,sum;
printf("Enter any number:");
scanf("%d",&n);
while(n>9)
{
for(sum=0;n!=0;n=n/10)
sum=sum+n%10;
n=sum;
}
printf("Equal single digit number %d",n);
}
Execution:
Enter any number: 6715
Equal single digit number 1
Specification27:
Print the following output.
1 2 3 4 1 2 3 4 1 2 3 4
Logic:
Inner loop is used to print 1 to 4 natural numbers
Outer loop is used to run inner loop for 3 times.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=3;j++)
{
for(i=1;i<=4;i++)
printf("%5d",i);
printf("\n\n");
}
}
Specification28:
Print the following output.
5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1
Logic:
Inner loop is used to print 5 to 1 natural numbers.
Outer loop is used to run the inner loop for 4 times.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=4;j++)
{
for(i=5;i>=1;i--)
printf("%5d",i);
printf("\n\n");
}
}
Specification29:
Print the following output.
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Logic:
Inner loop is used to print 1,2 …. natural numbers.
Outer loop is used to control the inner loop.
When "j" is 1 then for(i=1;i<=1;i++) prints 1
When "j" is 2 then for(i=1;i<=2;i++) prints
1 2
When "j" is 3 then for(i=1;i<=3;i++) prints
1 2
3
It happens for 5 times to print 5 rows.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=j;i++)
printf("%5d",i);
printf("\n\n");
}
}
Specification30:
Print the following output
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=5;j>=1;j--)
{
for(i=j;i>=1;i--)
printf("%5d",i);
printf("\n");
}
for(j=2;j<=5;j++)
{
for(i=j;i>=1;i--)
printf("%5d",i);
printf("\n");
}
}
Specification31:
Print
the following output.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=j;i++)
printf("%5d",i);
printf("\n");
}
for(j=4;j>=1;j--)
{
for(i=1;i<=j;i++)
printf("%5d",i);
printf("\n");
}
}
Specification32:
Print the following output
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=5;j>=1;j--)
{
for(i=1;i<=j;i++)
printf("%5d",i);
printf("\n");
}
for(j=2;j<=5;j++)
{
for(i=1;i<=j;i++)
printf("%5d",i);
printf("\n");
}
}
Specification33:
Print the following output.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=j;i>=1;i--)
printf("%5d",i);
printf("\n");
}
for(j=4;j>=1;j--)
{
for(i=j;i>=1;i--)
printf("%5d",i);
printf("\n");
}
}
Specification34:
Print
the following output.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=j;i++)
printf(" * ");
printf("\n");
}
for(j=4;j>=1;j--)
{
for(i=1;i<=j;i++)
printf(" * ");
printf("\n");
}
}
Specification35:
Print the following output
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=5;j>=1;j--)
{
for(i=1;i<=j;i++)
printf(" * ");
printf("\n");
}
for(j=2;j<=5;j++)
{
for(i=1;i<=j;i++)
printf(" * ");
printf("\n");
}
}
Specification36:
Print the following output.
Logic:
It needs two inner loops one is to print spaces and another
is to print terms.
Outer loop is used to control both the inner loops.
1
1 2 1 2
3 1 2
3 4 1 2 3
4 5
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=5-j;i++)
printf(" ");
/* 5 spaces */
for(i=1;i<=j;i++)
printf("%5d",i);
printf("\n");
}
}
Specification37:
1
2 1 3 2
1 4 3
2 1 5 4 3
2 1
Print the following output
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=5-j;i++)
printf(" ");
/* 5 spaces */
for(i=j;i>=1;i--)
printf("%5d",i);
printf("\n");
}
}
Specification38:
Print the following output:
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=5-j;i++)
printf(" ");
/* 5 spaces */
for(i=1;i<=j;i++)
printf("%5d",i);
printf("\n");
}
for(j=4;j>=1;j--)
{
for(i=1;i<=5-j;i++)
printf(" "); /* 5 spaces */
for(i=1;i<=j;i++)
printf("%5d",i);
printf("\n");
}
}
Specification39:
Print the following output.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=5-j;i++)
printf(" ");
/* 5 spaces */
for(i=j;i>=1;i--)
printf("%5d",i);
printf("\n");
}
for(j=4;j>=1;j--)
{
for(i=1;i<=5-j;i++)
printf(" "); /* 5 spaces */
for(i=j;i>=1;i--)
printf("%5d",i);
printf("\n");
}
}
Specification40:
Print the
following output.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;i++)
{
for(i=1;i<=5-j;i++)
printf(" "); /*5 spaces */
for(i=1;i<=j;i++)
printf("%5d",i);
for(i=j=1;i>=1;i--)
printf("%5d",i);
printf("\n");
}
}
Specification41:
Print the
following output.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;i++)
{
for(i=1;i<=5-j;i++)
printf(" "); /*5 spaces */
for(i=j;i>=1;i--)
printf("%5d",i);
for(i=2;i<=j;i++)
printf("%5d",i);
printf("\n");
}
}
Specification42:
Print the following output.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=5-j;i++)
printf(" ");
for(i=1;i<=j;i++)
printf("%5d",i);
for(i=j-1;i>=1;i--)
prinf("%5d",i);
printf("\n");
}
for(j=4;j<>=1;j--)
{
for(i=1;i<=5-j;i++)
printf(" ");
for(i=1;i<=j;i++)
printf("%5d",i);
for(i=j-1;i>=1;i--)
prinf("%5d",i);
printf("\n");
}
}
Specification43:
Print the following output.
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=5-j;i++)
printf(" ");
for(i=j;i>=1;i--)
printf("%5d",i);
for(i=2;i<=j;i++)
prinf("%5d",i);
printf("\n");
}
for(j=4;j<>=1;j--)
{
for(i=1;i<=5-j;i++)
printf(" ");
for(i=j;i>=1;i--)
printf("%5d",i);
for(i=2;i<=j;i++)
prinf("%5d",i);
printf("\n");
}
}
Specification44:
Print the following output.
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=5-j;i++)
printf(" "); /* 5 spaces */
for(i=1;i<=j;i++)
printf("* "); /* 4 spaces */
for(i=1;i<=j-1;i++)
printf("* ");
printf("\n");
}
for(j=1;j<=4;j++)
{
for(i=1;i<=5-j;i++)
printf(" "); /* 5 spaces */
for(i=1;i<=j;i++)
printf("* "); /* 4 spaces */
for(i=1;i<=j-1;i++)
printf("* ");
printf("\n");
}
}
Specification:
1 2 2 3 3 3 4 4 4
4 5 5 5
5 5
Print the following output.
Logic:
Outer loop is used to run the inner loop for "j"
number of times.
Inner loop runs the for "j" times and prints "j"
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=j;i++)
printf("%5d",j);
printf("\n");
}
}
Specification:
1 2
2 2
3 3 3
3 3 4 4
4 4 4
4 4 5 5
5 5 5
5 5 5
5 4 4
4 4 4
4 4
3 3 3
3 3
2 2 2 1
Print the following output.
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=5-j;i++)
printf(" ");
/* 5 spaces */
for(i=1;i<=j;i++)
printf("%5d",j);
for(i=1;i<=j-1;i++)
printf("%5d",i);
printf("\n");
}
for(j=4;j>=1;j--)
{
for(i=1;i<=5-j;i++)
printf(" ");
/* 5 spaces */
for(i=1;i<=j;i++)
printf("%5d",j);
for(i=1;i<=j-1;i++)
printf("%5d",i);
printf("\n");
}
}
Specification45:
Print the following output.
1 2 3
4 5 4
3 2 1 1 2 3
4 4 3
2 1 1 2 3 3 2
1 1 2 2 1 1
1
Program:
#include<stdio.h>
void main()
{
int i,j;
for(j=5;j>=1;j--)
{
for(i=1;i<=j;i++)
printf("%5d",i);
for(i=1;i<=2*(5-j)-1;i++)
printf(" ");
/* 5 spaces */
for(i=j;i>=1;i--)
if(i!=5)
printf("%5d",i);
printf("\n");
}
}
Specification46:
A B C
D E F
E D C
B A A B C
D E E
D C B
A A B C
D D C
B A A B C C B
A A B
B A A A
Print the following output.
#include<stdio.h>
void main()
{
char i,j;
int k;
for(j=’F’;j>=’A’;j--)
{
for(i=’A’;i<=j;i++)
printf("%c",i);
for(k=1;k<=2*(‘F’-j)-1;k++)
printf("
");
for(i=j;i>=’A’;i--)
if(i!=’F’)
printf("%c",i);
printf("\n");
}
}
Specification47:
Print the Floyd’s triangle up to ‘n’ rows.
Logic:
Outer loop is used to control the inner loop
Inner loop is used to print rows
Variable ‘x’ is used to print the actual output.
Program:
#include<stdio.h>
void main()
{
int i,j,k,n;
clarscr();
printf("How many rows:");
scanf("%d",&n);
x=1;
for(j=1;j<=n;j++)
{
for(i=1;i<=j;i++)
{
printf("%5d",x);
x++;
}
printf("\n");
}
}
Execution: How many
rows: 4 1 2 3 4 5
6 7 8
9 10
Spcification48:
Accept any integer and print whether the number is a strong
number or not.
Logic:
If the sum of factorials of individual digits is equal to
the given number then the number is called a strong number.
Example:
1!
+ 4! + 5! = 145 1
+ 1x2x3x4 +
1x2x3x4x5 = 145 1
+ 24 + 120 = 145
Program:
#include<stdio.h>
void main()
{
int i,fact,sum,temp,n;
printf("Enter an
integer:");
scanf("%d",&n);
temp=n;
for(sum=0;n!=0;n=n/10)
{
for(fact=1,i=1;i<=n%10;i++)
fact=fact*i;
sum=sum+fact;
}
if(sum==temp)
printf("String
number");
else
printf("Not a
string number");
}
Execution:
Enter an integer: 145
Strong number
Example explained:
The outer loop n!=0, n%10 and n=n/10 extracts individual
digits and sums the factorials calculated by the inner loop.
The inner loop calculates the factorials of individual
digits (n%10).
Specification49:
Accept the limit and print the strong numbers from 1 to the
given limit.
Program:
#include<stdio.h>
void main()
{
int i,fact,sum,temp,n,x,limit;
printf("Enter the limit:");
scanf("%d",&limit);
for(x=1;x<=limit;x++)
{
n=x;
for(sum=0;n!=0;n=n/10)
{
for(fact=1,i=1;i<=n%10;i++)
fact=fact*i;
sum=sum+fact;
}
if(sum==x)
printf("%5d",x);
}
}
Execution:
Enter the limit: 1000
1 2 145
Example explained:
The outer loop x=1, x<=limit and x++ gives number by
number to the inner loop
The inner loop tests the number and prints if the number is
a strong number.
Specification50:
Print the prime numbers from 1 to the given limit.
Program:
#include<stdio.h>
void main()
{
int i,n,limit,count;
printf("Enter the limit:");
scanf("%d",&limit);
for(n=1;n<=limit;n++)
{
for(count=0,i=1;i<=n;i++)
if(n%i==0)
count++;
if(count==2)
printf("%5d",n);
}
}
Execution:
Enter the limit: 20
2 3 5 7 11 13 17 19
Example explained:
The outer loop generates numbers from 1 to the limit using
n=1, n<=limit and n++.
The inner loop counts the factors of "n" and
prints if they are 2
Specification51:
Print the perfect numbers from 1 to the given limit.
Program:
#include<stdio.h>
void main()
{
int i,n,sum,limit;
printf("Enter the limit:");
scanf("%d",&limit);
for(n=1;n<=limit;n++)
{
for(sum=0,i=1;i<n;i++)
if(n%i==0)
sum=sum+i; /* sum of
factors other than the given number */
if(n==sum)
printf("%5d",n);
}
}
Execution:
Enter the limit: 1000
6 28 496
Specification52:
Print the Armstrong numbers from 1 to the given limit.
Program:
#include<stdio.h>
void main()
{
int n,x,sum,limit;
printf("Enter the limit:");
scanf("%d",&limit);
for(n=1;n<=limit;n++)
{
x=n;
for(sum=0;x!=0;x=x/10)
sum=sum+(x%10)*(x%10)*(x%10);
if(sum==n)
printf("%5d",n);
}
}
Execution:
Enter the limit: 1000
1 153 370 371 407
Specification53:
Print the palindrome numbers from 1 to the given limit.
Program:
#include<stdio.h>
void main()
{
int limit,n,x,rev;
printf("Enter the limit:");
scanf("%d",&limit);
for(n=1;n<=limit;n++)
{
x=n;
for(rev=0;x!=0;x=x/10)
rev=rev*10+x%10;
if(rev==n)
printf("%5d",n);
}
}
Execution:
Enter the limit: 100
1 2 3 4
5 6 7
8 9 11
22 33 44
55 66 77
88 99
Points to remember:
1. If the for loop is terminated with a semi colon (;) then
it iterates within conditional statement, can’t control the body of loop.
Example1:
#include<stdio.h>
void main()
{
int i;
for(i=1;i<=10;i++);
printf("Hello
world");
}
Output:
Hello world
Example explained:
Generally we expect printing of "Hello world" for
10 times but prints only once.
Because for is terminated with a semi colon, iterates within
conditional statement for ten times and finally printf("Hello World");
executes for once.
Example2:
#include<stdio.h>
void main()
{
int i;
for(i=1;i<=5;i++);
printf("%d",i);
}
Output:
6
Example explained:
The value of "i" after the completion of loop is 6
The printf("%d",i); prints the value of "i"
after the completion of loop.
2. Without condition
for loop behaves like an infinite loop.
Example:
#include<stdio.h>
void main()
{
for(;;)
printf("Hello
world");
}
Output:
Prints "Hello world" continuously until the
program is terminated using ctrl+break.
Short cut operators:
These are the arithmetic assigning operators.
Operator |
Example |
Meaning |
+= |
a+=b |
a=a+b |
-= |
a-=b |
a=a-b |
*= |
a*=b |
a=a*b |
/= |
a/=b |
a=a/b |
0 comments:
Post a Comment