cout<<"Introduzca el numero":cin>>N:
while(N>0)
{d=N%10;
Switch(d)
{case l: cl=c1+l; break;
case 2: c2=c2+1: break:
case 3: c3=c3+1: break;
case 4: c4=c4+1; break;
case 5: c5=c5+1; break;
case 6: C6=c6+1; break;
case 7: c7=c7+1; break;
case 8: c8=c8+1; break;
case 9: c9=c9+1; break;
default:cout<<"ERROR";
}
N=N/10;
}
Cout<<0<<"se repite"<<c0<<endl;
Cout<<1<<"se repite"<<c1<<endl;
Cout<<2<<"se repite"<<c2<<endl;
Cout<<3<<"se repite"<<c3<<end];
Cout<<4«<"se repite"<<c4<<endl;
Cout<<5<<"se repite"<<c5<<endl;
Cout<<6<<"se repite"<<c6<<endl;
Cout<<7<<"se repite"<<c7<<endl;
Cout<<8<<"se repite"<<c8<<endl;
Cout<<9<<"se repite"<<c9<<endl;
The program works fine, but for example if I put 20223 I only want it to show me how many times the 0, 2 and 3 are repeated, I don't want it to show all the digits as my program does.
The following remarks are the syntax errors I found in your code:
int main()
need to initialize the program.0
.Switch
, it should beswitch
, also theCout
, it should becout
.:
, it should be;
Now let's go with the solution to your problem:
Once fixing the observations that I mentioned, I managed to compile your code and in the output they show me the numbers that are repeated and those that are not repeated, and precisely what you need is that they do not show the numbers whose are not repeated (repeats 0 times):
The solution to this would be to simply check that if the repeat times counter is different
0
, show the message, otherwise (if it's 0), don't show it:Your final code would be the following:
Output:
In the first line of your code you are using
:
. But you have to use;
to indicate the end of the statement. Otherwise you will get compile errors.You could enter a variable
minVeces
and display every variable that counted at leastminVeces
.But the code becomes even more tedious to write and analyze.
So it is better to replace the variable of each digit with an array of 10 elements, one for each digit.
This way you can generalize the code using the digit as the array position and loops to perform the same task for each of its elements.
You can also convert the procedure of counting the digits into a function to be able to reuse it and you get a code like this:
You can try it here .