Print Digits Individually coderinme

Print Digits Individually coderinme

Suppose a user enters a number 12345. Now we shall code to print each digit of the user given number in a separate line just like written below:
1
2
3
4
5

Write a Java program which reads a 4-digit number and prints the digits on separate lines. (Each digit is printed on one line).
Program:

import java.io.*;
public class Q17 
{
public static void main(String[] args) throws IOException 
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter four digit number :");
int n=Integer.parseInt(in.readLine());
if(n>=1000 && n<=9999) {
int n1=(n/1000);
int n2=((n%1000)/100);
int n3=(((n%1000)%100)/10);
int n4=(((n%1000)%100)%10);
System.out.println("The first digit is : "+n1);
System.out.println("The second digit is : "+n2);
System.out.println("The third digit is : "+n3);
System.out.println("The four digit is : "+n4);
}
else 
{
System.out.println("You have enter wrong number :");
System.out.println("Please enter four digit number :");
}
}
}

For more programs on Java, visit our Java Archives

Print Digits Individually

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.