C Program to Print the Rightmost Digit of a Given Number

Write a program to print the rightmost ( last ) digit of a given number in C.

For example,

Input
821309
Output
9

The problem is very simple. The right-most digit of a number is basically the remainder of that number with 10. We can use the ‘%’ operator to get the remainder.

#include <stdio.h>

int main() {

	int n;
	printf("Enter a Number: ");
	scanf("%d", &n);
	printf("Right-most Digit of %d = %d", n, n % 10);
	
}

Output

Enter a Number: 98765
Right-most Digit of 98765 = 5

Now, you may need to find the right-most digit of the given number without using the ‘%’ operator. In this case, we will use the following formula to calculate the remainder.

Let N be the given number. We can write N as
N = 10 * Quotient + Remainder
Remainder = N -  10 * Quotient
The quotient is given by N / 10
#include <stdio.h>

int main() {

	int n,r,q;
	printf("Enter a Number: ");
	scanf("%d", &n);
	q = n / 10; // quotient
	r = n - q * 10; // remainder
	printf("Right-most Digit of %d = %d", n, r);
	
}

Output

Enter a Number: 81291322
Right-most Digit of 81291322 = 2

Read

Leave a Comment

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