logo

Kako preokrenuti broj u Javi

U ovom odjeljku naučit ćemo kako obrnuti broj u Javi korištenjem while petlja , za petlju i rekurzija .

Da biste poništili broj, slijedite dolje navedene korake:

  • Prvo nalazimo ostatak zadanog broja pomoću operatora modulo (%).
  • Pomnožite varijablu obrnuto s 10 i dodajte joj ostatak.
  • Podijelite broj s 10.

Ponavljajte gornje korake dok broj ne postane 0.

string metode java
Kako preokrenuti broj u Javi

Tamo su tri načini obrnutog broja Java :

  • Obrnuti broj pomoću while petlje
  • Obrnuti broj pomoću for petlje
  • Obrnuti broj pomoću rekurzije

Primijenimo gore navedene korake na primjeru.

Primjer

Pretpostavimo da želimo obrnuti broj 1234.

U ovom primjeru uzeli smo tri varijable imenovane broj (broj koji treba obrnuti), ostatak (pohranjuje ostatak), obrnuti (pohranjuje obrnuti broj) inicijalizirano 0.

Iteracija 1:

booleov niz java
broj = 1234
ostatak = 1234 % 10 = 4
obrnuto = 0 * 10 + 4 = 0 + 4 = 4
broj = 1234 / 10 = 123

Sada je vrijednost broja i obrnute varijable 123 odnosno 4.

Iteracija 2:

broj = 123
ostatak = 123 % 10 = 3
obrnuto = 4 * 10 + 3 = 40 + 3 = 43
broj = 123 / 10 = 12

Sada je vrijednost broja i obrnute varijable 12 odnosno 43.

Iteracija 3:

broj = 12
ostatak = 12 % 10 = 2
obrnuto = 43 * 10 + 2 = 430 + 2 = 432
broj = 12 / 10 = 1

Sada je vrijednost broja i obrnute varijable 1 odnosno 432.

Iteracija 4:

broj = 1
ostatak = 1 % 10 = 1
obrnuto = 432 * 10 + 1 = 4320 + 1 = 4321
broj = 1 / 10 = 0

Sada varijabla broj postaje 0. Dakle, dobivamo obrnuti broj 4321 .

Implementirajmo gornju logiku u a Java program .

wumpus svijet

Obrnuti broj pomoću while petlje

ReverseNumberExample1.java

 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); } } 

Izlaz

 The reverse of the given number is: 456789 

Obrnuti broj pomoću for petlje

U sljedećem programu zamijenili smo petlju while petljom for. Također uklanja posljednju znamenku broja nakon svake iteracije. Kada je stanje, broj!=0 postaje lažno, petlja izlazi i dobivamo obrnuti broj.

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); } } 

Izlaz

vrste binarnog stabla
 The reverse of the given number is: 654321 

U gornjem programu također možemo napisati for petlju na sljedeći način:

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

Obrnuti broj pomoću 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)>

Izlaz 2:

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

Sljedeći program obrće oba broja, pozitivan i negativan. Kada unesemo broj, prvo se provjerava je li broj pozitivan ili negativan. Ako je broj negativan, pretvara broj u pozitivan množenjem -1. Nakon toga izvodi iste korake (kao što smo izveli u gornjim programima) za preokret broja. Na kraju, ponovno provjerava je li broj negativan ili pozitivan. Da bi broj bio negativan, ponovno se množi obrnuti broj s -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; } } 

Izlaz 1:

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

Izlaz 2:

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