c++ - If treble product of first 2 digits -


i writing program should able calculate product of first n-1 digits.
note: if treble first 2 digits.

the code working when 4 digits entered , example : 1234 , product 6 . cant figure out treble part

so far tried this, no solution:

 using namespace std;  int main() {  int n , sum = 0, s,d,k,w, product ; cout <<"enter number: " << endl; cin >> n;  if ((n>1) && (n<10000)) { {  s= n/10;     d = n/100%10; // 2.     k = n/1000%100%10; //1.     w = n/10%10; //3. //1234    product = w*d*k;     if ((n>1) && (n<1000)) {        s = n/10;     d = n/100%10; // 2.     k = n/1000%100%10; //1.     w = n/10%10; //3.      product = k*d;  }      cout <<"sum of digits inp numb : " << d+k+w  << " , product : " << product  <<  endl;       cout << "w: " << w << " k: " << k << " d: " << d << " s: " << s << endl;     }  }     return 0; } 

 if ((n>1) && (n<1000)) {        s = n/10;     d = n/100%10; // 2.     k = n/1000%100%10; //1.     w = n/10%10; //3.      product = k*d;  } 

this part should "treble" part. line issue:

product = k*d;. 

if n < 1000, k becomes 0%100%10 0. product 0. think intention was:

product = w*d; 

for general solution, repeatedly divide , take running product of modulus. this:

n = n/10; product = 1; while (n > 0){     product = product * (n%10);     n = n/10; } 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -