0

Needle in the haystack. I'm a beginner in programming and we only learned a thing or two so far, barely reached arrays yet.

Input: 1 4325121 Output: 2 
  1. Input two values in one line. The first one shall accept any integer from 0-9 and the other one shall take a random positive integer.
  2. Using a while loop, count how many of the first integer (0-9) is present in the digits of the second inputted integer and print the result.

No arrays to be used here, only while loops and else-if conditions with basic coding knowledge and without the use of advanced coding.

2
  • 1
    Write down a "random positive integer" on some paper, and try to figure out how to get one digit at a time from that number. Hint: It's all about simple arithmetic (division and remainder). Commented Dec 6, 2021 at 7:27
  • Tip2: modulus 10. Commented Dec 15, 2021 at 7:31

3 Answers 3

2

As you said, you need to keep it as simple as possible. Then this can be a solution:

#include <iostream> int main() { int first { }; int second { }; std::cin >> first >> second; int quo { second }; int rem { }; int count { }; while ( quo > 0 ) { rem = quo % 10; quo /= 10; if ( first == rem ) { ++count; } } std::cout << "Result: " << count << '\n'; } 
Sign up to request clarification or add additional context in comments.

9 Comments

This is the right answer, thank you. Can you explain why there appears to be curly brackets on int first { } ;?
@Julian Williams Those curly brackets are initializers. int first { } is the equivalent of int first = 0 but it's possibly more optimized and idiomatic. In short, an empty pair of curly brackets initializes the variable with 0. You should almost always initialize the variables. Otherwise, they can cause problems.
@JulianWilliams Do you understand what the code does, and why it does what it does? This is the important part, or otherwise you're in a cargo cult which is a bad thing.
@digito_evo To prevent cargo cult programming, you should preferably explain what you do, and why you do it.
@Julian Williams The if condition is there to check to see if we have found a digit in the second number that is the same as the first number. Because rem is holding one of the digits of the second number at every iteration of the loop. So we need to compare it with the first number to see if they're equal or not.
|
0

Using while loop

 #include <iostream> using namespace std; int main() { int a = 1; int b = 4325121; int count = 0; while(b > 0) { int m = b % 10; if(m == a) { count++; } b /= 10; } cout << count; return 0; } 

Comments

-1

Nice little problem. But actually, to keep it as simple as possible no calculations are needed at all. I simplified my example, and it just keeps working on the input text, which is 100% sufficient to solve the problem:

#include <iostream> #include <string> using namespace std; int main() { char digit; std::string number; cout << "Input: "; cin >> digit >> number; int count = 0; for (char const character : number) if (character == digit) count++; cout << "Result: " << count << endl; return 0; } 

Given the question, this code solves the problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.