Reputation: 27
I am trying to make a program in c++ to reverse a number it is ok with a number like 1234 but if am trying to input a number like 5430 it is showing 345 and the same in case the number starting with zero eg: if input 0234 it will show 432.
Can somebody tell me how to handle zeros at starting and ending.
I have to store the number only without converting it to string
#include<iostream>
using namespace std;
int main(){
int n;
int lastdigit;
cin >> n;
int reverse = 0;
while(n!=0){
lastdigit = n % 10;
reverse = reverse * 10 + lastdigit;
n = n / 10;
}
cout << reverse<<endl;
}
Upvotes: 0
Views: 1591
Reputation: 5202
Here's an alternative potential solution that keeps it "as numbers."
#include <iostream>
#include <stack>
int main() {
int num;
std::cout << "Number: ";
std::cin >> num;
// Store individual digits in a stack
std::stack<int> digits;
while (num) {
digits.push(num % 10);
num /= 10;
}
// Dump first stack into new stack, reversing them
std::stack<int> reversed;
while (!digits.empty()) {
reversed.push(digits.top());
digits.pop();
}
// Print and deplete reversed digit stack
std::cout << '\n';
while (!reversed.empty()) {
std::cout << reversed.top();
reversed.pop();
}
std::cout << '\n';
return 0;
}
It takes advantage of the std::stack
data structure, but is cumbersome. As a comparison point, here is my suggestion of using a std::string
.
#include <iostream>
#include <string>
int main() {
std::string num;
std::cout << "Number: ";
std::getline(std::cin, num);
for (auto criter = num.crbegin(); criter != num.crend(); ++criter) {
std::cout << *criter;
}
std::cout << '\n';
return 0;
}
Output for either program:
Number: 300
003
You can probably see why the std::string
is preferred.
Upvotes: 0
Reputation:
This should sort your issue.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i;
cin>>n; // size of array
int a[n+1]={0};
for(i=1;i<=n;i++)
cin>>a[i];
for(i=n;i>0;i--)
cout<<a[i];
}
Upvotes: 0
Reputation: 117298
If you are not allowed to use a std::string
to std::reverse
the number, you could store the number of digits in the original number and use the I/O manipulators std::setw()
and std::setfill()
to add the leading zeroes when you print the reversed number.
Example:
#include <iomanip>
#include <iostream>
int main(){
int n = 5430;
int reverse = 0;
int no_of_digits = 0;
bool neg = n < 0;
if(neg) n = -n;
while(n!=0) {
++no_of_digits;
reverse = reverse * 10 + (n % 10);
n = n / 10;
}
std::cout << std::setw(no_of_digits) << std::setfill('0') << reverse;
if(neg) {
reverse = -reverse;
std::cout << '-';
}
std::cout << '\n';
}
Output:
0345
Upvotes: 2
Reputation: 26
I'd advice you to first convert the number into a string and then reverse it. This is better since string variables can store leading zeros (since they're treated as strings rather than numbers) unlike int variables where leading zeros are ignored (sort of).
If you really want to store it in an int variable then you need to make another variable that will store the number of leading zeros then either generate a string that contains the amount of leading zeros you need and print it or print zeros multiple amount of times using for loop.
In both cases, however, you will need to get the digits seperately or as a string since the leading zeros would be ignored if you take them directly with cin into an int variable or if the number of digits is fixed you can just print zeros as needed since if you get the number 0010 for example the first 2 zeros will be ignored and you will have 10 instead.
Hopefully this helps, and if you need help don't be afraid to ask!
Upvotes: 1