ok, so I'm a new programmer and I'm having trouble with my project, here are my instructions:
"Write a program that asks the user to input a number between 1 and 5,000. If the user enters a number greater than 5,000, the program terminates with an appropriate message. If the number is valid, the program displays the thousands digit, hundreds digit, tens digit and ones digit of the number, each on a separate line. Finally, the program displays the original number reversed on one line."
here is an example of what the run should look like:
Please enter a number between 1 and 5000: 3927
Here are the digits one by one
3
9
2
7
Here is the number in reverse 7293
Press any key to continue
We were hinted to use division rules and operator precedence rules but being a new programmer I don't really know how.
n%10 ( n is an integral number and % is the C++ operator to get the remainder of a division ) will return the last decimal digit of 'n'. To get the second digit, divide 'n' by 10 and start again
that's what I have been trying to do. I have been fooling around trying to divided the imputed number by 1000, then display it, then do the imputed number % by 1000 but I can't seem to get it right. this is what I'm workign with so far (although it doesn't even work right, its the idea im trying to display)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int user;
int one;
int two;
cout<<"enter number";
cin>>user;
one = user / 1000;
two = user % 1000;
cout<<"here "<<one<<two<<endl;
return 0;
}
remember I am a very new programmer haha so bare with me please
I can get the first digit, if i input 3927, i will get 3 but then ont he next line i get 927, but i only want the 9, how can i go about just getting the 9 out of the equation, or do i need to figure out a whole other equation then what i got going now
ooo so i take the remainder which would be 927 then divide that by 100 which gives me 9.27 but declaring it an integer would give me 9, then I just keep going from there?
ahh one more problem =[ ok so this is my program:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
int one;
int two;
int three;
int four;
int five;
int six;
int seven;
int eight;
int nine;
int ten;
cout<<"enter number"<<endl;
cin>>n;
one = n / 1000;
two = n % 1000;
three = two / 100;
four = two % 100;
five = four / 10;
six = four % 10;
seven = n % 10;
eight = five % 10;
nine = three %10;
ten = one % 10;
cout<<"here are the digits one by one "<<endl<<one<<endl<<three<<endl<<five<<endl<<six<<endl;
cout<<"here is the number in reverse " <<seven<<eight<<nine<<ten<<endl;
return 0;
}
and it does what its suppose to if i enter a four digit number, but when i use a three,two, or one digit number, 0's are displayed when the number is written in reverse and that is not allowed. how do i fix this?
Since your four digits are in the variables "one", "three", "five", and "six",
you don't output "one" if it is zero. You don't output "three" if both it AND
"one" are zero. And so forth.
Think a little bit more about why you do not need the variables "seven" through
"ten".