C Program to Find Last Digit Of a Number

In this article, we are going to discuss how to get the last digit from a given number. So basically if we want the last digit of a given number then we have to divide that number by 10 after that we get a remainder and that remainder is our final answer means that the remainder is our last digit.

C Program to Find Last Digit Of a Number

For example-

Input-

So first step we are going to take input from the user in the form of the number, suppose we take 22456.

Logic-

So to calculate the last digit of a given number we required the formula that is :
last_digit = number % 10

Output-

according to our input, we get output as
Enter the number:22456
The last digit of 22456 is 6

So let’s try to implement these formulas in our program, so let’s start!

Method 1:

// get the last digit from the given number

#include<stdio.h>
int main()
{
int num, last_digit;
printf(“Enter the number :”);
scanf(“%d”,&num);
last_digit = num % 10;
printf(“The last digit of %d is %d”,num, last_digit);
return 0;
}

Output-

Enter the number:22456
The last digit of 22456 is 6

Method 2:

#include<stdio.h>
int last_digit(int num){
return (num%10);
}
int main()
{
int num;
printf(“Enter the number :”);
scanf(“%d”,&num);
printf(“The last digit of given number is %d”, last_digit(num));
return 0;
}

Output-

Enter the number:12345354
The last digit of a given number is 4

From the above program, we can see that,

we simply use user define a function in it.

In that we give the last_digit name to the function after that we simply pass the value of the number in the parameters and then we simply apply the formula.

then after that, we simply use the main function.

So these are the two methods to find the last digit of the given number, I hope these concepts are clear to you now, you can explore this concept by putting new values in it. So for now keep learning and keep exploring.