Java Program to Find Sum of First and Last Digit of a Number

In the previous article, we have seen Java Program to Calculate Profit and Loss

In this article we will see how we can find the sum of first and last digit of a number using Java programming language. What is the java program to find sum of first and last digit of a number, Sum of first And last digit in java, First and last digit sum in java, Java program to find the sum of first and last digit, Add first and last digit in java, First and last digit, Java program to find first and last digit of a number, Last digit of a number in java are discussed here.

Java Program to Find Sum of First and Last Digit of a Number

If a number is given then starting digit of the number is called as first and end digit of the number is called as last digit of number.

For example:

If the number is 89453 then first digit is 8 and last digit is 3.

So the sum of first and last digit = 8+3 =11

Let’s see different ways

Method-1: Java Program to Find Sum of First and Last Digit of a Number By Finding Only First Digit Inside While Loop and Last Digit Outside Loop (Static Input Value)

Approach:

  • Declare an integer variable say ‘number‘ and initialize a value.
  • Declare two integer variables firstDigit and lastDigit and initialize both to 0.
  • Find last digit by lastDigit=number%10
  • Inside while loop find last digit by rem=number%10 and then number=number/10 and continuing the while loop till number value becomes 0.
  • After loop completes ‘rem‘ has the first digit.
  • Then find the sum of of firstDigit and lastDigit

Program:

public class Main 
{
   public static void main(String args[])
   {
      //a number declared
      int number = 63365;
      //storing the number value in a temporary variable say 'temp'
      int temp=number;
      //declaring two integer variable firstDigit and lastDigit 
      //and initializing both with value as 0
      int firstDigit=0;
      int lastDigit=0;
      
      //find last digit of number
      lastDigit=number%10;
      
      //countinue the loop till the number becomes 0
      //inside loop variable 'firstDigit' will give the first digit at the end of while loop
      while(number!=0)
      {
         int rem = number%10;
         firstDigit=rem;
         number= number/10;
      }
      System.out.println("Number is: "+temp);
      System.out.println("First digit of number: "+ firstDigit );
      System.out.println("Last digit of number: "+ lastDigit);
      System.out.println("Sum of first and last digit of number: "+ (firstDigit+lastDigit));
   }
}
Output:

Number is: 63365
First digit of number: 6
Last digit of number: 5
Sum of first and last digit of number: 11

Method-2: Java Program to Find Sum of First and Last Digit of a Number By Finding Only First Digit Inside While Loop and Last Digit Outside Loop (User Input Value)

Approach:

  • Declare an integer variable say ‘number‘ and take the value of it as user input.
  • Declare two integer variables firstDigit and lastDigit and initialize both to 0.
  • Find last digit by lastDigit=number%10
  • Inside while loop find last digit by rem=number%10 and then number=number/10 and continuing the while loop till number value becomes 0.
  • After loop completes ‘rem‘ has the first digit.
  • Then find the sum of of firstDigit and lastDigit

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
      Scanner sc=new Scanner(System.in);
      
      //taking a number input from user
      System.out.println("Enter a number: ");
      int number = sc.nextInt();
      //storing the number value in a temporary variable say 'temp'
      int temp=number;
      //declaring two integer variable firstDigit and lastDigit 
      //and initializing both with value as 0
      int firstDigit=0;
      int lastDigit=0;
      
      //find last digit of number
      lastDigit=number%10;
      
      //countinue the loop till the number becomes 0
      //inside loop variable 'firstDigit' will give the first digit at the end of while loop
      while(number!=0)
      {
         int rem = number%10;
         firstDigit=rem;
         number= number/10;
      }
      System.out.println("Number is: "+temp);
      System.out.println("First digit of number: "+ firstDigit );
      System.out.println("Last digit of number: "+ lastDigit);
      System.out.println("Sum of first and last digit of number: "+ (firstDigit+lastDigit));
   }
}
Output:

Enter a number: 
45678
Number is: 45678
First digit of number: 4
Last digit of number: 8
Sum of first and last digit of number: 12

Method-3: Java Program to Find Sum of First and Last Digit of a Number – Finding First Digit  By Dividing Number with power of 10 ^ (totalDigits-1) and Last Digit By Number%10

Approach:

  • Declare an integer variable say ‘number‘ and initialize a value.
  • Declare two integer variables firstDigit and lastDigit and initialize both to 0.
  • Find last digit by lastDigit=number%10
  • Find first digit by firstDigit=number/((int) Math.pow(10, total_digits-1))

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
      Scanner sc=new Scanner(System.in);
      
      //taking a number input from user
      System.out.println("Enter a number: ");
      int number = sc.nextInt();
      //storing the number value in a temporary variable say 'temp'
      int temp=number;
      //declaring two integer variable firstDigit and lastDigit 
      //and initializing both with value as 0
      int firstDigit=0;
      int lastDigit=0;
      //initializing totalDigits as 0
      int totalDigits=0;
      
      //find last digit of number
      lastDigit=number%10;
      
      //countinue the loop till the number becomes 0
      //temp holds the value of number
      while(temp!=0)
      {
         temp= temp/10;
         totalDigits++;
      }
      
      //find first digit of number
      firstDigit=number/((int) Math.pow(10, totalDigits-1));
      
      System.out.println("Number is: "+number);
      System.out.println("Total Digits: "+totalDigits);
      System.out.println("First digit of number: "+ firstDigit );
      System.out.println("Last digit of number: "+ lastDigit);
      System.out.println("Sum of first and last digit of number: "+ (firstDigit+lastDigit));
   }
}
Output:

Enter a number: 
56478
Number is: 56478
Total Digits: 5
First digit of number: 5
Last digit of number: 8
Sum of first and last digit of number: 13

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Practice these: 

  1. Write A Program To Find Sum Of First And Last Digit Of A Number In Java
  2. How To Get First Digit Of A Number In Java
  3. Write A Program To Find The Sum Of Last 2 Digits Of A Given Number
  4. Write A Java Program To Find The Sum Of The Digits Of A Given Integer
  5. How To Add First And Last Digit Of A Number In Java

Related Java Programs: