In this tutorial you will learn about the Java Program to Find Sum of first & last digit of a number and its application with practical example.
In this tutorial, we will learn to create a Java Program to Find Sum of first & last digit of a number using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
Java Program to Find Sum of first & last digit of a number.
In this program we will find sum of first and last Digit of a Number .We would first declared and initialized the required variables. Next, we would prompt user to input a digit. Later we will find sum of first and last value of a digit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.Scanner; public class FirstLast { public static int Sum(int number) { // declaring variables int last, first, divisor; int total = 0; int sum = 0; last = number%10; // find total number of digits total = findDigit(number); // calculate divisor value divisor = (int)Math.pow(10, total-1); // find first digit first= number / divisor; // add values sum = first + last; return sum; } // method to find total number of digits public static int findDigit(int number) { int count = 0; while(number!=0) { count++; number = number/10; } return count; } public static void main(String[] args) { // declare variables int number = 0 ,sum; Scanner scan = new Scanner(System.in); // taking value from user.. System.out.print("Enter any number "); number = scan.nextInt(); // find sum of digits of number sum = Sum(number); // display result System.out.println("The sum of first & last"+" digit of the number is "+number+" = "+ sum); scan.close(); } } |
Output
In our program we take a digit from user and add first and last digit of a number and return the sum of first and last digit.
In the above program, we have first declared and initialized a set variables required in the program.
- number=to take value from user.
- first=it will hold digits first number.
- last= it will hold digit last number.
- sum = for counting sum of first and last.
- count = for counting digit number.
In the above program, we have first declared and initialized a set variables required in the program. after that we will take a value from user and find sum of its first and last digits.
How to find the sum of first and last digit of a number in java,
-
-
-
- First of all take a number from user.
- Declaring variable and initialize them.
- First find the last digit simply we use %(modulus) operator.
- The expression is “number %10” gives the last digit of the number.
- For finding the first digit of a number.
- Find the total number of digits in the given number.
- Divide the number by
10^(total_number_of_digits-1)
, it will give the first digit of the number.Add first and the last digit to the sum variable
-
-