C Program to Get a Digit of Any Position of a Number

This C program finds the digit of specified position of a number. The numbering starts from 0. The least significant digit of a number is 0-th digit. For example 0th digit of 2341238 is 8 and 3-rd digit is 1.

Output: (Position starts from 0).
Enter an integer: 345672
Enter a position: 3
3th digit of 345672 is 5.

The function getdigit() uses C integer operations, modulo and dev, to find the digit of a particular location of a number. ItĀ first does the modulo operation on the input number by 10, if position is 0, 100 if position is 1, 1000 if position is 2 and so on.
In our above example, we want to find 3rd digit of 345672, so the getdigit() function will do modulo operation with 345672 with 10000. The result of the modulo operation will be 5672, the intended digit will become the right most in the result. After this, the function does div operation with the result (5672 here) by 1000, same number of 0s as intended position after 1.
The result of the div operation will become the digit of the intended position.

Leave a Reply

Your email address will not be published. Required fields are marked *