How to get the first digit of a number in java – Java Program to Find Product of First and Last Digit of a Number

How to get the first digit of a number in java: In the previous article, we have seen Java Program to Find Sum of First and Last Digit of a Number

In this article we will see how we can find the product of first and last digit of a number using Java programming language.

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

Get first digit of int java: 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 product of first and last digit = 8*3 =24

Let’s see different ways

Method-1: Java Program to Find Product 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 product 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("Product of first and last digit of number: "+ (firstDigit*lastDigit));
   }
}
Output:

Number is: 63365
First digit of number: 6
Last digit of number: 5
Product of first and last digit of number: 30

Method-2: Java Program to Find Product 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 product 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("Product 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
Product of first and last digit of number: 32

Method-3: Java Program to Find Product 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))
  • Then find product 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;
      //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("Product 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
Product 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.

Related Java Programs: