Simple C++ Design Question for Numerology App

Started by
13 comments, last by Xai 10 years, 7 months ago

Hi,

Very simple question on how best to go about this with c++.

I'd like to make an app where a user enters their year, month, and date of birth, and then return 3 numbers:

-Entire birth day condensed down to one digit.

-Date of birth condensed to one digit.

-Month and date of birth condensed to one digit.

I am completely unsure which containers, classes, and functions could be used to accomplish this, as I am a complete noobie.

If anyone could make any suggestions, they would be greatly appreciated! :)

Thanks,

Kev

Advertisement

-Entire birth day condensed down to one digit.

-Date of birth condensed to one digit.

-Month and date of birth condensed to one digit.

...
What does this even mean?

Perhaps a few examples might clear things up...

Absolutely!

If I was born on August 4, 2097, then I would like those numbers (ie. 8/4/2097) all condensed to one digit.

Condensing is basically saying that each digit is added up, until the result is a single digit.

In this example. that would mean:

8+4+2+0+9+7, which would give you

30

Then, since I only want one digit, it would condense to the number 3.

This is what I want my program to accomplish, given the entire birthday, as well as for just the date, and then again for the month and the day together.

It's very basic, I just don't know how to go about it.

Basically, what I'd like to know is, if I have something like this:

int reduceAllToOneDigit(int x, int y, int z)

{

int use;

use = x+y+z;

if(use>9)

//What to put here?

else

cout << "Life Path #:\t" << use << ".";

return use;

}

What could I use (maybe an iterator?) to iterate from the beginning digit to the end digit, to add them all up, one by one, until they are finally condensed down to one digit? A while loop would make more sense there, as well, I just realized, instead of an if loop.

To get each digit, look at the modulus division operator %.

For example:

int lastDigit = x % 10;

if x = 1234, lastDigit will be 4.

so for your loop, perhaps something like this:

while( x != 0)

{

sum += x % 10;

x /= 10;

}

You will need to repeat this on the sum if it is over 10, and repeat that again if the next sum is over 10.

That formula doesn't seem to work with 2-digit numbers, such as 26.

I appreciate the help, though.

How so? 26 % 10 = 6, puts your sum at 6, divide x by 10, x is now 2(integer division), 2 % 10 = 2, sum is now 6 + 2 = 8, dividing x by 10 again gives you x = 0 and the loop breaks.

int condense_positive_number(int n) {
  return 1 + (n - 1) % 9;
}

int condense_positive_number(int n) {
  return 1 + (n - 1) % 9;
}

Awesome, how did you come up with that?

All the powers of 10 are congruent to 1 modulo 9, which means that a number and the sum of its digits are congruent modulo 9. If you iterate this enough times, you'll end up with a single digit that depends only on the congruence class modulo 9 of the original number. The subtracting 1 and adding 1 part are there to return `9' instead of `0' for multiples of 9.

I haven't watched the whole thing, but this seems like a reasonable explanation, if the above didn't make sense:

This topic is closed to new replies.

Advertisement