Wednesday 10 October 2012

Function024

Input a number and a position from user. Find out the digit in that position counted from right to left. Print appropriate error message if the position is invalid.

#include <stdio.h>
#include <conio.h>
int position ( int, int );
void main ( )
{                        
        int      n, p, d;
        clrscr ( );
        printf("\n\n\n\t\t Enter a number (less than 32767)..:"); scanf("%d",&n);
        printf("\n\t\t Enter the position..:"); scanf("%d",&p);
                                 
        d = position ( n, p );
        if ( d == -1 )
        {                                               
                   printf("\n\n\n\t\t Invalid position.");
        }                                              
        else           
        {                                            
                   printf("\n\n\n\t\t Required digit is .:%d",d);
        }
        getch ( );
}                             
                                      
int position ( int n,int d)
{                       
        int     i = 1, r;
        if ( d <= 0 )
                   return -1;
        while ( n > 0 )
        {                        
                     r = n % 10;
                     if ( i == d )
                              return r;
                     i++;
                     n = n / 10;
        }
        return -1;
}
Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...