#1. Write a program that takes a non-negative long or int as input and displays each of the digits on a separate line. Note: you may not assume that the number entered will be any particular number of digits. The program output should look similar to:
Enter an integer: 7134
4
3
1
7
Hint: Here’s a few questions to guide you:
What value would you divide ANY number by to get rid of its right-most digit?
Once you have the answer above….know that performing a modulus operation by that same amount will isolate the right-most digit for you (since it is the remainder)
If you kept repeating that process, when would you know to stop?
My Program:
//Seperate from right to left
#include <iostream>
// C++ input/output library
using namespace std;
// std namespace functions being used
int main() {
cout<<"Enter an integer (Four digits): ";
// prompt the user to enter a number
char input[4];
// create a new array of characters
cin>>input;
// get four numbers into input
cout<<input[3]<<endl<<input[2]<<endl<<input[1]<<endl<<input[0];
// output each character on a different line backwards
fflush(stdin);
// flush stdin
cin.get();
// wait for user to press etner, so they see the output
return 0;
// exit program
}
#2. Write a program that takes a non-negative long or int as input and displays each of the digits on a separate line. Note: you may not assume that the number entered will be any particular number of digits. The program output should look similar to:
Enter an integer: 7134
7
1
3
4
Hint: You will need more than one loop to solve this problem:
(1) one to figure out how many digits there are (how can you remove a digit one at a time until there are none?) and
(2) one to slowly display the digits from left to right. Keep in mind that if you reduce the number during the loop that figures out the number of digits, you will need a copy of that original number for the second step.
My Program:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int number;
cout << "Enter a number to separate it's digits: ";
cin >> number;
int exponent = floor( log10( static_cast<double>(number) ) );
int divisor;
while (number != 0)
{
divisor = pow(10.0, exponent);
cout << number/divisor << " ";
number %= divisor;
exponent--;
}
cout << endl;
#include <iostream>
usingnamespace std;
void writeOne( int n, bool up )
{
if ( n < 10 )
{
cout << n << '\n';
}
else
{
if ( up ) cout << n % 10 << '\n';
writeOne( n / 10, up );
if ( !up ) cout << n % 10 << '\n';
}
}
int main()
{
int N;
cout << "Input a number: "; cin >> N;
cout << "Up: \n"; writeOne( N, true );
cout << "Down:\n"; writeOne( N, false );
}
this is code formatting tags, the <> on the side editor.
please use them, it does syntax and formatting that is cleaner than just plain text of typing junk in here without.
How is your background color blue and mine shows nothing? Where did you copy and paste the code from?
@jonnin
this is code formatting tags, the <> on the side editor.
please use them, it does syntax and formatting that is cleaner than just plain text of typing junk in here without.
@naveenmmenon aight bet thanks
@naveenmmenon
posts code again without code tags.
Pleas click the Format button that looks like "<>" and post your code between the tags.
I the initial post the button does not work. Simply type "[ code]" and "[ /code]" (without the spaces), and paste your code between the tags. It will make it easier to review your code.
By the way, you can edit your previous post. Highlight the code section and then click the "<>" button. That will insert the tags around the highlighted code.
//Seperate from right to left
#include <iostream>
// C++ input/output library
usingnamespace std;
// std namespace functions being used
int main() {
cout<<"Enter an integer (Four digits): ";
// prompt the user to enter a number
char input[4];
// create a new array of characters
cin>>input;
// get four numbers into input
cout<<input[3]<<endl<<input[2]<<endl<<input[1]<<endl<<input[0];
// output each character on a different line backwards
fflush(stdin);
// flush stdin
cin.get();
// wait for user to press etner, so they see the output
return 0;
// exit program
}
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
int exponent = floor( log10( static_cast<double>(number)) );
int divisor;
while (number != 0)
{
divisor = pow(10.0, exponent);
cout << number/divisor << " ";
number %= divisor;
exponent--;
}
cout << endl;
system("pause");
return 0;
}
My teacher commented:
Naveen - you must do this while still treating the variable as numeric - not just an array of chars - also should work for any length - please try again. She gave me a 0. Any chance you know how to fix this situation?
The two programs should be SEPERATE not COMBINED! How do you fix this mathematically?