Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
input:4 terms 9+99+999+9999
expected output = 11106

What I have tried:

i tried but i didnt get output.
Posted
Updated 7-Apr-21 20:37pm
Comments
Patrice T 7-Apr-21 12:42pm    
Show your code

This is not a complex task, not even slightly.
All you have been asked to do is sum a number of terms, and you know how to do that - just add them up!

1) Get the number of terms.
2) Preset the total to 0; and the initial term to 9
3) Loop from zero to N - 1
3.1) Add the term to the total.
3.2) Multiply the term by ten, and add 9.
4) Print the total.
 
Share this answer
 
Comments
Maciej Los 8-Apr-21 2:26am    
You're amazing, Master!
private int Nsum(int cnt)
{
char pad = '9';
string str = pad.ToString();

int rtnValue = 0;

for (int i = 0; i < cnt; i++)
{
rtnValue = rtnValue + int.Parse(str.PadLeft(i + 1, pad));
}

return rtnValue;
}
 
Share this answer
 
Comments
Maciej Los 8-Apr-21 2:27am    
Do you really want to work on chars?
Jung-hoon's 8-Apr-21 20:22pm    
Is there a simpler way?
Maciej Los 9-Apr-21 0:16am    
Yes. Please, follow the instruction in solution #1.
[EDIT]
For example:
int inp = 4;
int term = 9;
int temp = term;
int total = term;
for(int i=0; i<inp; i++)
{
	temp *= 10;
	temp += term;
	Console.WriteLine($"current={total}; next={temp}");
	total += temp;
}
Jung-hoon's 9-Apr-21 2:59am    
First of all, I'm sorry I'm not good at English.

Solution 1 is the basic method.
Faced with the problem, I thought it would be fun to use char.

You thought there was a new method that wasn't basic.
i'm sorry ^^
Jung-hoon's 9-Apr-21 3:03am    
Thank you for responding to my playful answer.

You are a good man ^^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900