0
write a c++ program to reverse a digits
qn
6 Respuestas
+ 2
http://code.sololearn.com/cO0Ai4pZ9W1C
#include <iostream>
using namespace std;
unsigned int reverseDigits(unsigned int n) {
int i, m;
m = 0;
while (n != 0) {
m = m*10 + n%10;
n = n/10;
}
return m;
}
int main() {
unsigned int n;
cout << "Reverse digits" << endl;
cout << "Please enter a number: ";
cin >> n;
cout << n << endl;
cout << reverseDigits(n) << endl;
return 0;
}
+ 1
#include <iostream>
using namespace std;
int main() {
long int dig,i,rev;
cout<<"Enter the digit\t:: ";
cin>>dig;
// reversing digit
cout<<"The reversed digit is \t:"<<endl;
for(i=1;dig!=0;i*=1){
rev=dig%10;
dig/=10;
cout<<rev;
}
return 0;
}
0
u can simple use string functions to reverse digits
0
u want to reverse string or numbers?
0
Let getReversedNumber(N) is a function, which returns reverse of N. Then, we can use recursion to reverse the digits if a number using below mention recursive equation.
getReversedNumber(N) = (N%10)X pow(10, (log10(N/10) + 1)) + getReversedNumber(N/10)
Here is the c program to reverse a number using recursion
http://www.techcrashcourse.com/2015/03/c-program-to-reverse-number-using-recursion.html
Reversing a number using loop
while(number != 0){
rightDigit = number % 10;
reverse = (reverse * 10) + rightDigit;
number = number/10;
}
http://www.techcrashcourse.com/2014/10/c-program-reverse-number.html
- 3
#include<iostream.h>
int maun()
{
int a=5,i;
for(i=0;i<=a;i++)
{
cout<<a<<endl;
a--;
}
return 0;
}