Saturday, April 12, 2014

Printing all even numbers from a user provided range


In our last post we had shown you how to directly print all even numbers from a static range. If you have not read our previous post than we recommend to read it first by clicking the link printing even numbers using for loop before reading further.

In today's post we are going to show you the C programming code to print all even numbers from any range of numbers provided by the user.  The code is given below :


   #include<stdio.h>
   #include<conio.h>

   int main()
   {
       int i,start,end;
       clrscr();
    
       printf("Enter starting number : ");
       scanf("%d",&start);
       printf("Enter ending number : ");
       scanf("%d",&end);

       for (i=start;i<=end;i++)
       {
           if(i%2==0)
           {
               printf(" %d\n",i);
           }
       }
    
       getch();
       return 0;

   }

Now this is the perfect code to solve our even number problem as you can see inside the loop it checks the " i " 's value each and every time it loops to find whether the current value of i returns the remainder as zero if divided by 2 if it does then its an even number.

Hope you liked our post if you have any suggestions or query please comment.

Happy Programming !

No comments :

Post a Comment