0

C++ task about mat and numbers

to create a program that imports 10 natural numbers from the keyboard and determines the product of the numbers in which the number one is encountered 3 times this is the task. #include <iostream> using namespace std; int main() { int p=1, n, r, k=0, s=1; for (int i=0; i<10; i++){ cin>>n; int b=n; while(b!=0){ r=b%10; if(r==1){ k++; } b=b/10; } if (k==3){ s=s*n; } } cout<<s; return 0; } and i don't know where is my mistake maybe in counting one number? Need help

14th Jan 2019, 2:30 PM
Arsen
12 Antworten
+ 2
Okay Arsen try this: #include <iostream> using namespace std; int main() { int n, r; long long s = 1; for (int i = 0; i < 10; i++) { cin >> n; int b = n, k = 0; while(b) { r = b % 10; if(r == 1) { if(++k == 3) break; } b /= 10; } if (k == 3) { s = s * n; } } cout << "Product: " << s; return 0; }
14th Jan 2019, 4:24 PM
Ipang
+ 2
Arsen "while(b)" works the same way with "while(b != 0)" it checks whether <b> is non zero and continue the loop if it is. A mild difference be that "while(b)" doesn't compare <b> with zero, by skipping the comparison it works slightly faster.
14th Jan 2019, 4:47 PM
Ipang
+ 1
I'm not really understanding your intro, to prevent misunderstanding, some questions first; Assume <n> = 3111 a number having 3 digit (1), then you want the product, but what number used to multiply with <n> to obtain the product? What if <n> has more than 3 digit (1), does it meet the requirement? or it is necessarily 3 digit (1)?
14th Jan 2019, 3:00 PM
Ipang
+ 1
They multiply by what? for example <n> = 3111 then multiply <n> with itself (squared) or <n> * 3 (number of digit (1)?
14th Jan 2019, 3:35 PM
Ipang
+ 1
Give me one example please, I still don't understand what to multiply the number with, I'm confused XD
14th Jan 2019, 3:42 PM
Ipang
+ 1
Okay now I understand what you want : ) Let me see that code again and I'll get back to you later ...
14th Jan 2019, 3:48 PM
Ipang
+ 1
Ok ipang but why you write w while (b) What does it mean
14th Jan 2019, 4:42 PM
Arsen
0
Only 3 digits of 1 number not more and not less
14th Jan 2019, 3:17 PM
Arsen
0
And if my numbers have 3 digits 1 number they multiply
14th Jan 2019, 3:18 PM
Arsen
0
No I am going to import 10 natural numbers chek if my numbers have 3 digits of 1 number they are going to multiply
14th Jan 2019, 3:38 PM
Arsen
0
For example I have 3245 3411 2311 3111 121 111 234 890 765 340. This are my 10 natural numbers. If they have 3 digits of the number 1 they multiply. In my example this numbers are 3111 and 111 and answer is 3111*111
14th Jan 2019, 3:45 PM
Arsen
0
Ok
14th Jan 2019, 3:49 PM
Arsen