Escriba un programa para invertir los dígitos de un número entero.
C++
#include <bits/stdc++.h> using namespace std; /* Iterative function to reverse digits of num*/ int reverseDigits(int num) { int rev_num = 0; while (num > 0) { rev_num = rev_num * 10 + num % 10; num = num / 10; } return rev_num; } /*Driver program to test reverseDigits*/ int main() { int num = 4562; cout << "Reverse of no. is " << reverseDigits(num); getchar(); return 0; } // This code is contributed // by Akanksha Rai(Abby_akku)
C
#include <stdio.h> /* Iterative function to reverse digits of num*/ int reverseDigits(int num) { int rev_num = 0; while (num > 0) { rev_num = rev_num * 10 + num % 10; num = num / 10; } return rev_num; } /*Driver program to test reverseDigits*/ int main() { int num = 4562; printf("Reverse of no. is %d", reverseDigits(num)); getchar(); return 0; }
Java
// Java program to reverse a number class GFG { /* Iterative function to reverse digits of num*/ static int reverseDigits(int num) { int rev_num = 0; while (num > 0) { rev_num = rev_num * 10 + num % 10; num = num / 10; } return rev_num; } // Driver code public static void main(String[] args) { int num = 4562; System.out.println("Reverse of no. is " + reverseDigits(num)); } } // This code is contributed by Anant Agarwal.
Python
# Python program to reverse a number n = 4562 rev = 0 while(n > 0): a = n % 10 rev = rev * 10 + a n = n // 10 print(rev) # This code is contributed by Shariq Raza
C#
// C# program to reverse a number using System; class GFG { // Iterative function to // reverse digits of num static int reverseDigits(int num) { int rev_num = 0; while (num > 0) { rev_num = rev_num * 10 + num % 10; num = num / 10; } return rev_num; } // Driver code public static void Main() { int num = 4562; Console.Write("Reverse of no. is " + reverseDigits(num)); } } // This code is contributed by Sam007
PHP
<?php // Iterative function to // reverse digits of num function reverseDigits($num) { $rev_num = 0; while($num > 1) { $rev_num = $rev_num * 10 + $num % 10; $num = (int)$num / 10; } return $rev_num; } // Driver Code $num = 4562; echo "Reverse of no. is ", reverseDigits($num); // This code is contributed by aj_36 ?>
Javascript
<script> let num = 4562; // Function to reverse digits of num function reverseDigits(num) { let rev_num = 0; while(num > 0) { rev_num = rev_num * 10 + num % 10; num = Math.floor(num / 10); } return rev_num; } // function call document.write(reverseDigits(num)); // This code is contributed by Surbhi tyagi </script>
C++
// C++ program to reverse digits of a number #include <bits/stdc++.h> using namespace std; /* Recursive function to reverse digits of num*/ int reverseDigits(int num) { static int rev_num = 0; static int base_pos = 1; if (num > 0) { reverseDigits(num / 10); rev_num += (num % 10) * base_pos; base_pos *= 10; } return rev_num; } // Driver Code int main() { int num = 4562; cout << "Reverse of no. is " << reverseDigits(num); return 0; } // This code is contributed // by Akanksha Rai(Abby_akku)
C
// C program to reverse digits of a number #include <stdio.h>; /* Recursive function to reverse digits of num*/ int reversDigits(int num) { static int rev_num = 0; static int base_pos = 1; if (num > 0) { reversDigits(num / 10); rev_num += (num % 10) * base_pos; base_pos *= 10; } return rev_num; } /*Driver program to test reverse Digits*/ int main() { int num = 4562; printf("Reverse of no. is %d", reversDigits(num)); getchar(); return 0; }
Java
// Java program to reverse digits of a number // Recursive function to // reverse digits of num class GFG { static int rev_num = 0; static int base_pos = 1; static int reversDigits(int num) { if (num > 0) { reversDigits(num / 10); rev_num += (num % 10) * base_pos; base_pos *= 10; } return rev_num; } // Driver Code public static void main(String[] args) { int num = 4562; System.out.println(reversDigits(num)); } } // This code is contributed by mits
Python3
# Python 3 program to reverse digits # of a number rev_num = 0 base_pos = 1 # Recursive function to reverse # digits of num def reversDigits(num): global rev_num global base_pos if(num > 0): reversDigits((int)(num / 10)) rev_num += (num % 10) * base_pos base_pos *= 10 return rev_num # Driver Code num = 4562 print("Reverse of no. is ", reversDigits(num)) # This code is contributed by Rajput-Ji
C#
// C# program to reverse digits of a number // Recursive function to // reverse digits of num using System; class GFG { static int rev_num = 0; static int base_pos = 1; static int reversDigits(int num) { if (num > 0) { reversDigits(num / 10); rev_num += (num % 10) * base_pos; base_pos *= 10; } return rev_num; } // Driver Code public static void Main() { int num = 4562; Console.WriteLine(reversDigits(num)); } } // This code is contributed // by inder_verma
PHP
<?php // PHP program to reverse digits of a number $rev_num = 0; $base_pos = 1; /* Recursive function to reverse digits of num*/ function reversDigits($num) { global $rev_num; global $base_pos; if($num > 0) { reversDigits((int)($num / 10)); $rev_num += ($num % 10) * $base_pos; $base_pos *= 10; } return $rev_num; } // Driver Code $num = 4562; echo "Reverse of no. is ", reversDigits($num); // This code is contributed by ajit ?>
Javascript
<script> // Javascript program to reverse digits of a number /* Recursive function to reverse digits of num*/ var rev_num = 0; var base_pos = 1; function reversDigits(num) { if(num > 0) { reversDigits(Math.floor(num/10)); rev_num += (num%10)*base_pos; base_pos *= 10; } return rev_num; } // Driver Code let num = 4562; document.write("Reverse of no. is " + reversDigits(num)); // This code is contributed // by Mayank Tyagi </script>
C++
// C++ program to reverse a number #include <bits/stdc++.h> using namespace std; int reverseDigits(int num) { // converting number to string string strin = to_string(num); // reversing the string reverse(strin.begin(), strin.end()); // converting string to integer num = stoi(strin); // returning integer return num; } int main() { int num = 4562; cout << "Reverse of no. is " << reverseDigits(num); return 0; } // This Code is contributed by ShubhamSingh10
Java
// Java program to reverse a number public class GFG { static int reversDigits(int num) { // converting number to string StringBuffer string = new StringBuffer(String.valueOf(num)); // reversing the string string.reverse(); // converting string to integer num = Integer.parseInt(String.valueOf(string)); // returning integer return num; } public static void main(String[] args) { int num = 4562; System.out.println("Reverse of no. is " + reversDigits(num)); } }
Python3
# Python 3 program to reverse a number def reversDigits(num): # converting number to string string = str(num) # reversing the string string = list(string) string.reverse() string = ''.join(string) # converting string to integer num = int(string) # returning integer return num # Driver code if __name__ == "__main__": num = 4562 print("Reverse of no. is ", reversDigits(num)) # This code is contributed by ukasp.
C#
// C# program to reverse a number using System; public class GFG{ public static string ReverseString(string s) { char[] array = s.ToCharArray(); Array.Reverse(array); return new string(array); } static int reversDigits(int num) { // converting number to string string strin = num.ToString(); // reversing the string strin = ReverseString(strin); // converting string to integer num = int.Parse(strin); // returning integer return num; } // Driver code static public void Main () { int num = 4562; Console.Write("Reverse of no. is " + reversDigits(num)); } } // This Code is contributed by ShubhamSingh10
Javascript
<script> // Javascript program to reverse a number function reversDigits(num) { // converting number to string let str = num.toString().split("").reverse().join(""); // converting string to integer num = parseInt(str); // returning integer return str; } // Driver Code let num = 4562; document.write("Reverse of no. is " + reversDigits(num)); </script>
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; int reversDigits(int num){ // converting number to string string str = to_string(num); // reversing the string reverse(str.begin(), str.end()); // converting string to integer num = stoll(str); // returning integer return num; } // Driver Code int main() { int num = 4562; cout << "Reverse of no. is " << reversDigits(num) ; return 0; } // This code is contributed by sanjoy_62.
Python3
# Python 3 program to reverse a number def reversDigits(num): # converting number to string string = str(num) # reversing the string string = string[::-1] # converting string to integer num = int(string) # returning integer return num # Driver code if __name__ == "__main__": num = 4562 print("Reverse of no. is ", reversDigits(num)) # This code is contributed by Susobhan Akhuli
C#
// C# program for the above approach using System; class GFG { static int reversDigits(int num) { // converting number to string string str = Convert.ToString(num); // reversing the string char[] charArray = str.ToCharArray(); Array.Reverse(charArray); str = new string(charArray); // converting string to integer num = Convert.ToInt32(str); // returning integer return num; } // Driver Code public static void Main(string[] args) { int num = 4562; Console.Write("Reverse of no. is " + reversDigits(num)); } } // This code is contributed by phasing17
Javascript
// JavaScript program for the above approach function reversDigits(num){ // converting number to string let str = num.toString().split(""); // reversing the string str.reverse(); // converting string to integer num = parseInt(str.join("")) // returning integer return num; } // Driver Code let num = 4562; console.log("Reverse of no. is " + reversDigits(num)); // This code is contributed by phasing17
Java
// Java program for the above approach import java.util.*; class GFG { static int reversDigits(int num) { // converting number to string String str = String.valueOf(num); // reversing the string str = new StringBuilder(str).reverse().toString(); // converting string to integer num = Integer.valueOf(str); // returning integer return num; } // Driver Code public static void main(String[] args) { int num = 4562; System.out.println("Reverse of no. is " + reversDigits(num)); } } // This code is contributed by phasing17
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA