0

I have to input 2 numbers, n and k, and find the product of n's digits which are different from k. Example: n = 1234, k=2, and the product should be 1 * 3 * 4;

    #include <iostream>

using namespace std;

int main()
{
    int n, k, p=1, digit;
    cin >> n >> k;

    while(true){
            digit= n % 10;

        if(digit != k){
            p = p * digit;
        }
        n = n/10;
    }
    cout << "Product of digits different from K is: " << p;
    return 0;
}

When i run it, after i input n and k, the program doesnt do anything else and just keeps the console open without an output.

MickeyMoise
  • 259
  • 2
  • 13

1 Answers1

1

The problem is the while(true). In this way the program stay in the loop for eternal. A possible solution can be this:

int main()
{
    int n, k, p=1, digit;
    cin >> n >> k;

    while( n > 0 ){
            digit= n % 10;

        if(digit != k){
            p = p * digit;
        }
        n = n/10;
    }
    cout << "Product of digits different from K is: " << p;
    return 0;
}
Zig Razor
  • 3,381
  • 2
  • 15
  • 35