Write a program to reverse the number.
Example:
Input: num = 1234 Output: reverse is 4321
Approach
C
#include <stdio.h>int main(){int num = 1234;int rev = 0;while (num > 0){int temp = num % 10;rev = rev * 10 + temp;num = num / 10;}printf("reverse is %d", rev);return 0;}
Java
public class ReverseInteger {public static void main(String aa[]) {int number = 1234;int revNumber = reverseNumber(number);System.out.println(number + " reverse is " + revNumber);}//Method to find revers of a numberpublic static int reverseNumber(int x) {int rev = 0;while (x != 0) {int m = x % 10;rev = rev * 10 + m;x = x / 10;}return rev;}}
C++
#include <bits/stdc++.h>using namespace std;//Function to find reverse of numberint reverseNumber(int n){int rev=0;while(n!=0){rev=rev*10+n%10;n=n/10;}return rev;}int main(){int number=1234;int revNumber=reverseNumber(number);cout<<number<<" reverse is "<<revNumber<<"\n";return 0;}
No comments:
Post a Comment