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
#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 ...
#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)
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
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 ;)
@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.
#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>
usingnamespace 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;
}
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