Tutorial 3 : Iterative Looping (WHILE and FOR Loop)                             Date : 22nd  Jan, 07


Fundamental of WHILE and FOR loop:

Control Flow in the WHILE Loop:

 

step1:    initialization of loop counter         i=1;     //Outside of Loop

step2:    condition for entering loop           while (condition)

                                                                   {           //body of the loop

                                                                       ................               

 step3:    body of the loop                              ....................

                                                                       i = i + 1; (step 4)

                                                                   }   //End of WHILE loop          

step4:    increment (decrement) loop counter (generally in the body of the loop)



Illustrative Example: Tutorial 3A:


Print first n consecutive integer numbers using WHILE loop.

 

#include <stdio.h>

void main()
{
   int n,i;
   printf("Enter value of n :\n");
   scanf("%d",&n);

   i=1; //Initialization while loop counter
   while (i<n) //condition to enter the loop
   {
       printf("\n%d\n",i);
       i=i+1; // Increment of loop counter
   }
}



Assign. 3A:

Modify the program in Tutorial 3A to print first n numbers of the following sequence.

1, 3, 6, 10, 15, ....


Control Flow in the FOR Loop:

for (initialization; condition; increment/decrement)

{                                                                 

Text Box: Initialization
     .................................                             

Text Box: yes
    // Body of the loopText Box: Condition checking
Text Box: Body of the loop
Text Box: Increment

    ....................................

}Text Box: Exit Loop
Text Box: no



Illustrative Example: Tutorial 3B:

Print first n consecutive integer numbers using FOR loop


#include <stdio.h>

void main()
{
    int n, i;
    printf("Enter value of n :\n");
    scanf("%d",&n);

    for (i=1;i<=n;i++)
   {
      printf("\n%d\n",i);
   }
}



Assign. 3B:

Modify the program in Tutorial 3B to print first n consecutive even numbers.



Tutorial Example:Tutorial 3C:


        Convert a given binary number into equivalent decimal number and vice versa.


// Use "cc -lm <programname.c>" for compilation

// This part is for converting binary to decimal.

#include <stdio.h>
#include <math.h>

void main()
{
   long int bin_num;
   int sum,pos;
   printf("Enter a binary number (maximum 8 digits) :");
   scanf("%ld",&bin_num);

   sum=0;
   pos=0;
   while ((bin_num/10) > 0)
  {
     sum=sum+(bin_num%10)*pow(2,pos);
     pos=pos+1;
     bin_num=bin_num/10;
  }
  sum=sum+bin_num*pow(2,pos);
  printf("The Decimal equivalent is = %d\n",sum);
}


// This part is for converting decimal to binary.

#include <stdio.h>
#include <math.h>

void main()
{

   int dec_num;
   int pos;
   long int sum;
   printf("Enter a Decimal number (maximum 512) :");
   scanf("%d",&dec_num);

   sum=0;
   pos=0;
   while ((dec_num/2) > 0)
   {
      sum=sum+(dec_num%2)*(long int)pow(10,pos);
      pos=pos+1;
     dec_num=dec_num/2;
  }
  sum=sum+dec_num*(long int)pow(10,pos);
  printf("The Binary equivalent is = %d\n",sum);
}



Assign. 3C:


Input a number (IP_num) and its base(IP_base). Input another base (OP_base) value. Convert the number to the second base.

For example: Input IP_num=184, IP_base=10, OP_base=8

Oput put will be = 270 (in base 8).

Hint: First converts the number (IP_num) from (IP_base) to the decimal (10) then convert into OP_base.

Test Run:         I/P (s)                                        O/P (s)

i) IP_num=184, IP_base=10, Op_base=8    :      270                           

ii)IP_num=1000, IP_base=2, Op_base=8    :      20

iii)IP_num=126, IP_base=6, Op_base=3    :     2000

iv)IP_num=12121, IP_base=3, Op_base=7    :   304



Tutorial Example: Tutorial 3D:


       Print all the triod numbers between 100 and 1000.

       Definition: Triod Number: A number is a triod number if sum of cubic of their digits are 

                         equal to the number.

                         For example: 153 is a triod number if  13 + 53 + 33 = 153.


#include <stdio.h>
#include <math.h>

void main()
{

   int sum,i,num;

   for (i=100;i<1000;i++)
  {
    sum=0;
    num=i;
   while ((num/10) > 0)
   {
      sum=sum+(int)pow((num%10),3);
      num=num/10;
   }
   sum=sum+(int)pow(num,3);
   if (sum==i)
   printf("\n%d\n",i);
}


Sample Output:

153

370

371

407



Assign 3D:


      Print the sum of digits of a given positive decimal number (ip_NUM).

     For example: ip_NUM=5348, O/P would be (5+3+4+8) = 20.

     Test Run:          I/P (s)            O/P (s)

          i)                     3456                18

          ii)                    6759                27

         iii)                    100                   1

         iv)                      5                     5