Program to.Find the.Smallest 5 digit number whose sum.Of digits is equal.To.S
Answers
Explanation:
1- take input value of s
make variable a,b,c,d,e
where a=x%10000;
b=x%1000;
c=x%100;
d=x%10;
e=x%1;
then make var sum where
sum=a+b+c+d+e;
after that use for loop
for (x=10000 ;x<=s ; x++)
{
if (sum== s)
{
printf "no. is $x";
exit;
}
}
Answer:
#include <iostream>
using namespace std;
int sumofdigits(int n)
{
int r=0,s=0;
while(n>0)
{
r = n%10;
s = s+r;
n = n/10;
}
return s;
}
int main() {
int sum;
cin >> sum;
int c = 0;
for(int i = 10000; i <=99999 ; i++)
{
if(sumofdigits(i)== sum)
{
cout<<i;
c = 1;
break;
}
}
if(c ==0)
{
cout<<"Not Possible";
}
return 0;
}
Explanation:
sumofdigits() is a function which will give the sum of the digits of a particular number.
Since the question wants only 5 digit numbers, so we have run the loop from 10000 to 99999 and passed each of the numbers in this range to sumofdigits(). if any of the numbers matches with the sum given as input then we display that number.
if no, then we print "Not Possible "