forked from manishaparuthi/manisha_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargestNo.cpp
60 lines (56 loc) · 1012 Bytes
/
largestNo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//Largest number with given sum
using namespace std;
#include <iostream>
int power(int base,int exponent)
{
int ans=1;
for(int i=0;i<exponent;i++)
{
ans=ans*base;
}
return ans;
}
int main()
{
int number;
int add;
cout<<" enter number of digits : ";
cin>> number;
cout<<" enter sum of digits: ";
cin>>add;
int temp;
int result=0;
if(add>number*9)
{
cout<<"no such number exist:";
}
else
{
while(number>0)
{
temp=power(10,number-1);
if(add>=9)
{
//cout<<" temp is: "<< temp;
temp=temp*9;
//cout<<"multiplying: "<<temp;
add=add-9;
result=result+temp;
//cout<<" rsult: "<<result;
}
else if(add<9)
{
//cout<<" temp is: "<< temp;
temp=temp*add;
//cout<<"multiplying: "<<temp;
result=result+temp;
//cout<<" rsult: "<<result;
break;
}
number=number-1;
}// end while
cout<<endl;
cout<<"largest number with given sum of digits is: "<<result;
}// end else
return 0;
}