Pages

Sunday, 25 December 2016

Problem 79 - Reverse Number dengan Rekursi

Program akan membalikan urutan angka dengan rekursi
Source code program

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. import java.util.Scanner;
  2. class RecursionReverseDemo
  3. {
  4.    public static void reverseMethod(int number)
  5.    {
  6.        if (number < 10) {
  7.        System.out.println(number);
  8.        return;
  9.        }
  10.        else {
  11.            System.out.print(number % 10);
  12.            reverseMethod(number/10);
  13.        }
  14.    }
  15.    public static void main(String args[])
  16.    {
  17.     int num=0;
  18.     System.out.println("Input your number and press enter: ");
  19.     Scanner in = new Scanner(System.in);
  20.     num = in.nextInt();
  21.     System.out.print("Reverse of the input number is:");
  22.     reverseMethod(num);
  23.     System.out.println();
  24.    }
  25. }
Hasil output program

0 comments:

Post a Comment