Qwerasdzxc
Qwerasdzxc

Reputation: 55

Counting Lucky Numbers - C++

I want to make a program that follows this rule:

Given an input 'n' from the user, you need to find all lucky numbers from 1 to n. Lucky numbers are those which have either 4 or 7 or both as their digits.

I made this but can't manage how to do it for numbers greater than 10.

#include<iostream>

using namespace std;

int main()
{
    int digits, n, count=0;
    cout << "Enter upper limit: ";
    cin >> n;

    for (int i=0; i<n; i++){
        if((i==4) || (i==7)){
            count++;
        }
    }
    cout << "Number of lucky numbers in given range is " << count;

    return 0;
}

Upvotes: 2

Views: 7395

Answers (4)

Kareem Zayed
Kareem Zayed

Reputation: 11

#include<iostream>
using namespace std;
int main()
{
    int a, b, count = 0;
    bool found_lucky = false;
    cin >> a >> b;
    for (int i = a; i <= b; i++) {
        int j = i, temp_count = 0, digits = 0;
        while (j > 0) {
            digits++;
            if ((j % 10 == 4 || j % 10 == 7))
                temp_count++;
            j = j / 10;
        }
        if (temp_count == digits) {
            count++;
            cout << i << " ";
            found_lucky = true;
        }
    }
    cout << endl << "The count of lucky numbers = " << count << endl;
    if (!found_lucky)
        cout << -1;
    return 0;
}

Upvotes: 0

Dan
Dan

Reputation: 26

I was doing that exercise right now. I took your initial suggestion and expanded on it to get the following (to be inserted in place of your for loop):

for (int i=0; i<=n; i++){
        int j = i;
        while(j > 1) {
            if((j % 10 == 4 || j % 10 == 7)) {
                count++;
                break;
            }
            j = j / 10;
        }
    }

This seems to get the intended answer. However, when trying to check the solution, the site claims that the result is wrong for n = 74 (it claims the correct answer is 15, but counting by hand I'm pretty sure it's 27, which is also what this code returns). I didn't figure out what I did wrong so I've tried to log a ticket with the site.

Upvotes: 1

Christian Abella
Christian Abella

Reputation: 5797

@Steven approach is really good but you can also replace your input to char array instead of int and use cin::getline function.

int main()
{
    int n;
    int count = 0;

    cout << "Enter upper limit: ";
    cin >> n;

    for (int i = 1; i <= n; i++)
    {
        char szBuffer[100];
        sprintf_s(szBuffer, "%d", i);
        if ( strstr(szBuffer,"4") != NULL || strstr(szBuffer,"7") != NULL )
        {
            count++;
        }
    }
    cout << "Number of lucky numbers in given range is " << count;

    return 0;
}

Upvotes: 1

Steven
Steven

Reputation: 13769

Format the integer into a string. Then, test the string for the required characters.

Bonus approach (integer-only): You could use logarithms, division, modulus, subtraction, etc to iterate through and test each digit.

Upvotes: 8

Related Questions