Program that extracts a digit from an integer


    # include <iostream.h>
    # include<conio.h>
    int digit (long, int);
    void main( )
    {
        int n , k;
        cout<<"\n Integer : " ;
        cin>>n;
                         do
                          {
                cout << "\n Digit :";
                cin >> k;
                cout<<"\nDigit number  " << k <<" of  "<< n
                                <<" is "<< digit(n, k) << endl;
                        }while (k > 0);       
               }
              int digit (long n, int k)
             {    
                for( int i = 0; i < k; i++)
                         n /= 10;                      //remove right-most digit
                return (n % 10);
              }

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|