Problem: 2.17


Question 2.17: Create a function that takes an integer n and reverses it.

Examples

rev(5121) ➞ "1215"

rev(69) ➞ "96"

rev(-122157) ➞ "751221"

Notes: This challenge is about using two operators that are related to division. If the number is negative, treat it like it's positive.


Solution:

package com.company;
import java.util.Scanner;
public class Lab2problem17 {
    public static void main(String[] args)
    {
        Scanner Obj = new Scanner(System.in);
        int n =Obj.nextInt();
        for(int i = 10; n>10;)
        {
            int r = n % i;
            System.out.print(r);
            n =n/i;
            r=1;
        }
        System.out.println(n);

    }
}

Previous Post Next Post