Write a method

0 votes

  1. Write a method, reverseDigits that takes an integer as parameter and returns the number with the digits reversed. Also, write a program to test your function.

Here is a sample of input/output:

Enter an integer: 4567

4567 after digits reversed = 7654

Enter an integer: -45

-45 after digits reversed = -54

Enter an integer: 980

980 after digits reversed = 89

May 4 in Java by MIH
• 160 points
90 views

1 answer to this question.

0 votes

Hey, @MIH,

You can follow this:

public class ReverseNumber {

    public static void main(String[] args) {

        int num = 4567, reversed = 0;

        while(num != 0) {
            int digit = num % 10;
            reversed = reversed * 10 + digit;
            num /= 10;
        }

        System.out.println("Reversed Number: " + reversed);
    }
}


Reversed Number: 7654

I hope this helps.

answered Jun 4 by Gitika
• 37,370 points

Related Questions In Java

0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 57,940 points
241 views
0 votes
1 answer

How can I make the return type of a method generic?

First of all, define callFriend: public <T extends ...READ MORE

answered May 18, 2018 in Java by sharth
• 3,370 points
82 views
0 votes
1 answer

How to call a method after a delay in Android using Java?

final Handler handler = new Handler(); handler.postDelayed(new Runnable() ...READ MORE

answered Jun 11, 2018 in Java by Akrati
• 3,170 points
3,785 views
0 votes
1 answer

Can a static reference be made to a non-static method

setLoanItem() isn't a static method, it's an instance ...READ MORE

answered Jun 26, 2018 in Java by samarth295
• 2,200 points
153 views
+2 votes
12 answers

“A Non-static method cannot be referenced from a static context” error

this method must be static so that it will exist even though the ...READ MORE

answered Jun 11, 2019 in Java by Neha
• 330 points
137,069 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,580 points
189 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,270 points
190 views
0 votes
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
1,090 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,200 points
1,754 views
0 votes
2 answers

How to generate a class at runtime?

Yes, I recently met this library which ...READ MORE

answered Apr 15 in Java by Edureka
• 500 points
109 views