Computer Science, asked by raghavrishi454, 5 hours ago

write a program to generate 4 digit number and display the reverse of number generated in java (without using loop ) (write program in scanner class)​

Answers

Answered by kamalrajatjoshi94
3

Answer:

Program:-

//Using enhanced approach using String Buffer

import java.util.*;

public class Main

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

int n;

String str;

System.out.println("Enter a four digit number");

n=in.nextInt();

if(n>=1000&&n<10000)

{

str=Integer.toString(n);

StringBuffer st=new StringBuffer(str);

st.reverse();

System.out.println("The reversed four digit number="+st);

}

else

System.out.println("Invalid input");

}

}

Output is attched.

Output is attched.No loop is used.

Attachments:
Answered by Shivu516
0

This program can also reverse numbers having more than 4 digits and it's also very simple

import java.util.Scanner;

public class NumberReverser {

   int reverse = 0;

   public static void main(String [] args){

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter a number: ");

       StringBuilder number = new StringBuilder(sc.next());

       System.out.print("The reversed number is ");

       System.out.print(number.reverse());

   }

}

Output:

Enter a number: 123456789

The reversed number is 987654321

Similar questions