- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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; } } cout << "Answer = "; 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
- Related Articles
- Find smallest number with given number of digits and sum of digits in C++
- Convert to number with digits as 3 and 8 only in C++
- Find the Largest number with given number of digits and sum of digits in C++
- Finding n-th number made of prime digits (2, 3, 5 and 7) only in C++
- Find n-th element in a series with only 2 digits (and 7) allowed in C++
- Find minimum number of currency notes and values that sum to given amount in C++
- Check if the given decimal number has 0 and 1 digits only in Python
- Print numbers with digits 0 and 1 only such that their sum is N in C Program.
- C++ Program to Sum the digits of a given number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- C Program to sum the digits of a given number in single statement
- What is the minimum and maximum number of digits in the sum if we add any two 3 digit number
- Maximize the given number by replacing a segment of digits with the alternate digits given in C++
- Find the Number With Even Sum of Digits using C++
- Find a number x such that sum of x and its digits is equal to given n in C++

Advertisements