Wednesday, 8 January 2014

C Programming Language Tutorial IX- Nested while loop

Nested while loop
Note: Before beginning with this lesson you should see the previous lessons of C Programming Language Tutorial VII. This is helping you to understand the concept of nested loop.
Nested Loop:
In case of nested loop, you have more than one loop. The structure of nested loop is loop within loop. If for loop is within while loop or for loop within for loop or while loop within for loop and vice versa known as nested loop.
In this case first outer condition is evaluated and then first inner loop gets execute till the inner condition is true. After termination of inner loop, outer loop gets executed and inner loop get reset this whole cycle is going on till the outer condition is true. Let us clear this concept by following syntax:
Nested while loop
            while(outer condition)
             {
                while(inner condition)
                {
                    -------
                   -------
                    -------
                }
             }
While creating program of nested loop you must be think of each and every step.
e.g. – what is outer condition?
          What is inner condition?
           How to reset inner loop?            
Let us see in details, generally outer condition is useful only to jump value step by step. i.e. if num=1 then it will 2,3,4……. till given range. Use inner condition same as you had used in case of while loop. Reset loop means to set any variable of inner loop at previous initialized value.
You have to study following program to grip concept of working of nested loop –
//nested while loop
#include<stdio.h>
#include<conio.h>
void main()
{
  int i=1,j=1;        //declaration and initialization of variables
  clrscr();
   while(i<=5)     //outer condition
   {
     while(j<=3)   //inner condition
     {
        printf("\ni = %d \t j = %d",i,j);    //simply print value of i and j
        j++;           //increment to j for getting termination of inner loop
      }
       printf("\n------------------------------------");
       j = 1;          //reset inner loop
       i++;           // increment to i for getting termination of outer loop
    }
     getch();
}         
o/p:
i = 1         j = 1
i = 1         j = 2
i = 1         j = 3
i = 2         j = 1
i = 2         j = 2
i = 2         j = 3
i = 3         j = 1
i = 3         j = 2
i = 3         j = 3
i = 4         j = 1
i = 4         j = 2
i = 4         j = 3
i = 5         j = 1
i = 5         j = 2
i = 5         j = 3
You have created program to print multiplication table of given number. But if you have a set or range of numbers and want to print multiplication table of each value then you can simply apply nested loop.
Let us see:
//multiplication table
#include<stdio.h>
#include<conio.h>
void main()
{
  int num=1,cnt=1,ans,range;
  clrscr();
  printf("\nEnter any range for table: "); 
  scanf("%d",&range);
 while(num<=range)  //outer loop is used to jump one by one number till range
 {
   printf("\nMultiplication Table of %d : \n",num);
//you can find that inner loop is same as you have used while printing multiplication table of single number         
  while(cnt<=10)           
   {
      ans = num * cnt;        //process 
      printf("\n%d * %-2d = %d",num,cnt,ans);  //It will print following mentioned output
      cnt++;
   }
   cnt=1;   //after printing table for one time you must need to reset counter for next set of numbers
   num++; //it will give you next number
}
   getch();
}
o/p:
Enter any range for table: 3
Multiplication Table of 1 :
1 * 1  = 1
1 * 2  = 2
1 * 3  = 3
1 * 4  = 4
1 * 5  = 5
1 * 6  = 6
1 * 7  = 7
1 * 8  = 8
1 * 9  = 9
1 * 10 = 10
Multiplication Table of 2 :
2 * 1  = 2
2 * 2  = 4
2 * 3  = 6
2 * 4  = 8
2 * 5  = 10
2 * 6  = 12
2 * 7  = 14
2 * 8  = 16
2 * 9  = 18
2 * 10 = 20
Multiplication Table of 3 :
3 * 1  = 3
3 * 2  = 6
3 * 3  = 9
3 * 4  = 12
3 * 5  = 15
3 * 6  = 18
3 * 7  = 21
3 * 8  = 24
3 * 9  = 27
3 * 10 = 30
Assignment: From above program you can be modify program of factorial by using range and print sum of all factorial.
In previous lesson you know that how to check whether given number is palindrome or not palindrome. Now you can apply range to find all palindrome numbers from given range.
Let us see the program –
//program to print all palindrome numbers from given range
#include<stdio.h>
#include<conio.h>
void main()
{
 int no=100,rem,rev=0,temp,range;      //number starts from zero
 clrscr();
 printf("\nEnter any range: ");
 scanf("%d",&range);              //range must be greater than 100
while(no<=range)                   //number goes from 100 till given range
{
 temp=no;      //assigns value of number to temp
 while(temp>0)   //temp is used here because you will need current value of number in outer loop
 {
  rem = temp % 10;
  temp = temp / 10;
  rev = rem + rev * 10;
 }
 if(no == rev)   //check whether the value of no is equal to rev
  printf("\nPalindrome number = %d",no);
rev=0;  //for each number rev=0 again
no++;  //increment to get next number
}
 getch();
}
o/p:
Enter any range: 200
Palindrome number = 101
Palindrome number = 111
Palindrome number = 121
Palindrome number = 131
Palindrome number = 141
Palindrome number = 151
Palindrome number = 161
Palindrome number = 171
Palindrome number = 181
Palindrome number = 191

Assignment : Create a program to print all prime numbers by setting start range and end range.

No comments:

Post a Comment