# Minimum number with digits as and 7 only and given sum in C++

## Problem statement

Lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. The task is to find minimum lucky number has the sum of digits equal to n.

## Example

If sum = 22 then lucky number is 4477 as 4 + 4 + 7 + 7 = 22

## Algorithm

1. If sum is multiple of 4, then result has all 4s.
2. If sum is multiple of 7, then result has all 7s.
3. If sum is not multiple of 4 or 7, then we can subtract one of them till sum becomes multiple of other.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
void luckyNumber(int sum) {
int a, b;
a = b = 0;
while (sum > 0) {
if (sum % 7 == 0) {
++b;
sum = sum - 7;
} else
if (sum % 4 == 0) {
++a;
sum = sum - 4;
} else {
++a;
sum = sum - 4;
}
}
if (sum < 0) {
cout << "-1\n" << endl;
return;
}
for (int i = 0; i < a; ++i) {
cout << "4";
}
for (int i = 0; i < b; ++i) {
cout << "7";
}
cout << endl;
}
int main() {
int sum = 22;
luckyNumber(sum);
return 0;
}

When you compile and execute above program. It generates following output

## Output

Answer = 4477