Codeforces 110A: Lucky Number Problem Solution in C++
Codeforces problem 110A requires us to determine whether a given number is lucky or not. A lucky number is defined as a number that contains only the digits 4 and 7, and the total count of these digits is either 4 or 7.
To solve this problem, we need to first read the input number. We can then loop through each digit of the number and check if it is a 4 or a 7. We can do this by using the modulo operator (%
) to get the remainder of the number when divided by 10, which gives us the rightmost digit of the number. If the digit is a 4 or a 7, we increment a counter variable.
After we have counted the number of 4s and 7s in the input number, we can check if the count is either 4 or 7. If it is, then the number is lucky, and we output “YES”. Otherwise, the number is not lucky, and we output “NO”.
Here’s the complete code in C++:
#include <iostream>
using namespace std;
int main()
{
long long number;
cin >> number;
int count = 0;
while (number != 0)
{
if (number % 10 == 4 || number % 10 == 7)
{
count += 1;
}
number /= 10;
}
if (count == 4 || count == 7)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
return 0;
}
In conclusion, the above code solves Codeforces problem 110A by counting the number of 4s and 7s in a given input number and checking if the count is either 4 or 7.
If you found this article helpful and informative, feel free to follow me on my social media accounts for more updates and insights on web development:
- Facebook: https://www.facebook.com/ahsan.aasim.03
- LinkedIn: https://www.linkedin.com/in/ahsan-aasim
- Medium: https://medium.com/@ahsan.aasim
- GitHub: https://github.com/ahsanaasim
Thank you for taking the time to read this article. I wish you all the best in your learning journey!