1

I'm pretty new to coding and currently have the following problem: I'm supposed to calculate the product of all numbers up to a certain number (basicly faculty) but I'm supposed to ignore all numbers that I can divide by 9 and the number 13. Also all numbers above 22 shall return -1. I wrote the following code, but it does not ignore the value 13 and I don't know why.

long long product(int val)
{
    if (val == 1)
        return 1;
    if (val <= 0 || val > 22)
        return -1;
    else
    {
        if (val % 9 == 0 || val == 13) {
            printf("if %d \n", val);
            return product(val - 1);
        }
        else
        {
            printf("else %d \n", val);
            return val * product(val - 1);
        }
    }
}

Printf were done to see whether or not it took the else or if without debugger.

mch
  • 8,786
  • 2
  • 29
  • 42
Rumapark
  • 11
  • 2
  • Have you tried running your code line by line in a debugger while monitoring the control flow and the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Feb 20 '22 at 12:55
  • 1
    "but it does not ignore the value 13" How did you come to this conclusion? – n. 1.8e9-where's-my-share m. Feb 20 '22 at 12:57
  • I'm sorry for that error @chux-ReinstateMonica, I accidently copied it after having tried a change. Edited it. – Rumapark Feb 20 '22 at 12:58
  • @n.1.8e9-where's-my-sharem. If it did the result would be "5215795200" for product(14) but it is "745113600", same for product(18) with "3040063488000" instead of "361767555072000" – Rumapark Feb 20 '22 at 13:02
  • Are you sure? Isn't `product(14)` supposed to be 14x12x11x10x8x7x6x5x4x3x2 ? If so, the result is indeed 745113600. Same goes for `product(18)`.Your code appears to work. What is the mathematical expression for the results that you consider correct? – melonduofromage Feb 20 '22 at 13:26
  • Rumapark, Your expected answer 5215795200 is 7 * [745113600](https://stackoverflow.com/questions/71194538/calculate-product-and-ignore-values-in-c?noredirect=1#comment125846407_71194538). – chux - Reinstate Monica Feb 20 '22 at 13:32
  • @melonduofromage You are right! I have sent a mail to professor, maybe her results are wrong... I should've probably just checked it as you have done with a calculator, sometimes I'm just dumb... – Rumapark Feb 20 '22 at 13:32
  • @chux-ReinstateMonica You're right but that was also a given condition. But since it didn't matter for the actual problem I experienced I did not state such, I should remember that if I ever need help again. Much appreciated! – Rumapark Feb 20 '22 at 13:38
  • 2
    Considering @chux-ReinstateMonica's comment, you requirements may contain something like "multiply by a number twice if it contains digit 7", since your expected value for `product(18)` is short by a factor of 119, i.e. 17x7. Or maybe your professor forgot to add that part but published results as so. – melonduofromage Feb 20 '22 at 13:40
  • @melonduofromage Nice deduction. – chux - Reinstate Monica Feb 20 '22 at 13:41

0 Answers0