Coud you please explain this programme , step by step , i cant understand its method

import java.util.Scanner;
public class Ex5_4 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int x = input.nextInt();
        int y = reverse(x);
        System.out.println("The reverse of " + x + " is " + y);
    public static int reverse(int x) {
        int i , count = 1 , sum = 0;
        for (i = 10 ; i <= x ; i = i * 10) {
            i = i / 10;

        for (int j = 1 ; j <= count ; j++) {
            int digit = x - ((x / 10) * 10);
            sum += digit * i;
            i = i / 10;
            x = x / 10;
        return sum;
3 Years
Discussion Span
Last Post by elaf salah

ThanQ for replaying

Purpose of code is to returns a reverse number
Exp- if i write 123 the consol show 321

What did we use count for ?
 what does the equation on line 18 refer to     
and why did we define   i  2 times ?
sorry but i`m Junior in programming 

1) Count is used to determine how long the number is (in decimal digits).

2) Line 18 extracts the digit at the lowest significant place by calculating the value of the number as it would be if the units digit was missing [(( x / 10 ) x 10 )] and subtracting that from the number itself (eg: if x is 123, then ( x / 10 ) = 12 (integer divide which drops the fraction) x 10 = 120. 123 - 120 = 3).

3) i is not defined twice. it is the value of the highest power of 10 in the number (the initial loop calculates the first power of 10 greater than the entire number, then it is divided by 10 giving the highest power of 10 in the number). In the second loop, that power is reduced by one power of ten for each iteration of the loop.

Thus, the first for() loop calculates the lowest power of 10 greater than x. That is then decreased by one power of 10 giving the highest power of 10 actually in x. In the meantime, count is calculated which gives the total number of digits (powers of 10) available.

In the second for loop, the lowest digit is extracted, multiplied by the highest power in the number and added to the result (making the LSD in the number the MSD in the result). Then the highest power is reduced by one power ( i = 1 / 10 ) and the value of x is reduced by one power (removing the low-order digit). The process repeats until all of the digits have been reversed.

What is probably confusing you is the integer divide. In many modern languages, using a divide operation on a declared integral variable implicitly truncates any fractional portion. If you want to divide an integral variable and retain the fraction, you must first cast it to a real type.

good explanation.
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.