sum in c#
I want to sum 2 digits (001,002). but it show result = 2. I need '002' behalf of '2'.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ramesh MaruthiPosted Aug 18, 2015, 9:34 AM
{
a = 001, b = 002;
int result = a + b;
string PaddedResult = result.ToString().PadLeft (3, '0');
Console.WriteLine(PaddedResult); // output 003
return PaddedResult;
}
Rahul PrajapatPosted Aug 18, 2015, 9:32 AM
string number1 = Console.ReadLine();
Console.WriteLine("Enter Second Number");
string number2 = Console.ReadLine();
int count = number1.TakeWhile(c => c == '0').Count();
int count2 = number2.TakeWhile(c => c == '0').Count();
if (count < count2)
Console.WriteLine((int.Parse(number1) + int.Parse(number2)).ToString().PadLeft(count2+1, '0'));
else
Console.WriteLine((int.Parse(number1) + int.Parse(number2)).ToString().PadLeft(count+1, '0'));
Suket ShahPosted Aug 18, 2015, 9:23 AM
Nikolay SavinPosted Aug 18, 2015, 9:19 AM
Afzaal Ahmad ZeeshanPosted Aug 18, 2015, 9:13 AM