Find Last Digit of A Number

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 16:- Find Last Digit of A Number

To find the last digit of a number, you can use the modulo operator % in Java. The modulo operator returns the remainder when one number is divided by another. In this case, we'll use it to find the remainder when the given number is divided by 10, which gives us the last digit.

Here's a Java program to find the last digit of a given number:

javaCopy code
import java.util.Scanner; public class LastDigitOfNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); // Find the last digit int lastDigit = number % 10; System.out.println("The last digit of " + number + " is: " + lastDigit); scanner.close(); } }

Example output:

yamlCopy code
Enter a number: 3568 The last digit of 3568 is: 8

In the program above, we take the user input, store it in the variable number, and then find the last digit using the expression number % 10. The result is stored in the variable lastDigit, which is then printed to the console. The % 10 operation effectively gives us the remainder when number is divided by 10, which corresponds to the last digit of the number.

 

Disclaimer:-

Under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use.

 

4. Operators

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?