CS 11001 Programming and Data Structure

(Spring Semester 2007)

Lab
Niloy Ganguly (NG) niloy@cse.iitkgp.ernet.in

Teaching Assistant

Arijit Sur, Dhiman Saha, Tanmoy Dey, Tirthankar Dasgupta, Chaitnya K. K., Piseke Dayakar, Gaurav Garg


Tutorial 3 : Iterative Looping (WHILE and FOR Loop)      


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:

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
   }
}


Control Flow in the FOR Loop:

for (initialization; condition; increment/decrement)

{                                                                 

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

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

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

}

Illustrative Example:  

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);
   }
}


Tutorial Example:

1.    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);
}

2.    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


Assignment 3:


a) 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


b) i)    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


  ii)   Print the sunny number of a given positive decimal number (ip_NUM).

         Definition: Sunny Number : Sunny number is a single digit number which can be

         obtained by repetitive summation of digits of a decimal number.

         For example, for 5348, sum of digit=  (5+3+4+8) = 20,   sunny (5348)=2+0=2

         again,  for 3989, 9+9+8+3=29;   2+9=11;    1+1=2,         sunny(3989) = 2

         sunny(5)=5, sunny(53)=8 etc.        

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

                   i)                   2456                 8

                  ii)                   6759                 9

                 iii)                    100                  1

                 iv)                      5                    5