Java to Must Enter a Number Again Input

Here we will learn how to opposite a number in Java. For example if a given input number is 19 and so the output of the program should be 91. There are several ways to opposite a number in Java. Nosotros will mainly hash out following three techniques to reverse a number.

Reverse number

Table of contents

  • Program 1: Reverse a number using while Loop
  • Program 2: Reverse a number using for Loop
  • Programme three: Reverse a number using recursion

Program 1: Contrary a number using while Loop

In this plan, we are taking the input number from the user using Scanner course and then we are reversing the number using while loop.

The logic we are using here is: Inside the while loop we are dividing the given number by ten using % operator and then storing the rest in the reversenum variable after multiplying the reversenum past 10. we are repeating this step again and over again until the given number get zilch.

import java.util.Scanner; course ReverseNumberWhile {    public static void main(String args[])    {       int num=0;       int reversenum =0;       Organization.out.println("Input your number and press enter: ");       //This statement will capture the user input       Scanner in = new Scanner(Arrangement.in);       //Captured input would be stored in number num       num = in.nextInt();       //While Loop: Logic to find out the reverse number       while( num != 0 )       {           reversenum = reversenum * 10;           reversenum = reversenum + num%10;           num = num/10;       }        Organization.out.println("Opposite of input number is: "+reversenum);    } }

Output:

Input your number and press enter:  145689 Reverse of input number is: 986541

Programme 2: Contrary a number using for Loop

This program is fairly similar to the first program, here we are using for loop instead of while loop.

As y'all tin come across, in this program we have not used the initialization and increment/decrement department of for loop because nosotros have already initialized the variables outside the loop and we are decreasing the value of num inside for loop by diving it by 10.

The logic is same as first program.

import java.util.Scanner; class ForLoopReverseDemo {    public static void main(String args[])    {       int num=0;       int reversenum =0;       System.out.println("Input your number and printing enter: ");       //This statement will capture the user input       Scanner in = new Scanner(Organization.in);       //Captured input would be stored in number num       num = in.nextInt();       /* for loop: No initialization function as num is already        * initialized and no increment/decrement role every bit logic        * num = num/10 already decrements the value of num        */       for( ;num != 0; )       {           reversenum = reversenum * 10;           reversenum = reversenum + num%ten;           num = num/x;       }        Arrangement.out.println("Contrary of specified number is: "+reversenum);    } }

Output:

Input your number and press enter:  56789111 Reverse of specified number is: 11198765

Program 3: Reverse a number using recursion

Here we are using recursion to reverse the number. We have defined a method reverseMethod() and nosotros are passing the input number to this method.

This method and so divides the number by 10, displays the balance and and so calls itself by passing the caliber as parameter. This procedure goes on and on until the number is in single digit and and so it displays the final digit (which is the kickoff digit of the number) and ends the recursion.

import java.util.Scanner; class RecursionReverseDemo {    //A method for reverse    public static void reverseMethod(int number) {        if (number < 10) { 	   System.out.println(number); 	   render;        }        else {            Organization.out.print(number % 10);            //Method is calling itself: recursion            reverseMethod(number/10);        }    }    public static void master(String args[])    { 	int num=0; 	System.out.println("Input your number and press enter: "); 	Scanner in = new Scanner(Organisation.in); 	num = in.nextInt(); 	System.out.print("Reverse of the input number is:"); 	reverseMethod(num); 	Arrangement.out.println();    } }

Output:

Input your number and press enter:  5678901 Reverse of the input number is:1098765

Example: Reverse an already initialized number
In all the above programs we are prompting user for the input number, however if do not want the user interaction function and want to reverse an initialized number and then this is how you can exercise it.

grade ReverseNumberDemo {    public static void master(String args[])    {       int num=123456789;       int reversenum =0;       while( num != 0 )       {           reversenum = reversenum * 10;           reversenum = reversenum + num%10;           num = num/10;       }        System.out.println("Reverse of specified number is: "+reversenum);    } }

Output:

Reverse of specified number is: 987654321

Related Coffee Examples :

nobleoberthe48.blogspot.com

Source: https://beginnersbook.com/2014/01/java-program-to-reverse-a-number/

0 Response to "Java to Must Enter a Number Again Input"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel