Future of world with technology grow, thrive, empower yourself, one step at a time. Sucess unlocked.
The journey of self-motivation and personal growth is a lifelong path, filled with twists and turns, triumphs and setbacks. By embracing this journey, we can develop the skills, confidence, and resilience needed to achieve our goals and live a fulfilling life. I hope that my insights and experiences will inspire and motivate you to embark on your own journey of self-discovery and growth. Join me as I share insights, experiences, and practical tips on living a fulfilling life.
Friday, December 13, 2019
Write a C++ program to find the smallest number that can be formed from given sum of digits and number of digits.
Find smallest number with given number of digits and sum of digits
Write a C++ program to find the smallest number that can be formed from given sum of digits and number of digits.
#include
using namespace std;
// Prints the smallest possible number with digit sum 's'
// and 'm' number of digits.
void findSmallest(int m, int s)
{
// If sum of digits is 0, then a number is possible
// only if number of digits is 1.
if (s == 0)
{
(m == 1)? cout << "Smallest number is " << 0
: cout << "Not possible";
return ;
}
// Sum greater than the maximum possible sum.
if (s > 9*m)
{
cout << "Not possible";
return ;
}
// Create an array to store digits of result
int res[m];
// deduct sum by one to account for cases later
// (There must be 1 left for the most significant
// digit)
s -= 1;
// Fill last m-1 digits (from right to left)
for (int i=m-1; i>0; i--)
{
// If sum is still greater than 9,
// digit must be 9.
if (s > 9)
{
res[i] = 9;
s -= 9;
}
else
{
res[i] = s;
s = 0;
}
}
// Whatever is left should be the most significant
// digit.
res[0] = s + 1; // The initially subtracted 1 is
// incorporated here.
cout << "Smallest number is ";
for (int i=0; i cout << res[i];
}
// Driver code
int main()
{
int s = 9, m = 2;
findSmallest(m, s);
return 0;
}
Output :
Smallest number is 18
Write a C++ program to find the smallest number that can be formed from given sum of digits and number of digits.
#include
using namespace std;
// Prints the smallest possible number with digit sum 's'
// and 'm' number of digits.
void findSmallest(int m, int s)
{
// If sum of digits is 0, then a number is possible
// only if number of digits is 1.
if (s == 0)
{
(m == 1)? cout << "Smallest number is " << 0
: cout << "Not possible";
return ;
}
// Sum greater than the maximum possible sum.
if (s > 9*m)
{
cout << "Not possible";
return ;
}
// Create an array to store digits of result
int res[m];
// deduct sum by one to account for cases later
// (There must be 1 left for the most significant
// digit)
s -= 1;
// Fill last m-1 digits (from right to left)
for (int i=m-1; i>0; i--)
{
// If sum is still greater than 9,
// digit must be 9.
if (s > 9)
{
res[i] = 9;
s -= 9;
}
else
{
res[i] = s;
s = 0;
}
}
// Whatever is left should be the most significant
// digit.
res[0] = s + 1; // The initially subtracted 1 is
// incorporated here.
cout << "Smallest number is ";
for (int i=0; i
}
// Driver code
int main()
{
int s = 9, m = 2;
findSmallest(m, s);
return 0;
}
Output :
Smallest number is 18
Subscribe to:
Comments (Atom)
Featured posts
Ethiopian culture calendar language
Ethiopian culture, calendar, language The Ethiopian language, specifically Amharic , uses a script called Ge'ez script . It consists of...
Popular posts
-
C program in data structure for polynomial subtraction #include<stdio.h> #include<conio.h> void main() { int a[10],b[10],...
-
Solved practical slips of 12th Computer Science journal Program 1:- Write a function in C++ that exchanges data (passing by refer...
