Program to print the right most digit in a number


#include<iostream>
using namespace std;
int main()
{
          int a;                         // declaring the variable
          cout<<"enter the number: ";
          cin>> a;
          a=a%10;                           // modulus operator
          cout<< "\nthe right most number is " << a;
                                                                    // print the right most digit
          return 0;
}

                       
                   % operator is used to get the right most digit of any number by 10 (which is the remainder of that number).

Note: 
  • The number should be within the range of int data type.
  • modulus operator doesn't support float numbers.
code:
CLICK HERE
output :
CLICK HERE

               Suppose if u want to get the left most digit.. ??
Pseudocode to print the left most digit :

loop:
  • Take a variable N=N/10
  • if(N>9) goto loop
  • else print the variable N           

                     Consider N=125, as N>9 it again start the loop. For 2nd time N=12 , 3rd time N=1, this time N<9 so the loop stops and print the right most digit.


Try it urself:
Program to print the 3rd digit (from right) in a number, greater than 100.