2 approaches to print first and last digit of a number in java

In this article you can learn how to find out the first and last digit of a number in java with 2 different approaches by doing coding on mobile.

print first and last digit of a number in java [2 different approaches]

2 approaches to print first and last digit of a number in java

In this article you can learn how to find out the first and last digit of a number in java with 2 different approaches by doing coding on mobile. 

👉 You hear right, I use my android phone to write code. 

Approach:-1 "find first and last digit of a number using loop in java"

 public class usingLoop {

    public static void main(String[] args){

      //take a number

      int num = 10738;

      System.out.println("The number is: "+ num);

      int last_digit = num % 10;

      

      //finding first digit

      int first_digit = num;

      while(num >= 10)

      {

        num = num / 10;

        first_digit = num;

      }

      System.out.println("First digit is: "+first_digit);

      System.out.println("Last digit is: "+ last_digit);

      

    }

}

 
+
Explanation

Find last digit 

Finding the last digit of a number is very easy. Just divide the given number with 10 and the remainder will be the last digit. 

In java we have modulus operator to find the remainder, so easily we can find the first digit.

Find first digit

We don't have any method to access first digit of a number directly. 

But we can start from the end side and eliminate digits one by one. And finally the first one can be achieved. 

Here if we divide the number with 10 then results must not contain the last digit as it becomes the remainder. 

So if we continuously divide the number with 10 upto last one we can easily find the first digit.

Output

find first and last digit of a number using loop in java

Also check out:

Approach:-2 "print first and last digit of a number in java using method"

public class usingMethod {

  public static void main(String[] args) {

    int num = 458920;

    System.out.println("The number is: "+ num);

    System.out.println("First digit is: "+first_digit(num));

    System.out.println("Last digit is: "+last_digit(num));

  }

  public static int first_digit(int num) 

  {

    int first_digit_value = num;

    while (num >= 10) 

    {

      num = num / 10;

      first_digit_value = num;

    }

    //System.out.println(first_digit_value);

    return first_digit_value;

  }

  

  public static int last_digit(int num)

  {

    return num % 10;

  }

}

     
+
Explanation

In this approach I just use the static method to find first and last digit of a number. 

Here I have created two separate method for first and last digit. Here the logic part is same.

Output

print first and last digit of a number in java using method

- Hey if you learn something new from the article, then please share with your friends.

➡️ And if you have any questions or any query then please ask me in the comment section. I will definitely answer to you.
`

Post a Comment

Hey Please 👏, feel free to share your opinion 🌍
© codeYourslf. All rights reserved. Distributed by Pixabin