C Program to Find Last Digit of a Number

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The last digit of a number in C can be calculated by taking the modulus of a given number by 10, and the operator that is used to find the last of a given number is the "%" operator.

Introduction

Let's suppose you have a number n, and you need to find the last digit of a given number. How you can find it? The most efficient way to find out the last digit of a given number in C language using the modulus operator.

Let's understand what is modulus operator( "%") is. This operator takes two operands, the first operator is the dividend, and the second operand is the divisor and returns the remainder after dividing the dividend by the divisor.

For example, the answer of 10%5 is 0. Because after dividing the 10 by 5, we will get the remainder as 0.

C Program to Find the Last Digit of a Number

Let's understand how to find the last digit of a number using a C program.

Output:

C Program to Find the Last Digit of a Number Using Function

Let's understand how to find the last digit of a number using the function in the C language.

In the below program, we are finding the last digit of the given number using the method last digit().

Output:

How Does This Program Work?

If we divide any number by 10, the remainder either will be 0 or in the range [0-9], or also after dividing the number by 10, each digit moves one place to the right to make it 10 times smaller.

In the below image, we are dividing the given number ie. 868 by 10, and the remainder we get after performing this operation is the last digit of the number.

dividing-the-number

Hence, the remainder of the number after dividing by 10 will be the last digit. If the number is too large that cannot be stored in any kind of datatype even in the long datatype. We will store the number in the String datatype and iterate the String till the end, and get the last digit of the given large number.

Conclusion

  • The modulus operator takes two values.
  • We can find the last digit of a given number by taking the modulus with 10.