logo

Kako obrniti številko v Javi

V tem razdelku se bomo naučili kako obrniti številko v Javi uporabo medtem ko zanka , za zanko in rekurzija .

Če želite obrniti številko, sledite spodnjim korakom:

  • Najprej poiščemo preostanek danega števila z uporabo operatorja modulo (%).
  • Pomnožite spremenljivko obratno z 10 in ji dodajte preostanek.
  • Število delite z 10.

Ponavljajte zgornje korake, dokler število ne postane 0.

java povratni niz
Kako obrniti številko v Javi

obstajajo tri načini za obračanje števila v Java :

  • Obrnite število z uporabo zanke while
  • Obrnite število z uporabo zanke for
  • Obrnite število z uporabo rekurzije

Uporabimo zgornje korake na primeru.

Primer

Recimo, da želimo obrniti število 1234.

V tem primeru smo vzeli tri imenovane spremenljivke število (številka, ki jo je treba obrniti), ostanek (shrani preostanek), vzvratno (shranjuje obratno številko) inicializirano 0.

1. ponovitev:

primerjaj z nizi v Javi
število = 1234
ostanek = 1234 % 10 = 4
obratno = 0 * 10 + 4 = 0 + 4 = 4
število = 1234 / 10 = 123

Zdaj je vrednost številke in obratne spremenljivke 123 oziroma 4.

2. ponovitev:

število = 123
ostanek = 123 % 10 = 3
obratno = 4 * 10 + 3 = 40 + 3 = 43
število = 123 / 10 = 12

Zdaj je vrednost številke in obratne spremenljivke 12 oziroma 43.

3. ponovitev:

število = 12
ostanek = 12 % 10 = 2
obratno = 43 * 10 + 2 = 430 + 2 = 432
število = 12 / 10 = 1

Zdaj je vrednost številke in obratne spremenljivke 1 oziroma 432.

Ponovitev 4:

število = 1
ostanek = 1 % 10 = 1
obratno = 432 * 10 + 1 = 4320 + 1 = 4321
število = 1/10 = 0

Zdaj spremenljivka številka postane 0. Zato dobimo obratno številko 4321 .

Implementirajmo zgornjo logiko v a Java program .

Obrnite število z uporabo zanke while

ReverseNumberExample1.java

nova vrstica python
 public class ReverseNumberExample1 { public static void main(String[] args) { int number = 987654, reverse = 0; while(number != 0) { int remainder = number % 10; reverse = reverse * 10 + remainder; number = number/10; } System.out.println('The reverse of the given number is: ' + reverse); } } 

Izhod

 The reverse of the given number is: 456789 

Obrnite število z uporabo zanke for

V naslednjem programu smo zanko while zamenjali z zanko for. Prav tako odstrani zadnjo številko števila po vsaki ponovitvi. Ko je stanje, število!=0 postane false, zanka zapusti in dobimo obrnjeno število.

ReverseNumberExample2.java

 public class ReverseNumberExample2 { public static void main(String[] args) { int number = 123456, reverse = 0; //we have not mentioned the initialization part of the for loop for( ;number != 0; number=number/10) { int remainder = number % 10; reverse = reverse * 10 + remainder; } System.out.println('The reverse of the given number is: ' + reverse); } } 

Izhod

 The reverse of the given number is: 654321 

V zgornjem programu lahko zanko for zapišemo tudi na naslednji način:

t ff
 for(;number != 0;) { int remainder = number % 10; reverse = reverse * 10 + remainder; number=number/10; } 

Obrnite število z uporabo rekurzije

ReverseNumberExample3.java

 import java.util.Scanner; public class ReverseNumberExample3 { //method for reverse a number public static void reverseNumber(int number) { if (number <10) 10 { prints the same number if is less than system.out.println(number); return; } else system.out.print(number % 10); reversenumber(number public static void main(string args[]) system.out.print('enter that you want to reverse: '); scanner sc="new" scanner(system.in); int num="sc.nextInt();" system.out.print('the reverse of given is: method calling reversenumber(num); < pre> <p> <strong>Output 1:</strong> </p> <pre> Enter the number that you want to reverse: 9 The reverse of the given number is: 9 </pre> <p> <strong>Output 2:</strong> </p> <pre> Enter the number that you want to reverse: 7654123 The reverse of the given number is: 3214567 </pre> <p>The following program reverses both numbers, positive and negative. When we enter a number, it first checks the number is positive or negative. If the number is negative, it converts the number into positive by multiplying -1. After that, it performs the same steps (as we have performed in the above programs) to reverse a number. At last, again it checks the number is negative or positive. To make the number negative, it again multiplies the reverse number by -1.</p> <p> <strong>ReverseNumberExample4.java</strong> </p> <pre> import java.util.*; public class ReverseNumberExample4 { public static void main(String args[]) { System.out.print(&apos;Enter the number that you want to reverse: &apos;); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int reverse_number = reverseNumber(n); System.out.println(&apos;The reverse of the given number is: &apos;+reverse_number); } //method to reverse a number public static int reverseNumber(int number) { boolean isNoNegative = number = 1) { lastDigit = number % 10; // gives the last digit of the number reverse = reverse * 10 + lastDigit; number = number / 10; // removes the last digit of the number } //makes the number negative return isNoNegative == true? reverse*-1 : reverse; } } </pre> <p> <strong>Output 1:</strong> </p> <pre> Enter the number that you want to reverse: -98765 The reverse of the given number is: -56789 </pre> <p> <strong>Output 2:</strong> </p> <pre> Enter the number that you want to reverse: 321987 The reverse of the given number is: 789123 </pre> <hr></10)>

Rezultat 2:

 Enter the number that you want to reverse: 7654123 The reverse of the given number is: 3214567 

Naslednji program obrne obe števili, pozitivno in negativno. Ko vnesemo število, najprej preveri, ali je število pozitivno ali negativno. Če je število negativno, pretvori število v pozitivno z množenjem -1. Po tem izvede enake korake (kot smo jih izvedli v zgornjih programih), da obrne številko. Na koncu znova preveri, ali je število negativno ali pozitivno. Da bi bilo število negativno, ponovno pomnoži obratno število z -1.

ReverseNumberExample4.java

 import java.util.*; public class ReverseNumberExample4 { public static void main(String args[]) { System.out.print(&apos;Enter the number that you want to reverse: &apos;); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int reverse_number = reverseNumber(n); System.out.println(&apos;The reverse of the given number is: &apos;+reverse_number); } //method to reverse a number public static int reverseNumber(int number) { boolean isNoNegative = number = 1) { lastDigit = number % 10; // gives the last digit of the number reverse = reverse * 10 + lastDigit; number = number / 10; // removes the last digit of the number } //makes the number negative return isNoNegative == true? reverse*-1 : reverse; } } 

Izhod 1:

 Enter the number that you want to reverse: -98765 The reverse of the given number is: -56789 

Rezultat 2:

 Enter the number that you want to reverse: 321987 The reverse of the given number is: 789123