Task:
1. In main() funtion, ask user to enter some positive number. If number is not positive, terminate the program.
2. Otherwise you call a seperate function from main() that takes number and:
-prints digits of this number;
-returns/prints the sum of digits
#include <iostream>
usingnamespace std;
int both(int n) {
int remainder;
int sum = 0;
while (n>0) {
remainder = n%10;
sum = sum + remainder;
int digit = n%10;
n = n/10;
cout << "The digits are: " << digit << endl;
}
return sum;
}
int main () {
int x;
cout <<"Enter a number: " << endl;
cin >> x;
if (x<=0){
return 0;
}
cout << "The sum of digits is: " << both(x) <<endl;
return 0;
}
The program should run like this:
1 2 3 4 5 6 7 8
Enter a number:
345
The digits are:
3
4
5
The sum of digits is:
12
The only part I am having trouble with is printing the digits of an integer because from my code, it prints in REVERSED order instead of ascending order. I need to use a while loop inside my function. Please help, thanks in advance!
you could find how many digits you have in the first place and then work your way left to right. 1111 -> 4 digits. 1111%1000, 111 % 100 ... etc
Either that or place into an array your results and print that array in reverse.
I'm sure you'll get more answers soon :)
#include <iostream>
int primt_and_sum_digits( int number )
{
if( number == 0 ) return 0 ;
constint least_significant_digit = number % 10 ; // print this at the end
// after the more significant digits have been printed
constint sum = primt_and_sum_digits( number / 10 ) + least_significant_digit ;
std::cout << least_significant_digit << ", " ;
return sum ;
}
int main()
{
for( int v : { 12, 345, 6789, 12345678 } ) // invariant: v is positive
{
std::cout << "The number is " << v << ", its digits are " ;
constint sum = primt_and_sum_digits(v) ;
std::cout << " and their sum is " << sum << ".\n" ;
}
}
#include <iostream>
#include <stack>
int primt_and_sum_digits( int number )
{
std::stack<int> digits ;
int sum = 0 ;
while( number != 0 )
{
constint least_significant_digit = number % 10 ;
digits.push( least_significant_digit ) ;
sum += least_significant_digit ;
number /= 10 ;
}
// print digits from top of the stack (reverse order of push) till it is empty
while( !digits.empty() )
{
std::cout << digits.top() << ", " ;
digits.pop() ;
}
return sum ;
}
int main()
{
for( int v : { 12, 345, 6789, 12345678 } ) // invariant: v is positive
{
std::cout << "The number is " << v << ", its digits are " ;
constint sum = primt_and_sum_digits(v) ;
std::cout << " and their sum is " << sum << ".\n" ;
}
}
#include <iostream>
usingnamespace std;
int both(int n) {
int remainder;
int sum = 0;
while (n > 0)
{
sum = (sum * 10) + n % 10;
n = n / 10;
}
n = sum;
sum = 0;
while (n>0) {
remainder = n % 10;
sum = sum + remainder;
int digit = n % 10;
n = n / 10;
cout << "The digits are: " << digit << endl;
}
return sum;
}
int main() {
int x;
cout << "Enter a number: " << endl;
cin >> x;
cin.ignore();
if (x <= 0){
return 0;
}
cout << "The sum of digits is: " << both(x) << endl;
cin.ignore();
return 0;
}