Menu Close

Magician’s Numbers in Java …fcukthecod

This problem practices the addition of 2-digit numbers. Encourage the students to share the methods that they use to solve the problem. For example some students may use place value while others will find it easier to use a rounding method.

91 + 19
place value: 91 + 9 + 10
rounding: 91 + 20 1

This problem also offers the opportunity for students to “play” with numbers. As well as practising addition the students are encouraged to look for patterns in their answers. This play encourages students to increase their understanding of numbers and how they relate to one another. It also helps develop problem solving skill and creativity.

As numbers are ‘reversed’ they swap places. (eg. 41 to 14) It is therefore important to discuss what is happening to the place value of the numbers.

If an Integer N, write a program to reverse the given number.

Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer N.

Output
Display the reverse of the given number N

Input:
3        // test cases
12345    // numbers
2512
9009

Output:
54321 
2152
9009
import java.io.*;
import java.util.*;
public class temp{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int n; int[] arr = new int[34];
    System.out.print("Enter the testcases: ");
    n = scan.nextInt();

    for(int i=0;i<n;i++){
      int x = scan.nextInt();
      arr[i] = x;
    }
    for(int i=0;i<n;i++){
      while(arr[i] !=0){
        int rev = 0;
        int remainder = arr[i] %10;
        rev = (rev*10)+remainder;
        arr[i] /= 10;
        System.out.print(rev);
      }
      System.out.println();
    }
  }
}

INPUT_1:
Enter the testcases:  3
12456
8905
9002

OUTPUT:
65421
5098
2009


INPUT_2:
Enter the testcases:  5
2012
2021
2002
2022
2004

OUTPUT:
2102
1202
2002
2202
4002


INPUT_3:
Enter the testcases:  2
1001
1010

OUTPUT:
1001
0101


INPUT_4:
Enter the testcases:  6
000
001
010
011
100
101

OUTPUT:
1
01
11
001
101


INPUT_5:
Enter the testcases:  6
0001
0010
0011
0100
0101
0110

OUTPUT:
1
01
11
001
101
011


ILLUSTRATION

Executed using javac