White a program to imput any triple digit number and perform
the following action on that:
Print out the total of all the digits
Reverse the entive number
Answers
Answer:
▲
Related Articles
Reverse digits of an integer with overflow handled
Write a program to reverse digits of a number
Write a program to reverse an array or string
Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i
Rearrange positive and negative numbers in O(n) time and O(1) extra space
Rearrange array in alternating positive & negative items with O(1) extra space | Set 1
Rearrange array in alternating positive & negative items with O(1) extra space | Set 2
Move all zeroes to end of array
Move all zeroes to end of array | Set-2 (Using single traversal)
Minimum swaps required to bring all elements less than or equal to k together
Rearrange positive and negative numbers using inbuilt sort function
Rearrange array such that even positioned are greater than odd
Rearrange an array in order – smallest, largest, 2nd smallest, 2nd largest, ..
Double the first element and move zero to end
Reorder an array according to given indexes
Arrange given numbers to form the biggest number | Set 1
Arrange given numbers to form the biggest number | Set 2
Find the largest number that can be formed with the given digits
Find next greater number with same set of digits
Finding sum of digits of a number until sum becomes single digit
Program for Sum of the digits of a given number
Compute sum of digits in all numbers from 1 to n
Count possible ways to construct buildings
Maximum profit by buying and selling a share at most twice
Maximum profit by buying and selling a share at most k times
Program for Fibonacci numbers
Write a program to print all permutations of a given string
Coin Change | DP-7
Set in C++ Standard Template Library (STL)
C++ Data Types
Write a program to reverse digits of a number
Difficulty Level : Easy
Last Updated : 20 Apr, 2020
Write a program to reverse digits of an integer.
Examples :
Input : num = 12345
Output : 54321
Input : num = 876
Output : 678
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
Flowchart:
ITERATIVE WAY
Algorithm:
Input: num
(1) Initialize rev_num = 0
(2) Loop while num > 0
(a) Multiply rev_num by 10 and add remainder of num
divide by 10 to rev_num
rev_num = rev_num*10 + num%10;
(b) Divide num by 10
(3) Return rev_num
Example:
num = 4562
rev_num = 0
rev_num = rev_num *10 + num%10 = 2
num = num/10 = 456
rev_num = rev_num *10 + num%10 = 20 + 6 = 26
num = num/10 = 45
rev_num = rev_num *10 + num%10 = 260 + 5 = 265
num = num/10 = 4
rev_num = rev_num *10 + num%10 = 265 + 4 = 2654
num = num/10 = 0
Program:
C++
#include <bits/stdc++.h>
using namespace std;
/* Iterative function to reverse digits of num*/
int reversDigits(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 reversDigits*/
int main()
{
int num = 4562;
cout << "Reverse of no. is "
<< reversDigits(num);
getchar();
return 0;
}
// This code is contributed
// by Akanksha Rai(Abby_akku)