-1

The question is to find the number of interesting numbers lying between two numbers. By the interesting number, they mean that the product of its digits is divisible by the sum of its digits.

For example: 459 => product = 4 * 5 * 9 = 180, and sum = 4 + 5 + 9 = 18; 180 % 18 == 0, hence it is an interesting number.

My solution for this problem is having run time error and time complexity of O(n2).

#include<iostream>
using namespace std;
int main(){
    int x,y,p=1,s=0,count=0,r;
    cout<<"enter two numbers"<<endl;
    cin>>x>>y;
    for(int i=x;i<=y;i++)
     {
            r=0;
         while(i>1)
           {
            r=i%10;
            s+=r;
            p*=r;
            i/=10;
           }
        if(p%s==0)
        {
            count++;
        }
    }
    cout<<"count of interesting numbers are"<<count<<endl;
    return 0;
}
codeling
  • 11,056
  • 4
  • 42
  • 71
  • 2
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 14 '22 at 05:57
  • 1
    "is having run time error" -> run it [with a debugger](https://stackoverflow.com/questions/25385173) to find out where the problem is and what kind of problem it is – codeling Apr 14 '22 at 06:04
  • Apart from duration, does it give correct answers? The handling of `i` seems worrying. Can you summarize the idea of what you implemented? It looks like it might count numbers below the start of the range as interesting. – Yunnosch Apr 14 '22 at 06:06
  • Cannot reproduce. E.g. here https://www.onlinegdb.com your code does not finish successfully (no output of number) for e.g. 10 20. If you ask about speed an O(), your program should otherwise be correct. – Yunnosch Apr 14 '22 at 06:19
  • My guess would be the input is `0` resulting in `p%s` generating a divide by zero. It'd be simpler to take the inputs as strings then you can just process them character by character rather than having to do the divisions and mods – Alan Birtles Apr 14 '22 at 06:19
  • 1
    You are modifying `i` inside a for-loop that runs on `i`. This is not what you want. – n. m. could be an AI Apr 14 '22 at 06:28

1 Answers1

0

If s is zero then if(p%s==0) will produce a divide by zero error.

Inside your for loop you modify the value of i to 0 or 1, this will mean the for loop never completes and will continuously check 1 and 2.

You also don't reinitialise p and s for each iteration of the for loop so will produce the wrong answer anyway. In general limit the scope of variables to where they are actually needed as this helps to avoid this type of bug.

Something like this should fix these problems:

#include <iostream>

int main()
{
    std::cout << "enter two numbers\n";
    int begin;
    int end;
    std::cin >> begin >> end;
    int count = 0;
    for (int number = begin; number <= end; number++) {
        int sum = 0;
        int product = 1;
        int value = number;
        while (value != 0) {
            int digit = value % 10;
            sum += digit;
            product *= digit;
            value /= 10;
        }
        if (sum != 0 && product % sum == 0) {
            count++;
        }
    }
    std::cout << "count of interesting numbers are " << count << "\n";
    return 0;
}

I'd guess the contest is trying to get you to do something more efficient than this, for example after calculating the sum and product for 1234 to find the sum for 1235 you just need to add one and for the product you can divide by 4 then multiply by 5.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60