In: Computer Science
Write a program to reverse each integer number on array (size 3)
using while loop. C++
Input: 3678 2390 1783
Output: 8763 0932 3871
#include <iostream>
#include <sstream> // for string streams
#include <string>
using namespace std;
int main()
{ long a[]={3678, 2390, 1783};
long n, reversedNumber, remainder;
for (int i=0;i<=2;i++)
{ n=a[i];
reversedNumber = 1;
while(n > 0)
{
remainder = n%10;
reversedNumber =
reversedNumber*10 + remainder;
n /= 10;
}
ostringstream str1; // declaring output string
stream
// Sending a number as a stream into
output
// string
str1 << reversedNumber;
// the str() coverts number into string
string r = str1.str();
cout << "Reversed Number = "
<< r.substr(1)<<endl;
}
return 0;
}
OUTPUT:
Reversed Number=8763
Reversed Number=0932
Reversed Number=3871