Look at the first solution to find the length of a given integer which is a primitive datatype. This approach is based on the core logic without using any built-in methods.
import java.util.Scanner;
public class LengthOfPrimitiveIntValue {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner inpInt= new Scanner(System.in);
//Input an Integer
System.out.println("Enter an Integer=");
int numValue=inpInt.nextInt();
System.out.println("The integer is="+numValue);
//Find out the Length
int lenOfInt=0, numBackUp= numValue;
while (numValue!=0) {
lenOfInt=lenOfInt+1;
numValue=numValue/10;
}
System.out.println("The Length of an Integer "+numBackUp+" is="+lenOfInt);
}
}
Output:
Enter an Integer=
324
The integer is=324
The Length of an Integer 324 is=3

Leave a comment