sum up all digits until a single digit is left

Problem solved!!!
Thanks for all your help!!
Last edited on
1
2
3
while (n > 9) {//While n has at least two digits
    //do a single iteration
}
I would consider something along this line...

1. To cure your switching back and forth conundrum, a function for even numbers, and a function for odds.

2. Maybe a loop so it'll repeat by itself.

3. I'm not sure if test-n ==0 is a true test for odd or even, but you can use whatever method you like.

4. If you put your functions below main, remember to use prototypes, else just put the functions before main()

A little Pseudo code to give you an idea of my thoughts...



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>
using namespace std;
int main()

{
    int n = 0;            // initalize n
    int number=10;    // Init number;
    cout << "Please enter a number ";    //Write something so it doesn't look like the thing hung up
    cin >> n;                  // get user input
    double test = n/2;
    while number>9
{
    if (test - n = 0)
        {
            sum=even numbers
        }
        else {sum=odd numbers}
        number=sum
    }
    if number <10
{
    cout number
}

function for even numbers;
return number

function for odd numbers;
return number
Last edited on
Problem solved!!!
Thanks for all your help!!
Last edited on
You neve change your test variable inside loop.
Yes!! But how???
I tried to change it but then infinite numbers kept popping up once I ran the program...
But how???
1
2
3
4
5
double test = n%2; //--\.
while (number>9)   //   |
{                  //   |
double test = n%2; //<--/
if (test == 0)
Please forgive my stupidity...but I'm not a computer science student and have only attended 3 lectures and feel totally clueless.
I did what you said but the sample run is still wrong...
You are reading input into n and for some reason operate on number.

Your code after fixing this error and getting rid of all tose extra unneeded varibles:
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
30
31
#include <iostream>

int handle_even(int num)
{
    int sum = 0;
    do {
        sum += num % 10;
    } while ( (num /= 10) != 0 );
    return sum;

}

int handle_odd(int p)
{
    return p/2;
}

int main()
{
    int number;
    std::cout << "Please enter a number: ";
    std::cin >> number;

    while (number>9) {
        if (number % 2 == 0)
            number = handle_even(number);
        else
            number = handle_odd(number);
        std::cout << number << '\n';
    }
}
Please enter a number: 498
21
10
1
Last edited on
Problem solved!!!
Thanks for all your help!!
Last edited on
Why so rounabout way? Why so many unused variables in main? Why do you ever need different n and number variables?

Why your functions take arguments by reference? Why do you copy those arguments in separate variable? Why do you have declaration and initialization on separate lines where it could be done on one line?

And you ahve stray semicilon on line 36
Problem solved!!!
Thanks for all your help!!
Last edited on
Why number is initialized to 10? Why do you need sum and number as separate variables? Why do you need check after loop?

I already posted fixed code before. Look at it and make corresponding changes to your.
Oh finally!!! Thanks a lot!!!

But actually this is only part of my question...
I have to put this function in a program that provides the user with four options described by respective functions. So I got this option's function done. But how can I put it back into the program when itself contains two functions already?
Make it is its own function instead of main and call it from where it is needed:
#include <iostream>

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

void even_odd_sequence(int number)
{
    while (number>9) {
        if (number % 2 == 0) {
            int sum = 0;
            do {
                sum += number % 10;
            } while ( (number /= 10) != 0 );
            number = sum;
        } else
            number /= 2;
        std::cout << number << '\n';
    }
}

void hailstorm_sequence(int number)
{
    while (number != 1) {
        if(number % 2 == 0) {
           number /= 2;
        } else {
            number = (number * 3) + 1;
        }
        std::cout << number << '\n';
    }
}


int main()
{
    int number;
    std::cout << "Please enter a number: ";
    std::cin >> number;

    int choice;
    std::cout << "\n1) Even-Odd sequence"
                 "\n2) Hailstorm sequence\n\n";
    std::cin >> choice;
    switch(choice) {
        case 1: even_odd_sequence(number);  break;
        case 2: hailstorm_sequence(number); break;
    }
}
You are my lifesaver!!! Thanks!!!
I am a social science student forced to study programming....really doomed if not for your help!!!
Topic archived. No new replies allowed.