programa Java para invertir un número entero.

  • Inicio
  • Java Resuelto Programas
  • Core Ejemplo de Java Programas

inversa Número de usar programa Java

//Java program to Reverse a Number.
import java.util.*;
public class ReverseNumber{
public static void main(String []args){
int number;
Scanner sc=new Scanner(System.in);
//Read Number
System.out.print("Enter an integer number: ");
number=sc.nextInt();
//calculate reverse number
int reverse_number=0;
while(number>0)
{
reverse_number = (reverse_number*10) + number%10;
number/=10;
}
System.out.println("Reverse Number is: " + reverse_number);
}
}

    
[email protected]:~$ javac ReverseNumber.java
[email protected]:~$ java ReverseNumber
Enter an integer number: 12345
Reverse Number is: 54321

Usando Función / Método

//Java program to Reverse a Number.
import java.util.*;
public class ReverseNumber{
//Function to find Reverse Number
public static int RevNumber(int num)
{
int sum=0;
while(num>0)
{
sum = (sum*10) + num%10;
num/=10;
}
return sum;
}
public static void main(String []args){
int number;
Scanner sc=new Scanner(System.in);
//Read Number
System.out.print("Enter an integer number: ");
number=sc.nextInt();
//find reverse number - calling function
int reverse_number;
reverse_number= RevNumber(number);
System.out.println("Reverse Number is: " + reverse_number);
}
}


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *