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.
#include <stdio.h>
#include <math.h>
/*Function to return the digit of n-th position of num. Position starts from 0*/
int getdigit(int num, int n)
{
int r, t1, t2;
t1 = pow(10, n+1);
r = num % t1;
if (n > 0)
{
t2 = pow(10, n);
r = r / t2;
}
return r;
}
void main()
{
int num = 0, pos;
printf("Enter an integer: ");
scanf("%d", &num);
printf("Enter a position: ");
scanf("%d", &pos);
printf("%dth digit of %d is %d.\n", pos, num, getdigit(num, pos));
}Output: (Position starts from 0).
Enter an integer: 345672
Enter a position: 3
3th digit of 345672 is 5.
The post C Program to Get a Digit of Any Position of a Number appeared first on QnA Plus.