Java Program to Find the First Digit of a Number

In the previous article, we have seen Java Program to Calculate Electricity Bill

In this article we will see how to find the first digit of a number using Java programming language.

Java Program to Find the First Digit of a Number

The digit at the beginning of a number is called as first digit.

For example:

A number is 786

So, the first digit of a number is 7.

Let’s see different ways to find the first digit of the number.

Method-1: Java Program to Find the First Digit of a Number By Using Static Input Value

Approach:

  • Declare an integer variable say ‘num‘ and initialize a value.
  • Assign the value of ‘num‘ to a temporary value ‘temp
  • Declare an integer variable ‘totalDigit‘ and initialize it to 0.
  • Take a while loop and continue till ‘temp‘ value becomes 0.
  • Inside while loop divide  ‘temp‘ by 10 and increment value of  ‘totalDigit‘.
  • After completion while loop now ‘totalDigit‘ holds the value of total number of digits.
  • Then find the first digit by dividing the number with (int)Math.pow(10,totalDigit-1) means num/(int)Math.pow(10,totalDigit-1)
  • Print the first digit.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //a number declared
  	    int num=590;

  	    //assigning the value of 'num' to a temporary variable 'temp'
  	    int temp=num;
  	    
  	    //declaring integer variable 'totalDigit' and initializing it to 0
  	    int totalDigit=0;
  	    //while loop to find total number of digits
  	    while(temp!=0)
  	    {
  	        temp=temp/10;
  	        totalDigit++;
  	    }
    
        //find first digit of the number
  	    int firstDigit = num /(int)Math.pow(10,totalDigit-1);
        
        System.out.println("First Digit: "+ firstDigit);
 
    }
}
Output:

First digit: 5

Method-2: Java Program to Find the First Digit of a Number By Using User Input Value

Approach:

  • Declare an integer variable say ‘num‘ and take value as user input.
  • Assign the value of ‘num‘ to a temporary value ‘temp
  • Declare an integer variable ‘totalDigit‘ and initialize it to 0.
  • Take a while loop and continue till ‘temp‘ value becomes 0.
  • Inside while loop divide  ‘temp‘ by 10 and increment value of  ‘totalDigit‘.
  • After completion while loop now ‘totalDigit‘ holds the value of total number of digits.
  • Then find the first digit by dividing the number with (int)Math.pow(10,totalDigit-1) means num/(int)Math.pow(10,totalDigit-1)
  • Print the first digit.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //taking a number input from user
  	    System.out.println("Please enter a number: ");
  	    int num=sc.nextInt();
  	    //assigning the value of 'num' to a temporary variable 'temp'
  	    int temp=num;
  	    
  	    //declaring integer variable 'totalDigit' and initializing it to 0
  	    int totalDigit=0;
  	    //while loop to find total number of digits
  	    while(temp!=0)
  	    {
  	        temp=temp/10;
  	        totalDigit++;
  	    }
    
        //find first digit of the number
  	    int firstDigit = num /(int)Math.pow(10,totalDigit-1);
        
        System.out.println("First Digit: "+ firstDigit);
 
    }
}
Output:

Please enter a number: 
5893
First Digit: 5

Method-3: Java Program to Find the First Digit of a Number By Using User Defined Method

Approach:

  • Declare an integer variable say ‘num‘ and take value as user input.
  • Then call a user defined method findFirst() and pass ‘num‘ as parameter.
  • Inside the method assign the value of ‘num‘ to a temporary value ‘temp
  • Declare an integer variable ‘totalDigit‘ and initialize it to 0.
  • Take a while loop and continue till ‘temp‘ value becomes 0.
  • Inside while loop divide  ‘temp‘ by 10 and increment value of  ‘totalDigit‘.
  • After completion while loop now ‘totalDigit‘ holds the value of total number of digits.
  • Then find the first digit by dividing the number with (int)Math.pow(10,totalDigit-1) means num/(int)Math.pow(10,totalDigit-1)
  • Print the first digit.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //taking a number input from user
  	System.out.println("Please enter a number: ");
  	int num=sc.nextInt();
  	//calling a method findFirst()
  	findFirst(num);
    }
    
    //findFirst() method to find first digit of the number
    public static void findFirst(int num)
    {
  	//assigning the value of 'num' to a temporary variable 'temp'
  	int temp=num;
  	    
  	//declaring integer variable 'totalDigit' and initializing it to 0
  	int totalDigit=0;
        //while loop to find total number of digits
  	while(temp!=0)
  	{
  	    temp=temp/10;
  	    totalDigit++;
  	}
    
        //find first digit of the number
  	int firstDigit = num /(int)Math.pow(10,totalDigit-1);
        
        System.out.println("First Digit: "+ firstDigit);
 
    }
}
Output:

Please enter a number: 
8945
First Digit: 8

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: