FaltuTech.Club : Fane of Advanced Logical Thriving Utopian Technical Club

Reverse a Number Using While and For Loop In Java

Reverse a Number Using While and For Loop In Java



class Rever{

    public static void main(String args[]){
        int num = 4894895;
	int reverse = 0;
	while(num != 0){
	
	    int remainder = num %10;
	    num /=10;
	    reverse = (reverse*10) + remainder;
	}

	System.out.println(reverse);
	
    }


}


Output

5984984

Discussion

For ‘for’ loop use – for(;num!=0;)

Compiled with JDK 9