Reversing a multi digit number

#include<iostream>
using namespace std;

int main() {
int number, reverse = 0;
cout<<"Input a Number to Reverse and press Enter: ";
cin>> number; // Taking Input Number in variable number

for( ; number!= 0 ; )
{
reverse = reverse * 10;
reverse = reverse + number%10;
number = number/10;
}
cout<<"New Reversed Number is: "<<reverse;
return 0;

}


problem is that question says that
program should store each digit on a separate index of an array
for example if user enters 12 then output should be 21 and array index 0 should contain 2 and array index 1 should contain 1
Last edited on
closed account (37oyvCM9)
try storing them in a string
Can you update how to do so ..
closed account (48bpfSEw)
http://www.cplusplus.com/reference/algorithm/reverse/
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

int main()
{
    int number = 0, index = 0;    
    int digit[100] = {0};
    
    std::cout<<"Input a Number to Reverse and press Enter: ";
    std::cin>> number; // Taking Input Number in variable number

    while( number!= 0 )
    {
        digit[index] = number % 10;
        number = number/10;
        index++;
    }
    
    std::cout << "New Reversed Number is: ";
    for (int i = 0; i < index; i++)
        std::cout << digit[i];
    
    return 0;
}
Input a Number to Reverse and press Enter: 45678
New Reversed Number is: 87654 
Exit code: 0 (normal program termination)
Question is still there
problem is that question says that for all multi-digit numbers
program should store each digit on a separate index of an array
for example if user enters 12 then output should be 21 and array index 0 should contain 2 and array index 1 should contain 1
Thnxx for co-ordination ...
closed account (48T7M4Gy)
Living proof:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>

int main()
{
    int number = 0, index = 0;    
    int digit[100] = {0};
    
    std::cout<<"Input a Number to Reverse and press Enter: ";
    std::cin>> number; // Taking Input Number in variable number

    while( number!= 0 )
    {
        digit[index] = number % 10;
        number = number/10;
        index++;
    }
    
    std::cout << "New Reversed Number is: ";
    
    for (int i = 0; i < index; i++)
        std::cout << digit[i];
        
    std::cout << '\n';
        
    for (int i = 0; i < index; i++)
        std::cout << "digit[" << i << "] = " << digit[i] << '\n';
    
    return 0;
}

Input a Number to Reverse and press Enter: 123
New Reversed Number is: 321
digit[0] = 3
digit[1] = 2
digit[2] = 1
 
Exit code: 0 (normal program termination)
What does kemort's program actually do?

It will fail with certain inputs. What inputs and why?
closed account (48T7M4Gy)
LOL
Problem is still there
problem is that question says that for all multi digit numbers,
program should store each digit on a separate index of an array
for example if user enters 12 then output should be 21 and array index 0 should contain 2 and array index 1 should contain 1


Thanks for co ordination
closed account (48T7M4Gy)
Input a Number to Reverse and press Enter: 12
New Reversed Number is: 21
digit[0] = 2
digit[1] = 1
 
Exit code: 0 (normal program termination)
@Kemort you are really great it really worked.....
Last edited on
closed account (48T7M4Gy)
I'm glad I was able to help, but keskiverto's questions are still relevant. Maybe it's not so good for numbers bigger than int, not so good for negative numbers, and not too good for 0.

But those possibilities can be fixed easily with a couple of extra lines of code ;)
Last edited on
@salmanasghar12:
Please explain each construct in kemort's code. If you cannot do that and simply did copy-paste the code, then you don't know the answer. If you don't know and understand the answer, then you have not learned. To learn is what you have to do.
@kemort can you explain these things denoted with comment lines // because i couldn't understand them if i tell sincerely

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>

int main()
{
    int number = 0, index = 0;    
    int digit[100] = {0}; // why did u take [100] and take it as {0}
    
    std::cout<<"Input a Number to Reverse and press Enter: ";
    std::cin>> number; // Taking Input Number in variable number

    while( number!= 0 )
    {
        digit[index] = number % 10;//why did u take index inside those beackets
        number = number/10;
        index++;
    }
    
    std::cout << "New Reversed Number is: ";
    
    for (int i = 0; i < index; i++) // how this loop worked...
        std::cout << digit[i];
        
    std::cout << '\n';
        
    for (int i = 0; i < index; i++)
        std::cout << "digit[" << i << "] = " << digit[i] << '\n';
    
    return 0;
}




//other thing is can't you do anything to following program to make required output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;

int main() {
int number, reverse = 0;
cout<<"Input a Number to Reverse and press Enter: ";
cin>> number; // Taking Input Number in variable number

for( ; number!= 0 ; )
{
reverse = reverse * 10;
reverse = reverse + number%10;
number = number/10;
}
cout<<"New Reversed Number is: "<<reverse;
return 0;

}
closed account (48T7M4Gy)
// why did u take [100] and take it as {0}

I chose 100 as an arbitrarily large size for the array. For an int you won't need anywhere that number. I initialise the array to zeroes everywhere because it is good practice by avoiding undefined behaviour, as unlikely as that would be in this case.

//why did u take index inside those beackets

I took index (= 0 to start off with) inside those brackets so I could store the digits in the array as required.

// how this loop worked...

Just the same as any for loop. Check out a tutorial on the subject if it's not clear. Note: index in the loop is the index to the final digit location which started at 0 and ended up at index - 1.

//other thing is can't you do anything to following program to make required output

No I can't, that is your job. :)
Great really

Nice expalanation...love it..
U rock....
Last edited on
Topic archived. No new replies allowed.