Print digits of a number separately in Java | AskTheCode

How to print the digits of a number separately in java | AskTheCode


import java.util.Scanner;


class printNumberSeparately{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number: ");

int n = sc.nextInt();

int tmp = n, factor = 1;

while(tmp != 0){

tmp = tmp / 10;

factor = factor * 10;

}

while(factor > 1){

factor = factor / 10;

System.out.print((n/factor)+" ");

n = n % factor;

}

}

}


20 views0 comments

Recent Posts

See All

"Believe you can and you're halfway there"