Sum of First and Last Digit of a Number in Java

Previously we have written a Java program to find the sum of digits of the given number. Now in this post, we will write a Java program to find the sum of the first and last digit of a number.

Example:-
Number = 12345
The first digit = 1, last digit = 5
Sum of first and last digits = 1 + 5 = 6

Procedure to find the sum of first and last digit of a number in java,

  • Take a number
  • Declare sum variable and initialize it with 0
  • Find the last digit. To find the last digit we can use % operator. The expression number%10 gives the last digit of the number.
  • Find the first digit of the number. For this
    • 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.

How to find or get the last digit of a number in java?

You can easily find the last digit of a number in Java using % operator. Any number%10 gives the last digit of the number. Below code in Java is used to get the last digit of a number,

int number = 1234;
int last_digit = number % 10;

So, last_digit = 1234 % 10 = 4

How to find or get the first digit of a number in Java?

To find or get the first digit of a number in Java, First of all, we need to count the total number of digits in the given number.

int number = 1234;
So, total_digits = 4;

After calculating number of digits, calculate the power of 10 ^ (total_digits-1)

int divisor = (int) Math.pow(10, total_digits-1);
int first_digit = number / divisor;

So, divisor = (int)Math.pow(10, 3) = 1000

The return type of the pow(-,-) method of Math class is double. We are working with integer values so convert it into an integer using type-casting. You can also use your own method to find the power of a given number. Now, divide the number using the power value.

So, first_digit = 1234 / 1000 = 1

Java Program to find the sum of first and last digit of the number

import java.util.Scanner;

public class SumOfFirstAndLastDigitProgram {

  // method to find sum of first and last digit
  private static int FirstLastDigitSum(int number) {

     // declare variables
     int lastDigit, firstDigit, divisor;;
     int totalDigits = 0;
     int sum = 0;

     // find last digit
     lastDigit = number%10;

     // find total number of digits
     totalDigits = findDigits(number);

     // calculate divisor value
     divisor = (int)Math.pow(10, totalDigits-1);

     // find first digit
     firstDigit = number / divisor;

     // add values
     sum = firstDigit + lastDigit;

     return sum;
  }

  // method to find total number of digits
  private static int findDigits(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;
     int sum = 0;

     // create Scanner class object 
     // for reading the values
     Scanner scan =  new Scanner(System.in);

     // read input
     System.out.print("Enter an integer number:: ");
     number = scan.nextInt();

     // find sum of digits of number
     sum = FirstLastDigitSum(number);

     // display result
     System.out.println("The sum of first & last"
            +" digit of the number "+number
            +" = "+ sum);

     // close Scanner class object
     scan.close();
  }
}

The output for the different test-cases:-

Enter an integer number:: 213457
The sum of first & last digit of the number 213457 = 9

Enter an integer number:: 456
The sum of first & last digit of the number 456 = 10

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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