-4
#include <iostream>

using namespace std;

int main()
{
  int a = 101;
  return 0;
}

Question : How do I know that the number (1) is repeated twice in the variable

  • 4
    You use your eyeballs and determine it. If you want a more programmatic answer, you need to post a question that's more specific than this... such as posting what you've tried and how it was different from your expectations. – mah Jul 05 '13 at 11:24
  • This may help you decide: [Is Anything Real?](http://www.youtube.com/watch?v=L45Q1_psDqk) – paddy Jul 05 '13 at 11:24
  • 1
    @mah your eyeballs :D –  Jul 05 '13 at 11:25
  • Take a look at [What is the fastest way to check for duplicate digits of a number?](http://stackoverflow.com/questions/4801487/what-is-the-fastest-way-to-check-for-duplicate-digits-of-a-number) – Parag Bafna Jul 05 '13 at 11:27
  • #include #include using namespace std; int main() { int a = 101; std::string s = std::to_string(a); size_t n = std::count(s.begin(), s.end(), '1'); //now you have number of "1" in n. Do whatever you want this. return 0; } – huseyin tugrul buyukisik Jul 05 '13 at 11:31

2 Answers2

8

If you look at the code, you will see that the number 101 is assigned to the variable a and that number has the digit 1 twice in its decimal representation. So direct inspection is the way to go. I wouldn't even write the code for such a trivial requirement.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3

Use modulus 10 and division 10 to find it. Rough idea is,

while( a > 0 )
{
    if( a % 10 == 1 )count_one++;
    a=a/10;
}
VoidPointer
  • 3,037
  • 21
  • 25