-->
Showing posts with label program to display even positioned digits.. Show all posts
Showing posts with label program to display even positioned digits.. Show all posts

Friday, June 3, 2016

program to get/display digits which are even present in a number.

//program to get/display digits which are even position present in a number.
#include<stdio.h>
#include<conio.h>
void main()
{
   int n,rem,count=0;
   printf("enter a number for 'n'\n");
  scanf("%d",&n);
  while(n!=0)
    {
      rem=n%10;
       count++;
       if(count%2==0)
        {
          printf("%d",rem);
          }
       n=n/10;    
   }
getch();
}
--------------------------------------------------------------------------------------------------
logics in mind:-
>first we enter a number(for 'n').
->If we have number 123 then its even positioned digits are 2(from both side). It means, first we have to get 3 then 2 and then 1.        
                  ->For this, 
                 ->we have to divide by 10 to get last digit. 
                 ->we also use 'count' to to know position  of digits.
                 ->we divide that count by 2 to get remainder.If count is 2 then we understand that the digit is in even position and if not then that is not.
                 ->Then we display that.
                -> to get second digit, we get first 12 and for this , we use 123/10.It is done to get integer                           part  only.
->We repeat this until the value reaches 0. We use loop for this as shown above in the code.