Pages

Friday, July 19, 2013

Write a program that Print Least Significant Digit in C++

This program print the first digit of an integer read from the keyboard

Souce Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <iostream.h>
  #include <conio.h>
  using namespace std;
  // Prototype Declarations
  
  int firstDigit (int num);
  int main ()
  {
     cout << " Enter an integer: ";
     int number;
     cin  >> number;
     int  digit = firstDigit (number);
     cout << " Least Significant digit is: "<< digit << endl;    
     getch();
      return 0;
                 }// main
   /* ============= firstDigit =================
      This function extracts the least significant 
      digit of an integer   
      pre  num contains an integer
      post Returned least significant digit        */              
      int firstDigit (int num)
      {
          return (num % 10);

                            } // firstDigit

Output:


 Enter an integer: 345
 Least Significant digit is: 5

No comments:

Post a Comment