hi all,
i am a beginner in java programming.Could anyone please provide source code for the following program.
A program that reads a integer and breaks it into a sequence of individual digits in reverse order.TQ.

Dani AI

Generated

A straightforward, reliable solution uses integer arithmetic instead of string recursion or array conversions: repeatedly take n % 10 to get the last digit, print or store it, then divide n by 10. That naturally yields the digits in reverse order and avoids extra allocations or recursion. ’s string-reverse idea and ’s array suggestion are valid alternatives, but the arithmetic method is simplest for this specific task. and ’s reminder about attempting the homework first is sensible—this example is short and meant to clarify the technique.

import java.util.Scanner;

public class ReverseDigits {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        if (!sc.hasNextLong()) {
            System.out.println("Not an integer.");
            sc.close();
            return;
        }
        long n = sc.nextLong();
        sc.close();

        if (n == 0) {
            System.out.println(0);
            return;
        }
        if (n < 0) n = -n;   // drop sign before processing

        while (n > 0) {
            System.out.println(n % 10);   // prints last digit first
            n /= 10;
        }
    }
}

Notes and edge cases: the loop prints digits for input 12345 as 5 4 3 2 1 (each on its own line). Inputs like 1200 produce 0 0 2 1 (trailing zeros are preserved in the reversed output). To preserve leading zeros from textual input (for example "00123"), read the value as a String and iterate characters from the end—this is the route taken in the recursive example posted earlier. For values larger than long, convert to BigInteger or process the input as text. Basic input validation (shown above) prevents crashes on non-numeric input.

Recommended Answers

All 7 Replies

Hi Kumar,

Attempt to do your homework yourself, and then we can help you along the way. We do not do homework for you here.


Ed

hi all,
i am a beginner in java programming.Could anyone please provide source code for the following program.
A program that reads a integer and breaks it into a sequence of individual digits in reverse order.TQ.

Oh my, how many times must this be repeated?

If you're having trouble try reading in the integers into an array. Or you could turn the string into an array with the CopyTo() method or ToArray() methods.

Here is how to reverse an inputed string, just apply it all to Int and you should have your program:

class ReverseString

public class ReverseString
{
 public String reverse(String arg)
 {
	String tmp = null;
	if (arg.length() == 1)
	{
		return arg;
	}
	
	else
	{
		

		//extract the last char
		String lastChar = arg.substring(arg.length()-1,arg.length());
		
		
		//extract the remaining chars
		String remainingString = arg.substring(0, arg.length() -1);

		tmp = lastChar + reverse(remainingString);
		return tmp;
		
		
	}
  }
}

class TestReverse

import java.io.*;


public class TestReverse
{
	public static void main(String[] args) throws IOException
 	{
		System.out.println("Enter a line to be reversed");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inData;
		inData = br.readLine();
		ReverseString rs = new ReverseString();
		System.out.println("Reversed: " + rs.reverse(inData));	
	}
}

You can also get it to print the letters individually if you put the println() statement in the first class.

Here is how to reverse an inputed string, just apply it all to Int and you should have your program:

class ReverseString

public class ReverseString
{
 public String reverse(String arg)
 {
	String tmp = null;
	if (arg.length() == 1)
	{
		return arg;
	}
	
	else
	{
		

		//extract the last char
		String lastChar = arg.substring(arg.length()-1,arg.length());
		
		
		//extract the remaining chars
		String remainingString = arg.substring(0, arg.length() -1);

		tmp = lastChar + reverse(remainingString);
		return tmp;
		
		
	}
  }
}

class TestReverse

import java.io.*;


public class TestReverse
{
	public static void main(String[] args) throws IOException
 	{
		System.out.println("Enter a line to be reversed");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inData;
		inData = br.readLine();
		ReverseString rs = new ReverseString();
		System.out.println("Reversed: " + rs.reverse(inData));	
	}
}

plz don't do like this..................
they should do the hw themselvs

plz don't do like this..................
they should do the hw themselvs

VERY true.

We're here not to help people cheat, but to help people learn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.