Saturday, October 23, 2021

Math Class Function In Java

 In this task, you are being asked to apply Math class functions.
Write a program that computes the first and last digit of a number. For example, if the input is
23456, the program should print 2 and 6.
Hint: Use % and Math.log10.

Code:

import java.util.Scanner;
public class Lab4Task4{
public static void main(String[] args) {
Scanner Ahsan = new Scanner(System.in);

int firstDigit, lastDigit, num;
System.out.print("Enter the number: ");
num = Ahsan.nextInt();

firstDigit = num / (int) Math.pow(10, (int) Math.log10(num));
lastDigit = num % 10;
System.out.println("Input number: " + num);
System.out.println("First Digit is: " + firstDigit);
System.out.println("Last Digit is: " + lastDigit);
}
}

OutPut:

Math Class Function In Java


No comments:

Post a Comment

String Array and passing method in java

 In this task, we pass a method and also cover the passing String Array to the method. Code:   import java.util.Scanner; public class String...