Forums » .NET » ASP.NET »

Program to find the sum of digits of a given number until the sum becomes a single digit.


Posted Date: 25 Apr 2013      Posted By:: divya     Member Level: Bronze    Member Rank: 4471     Points: 1   Responses: 6



Program to find the sum of digits of a given number until the sum becomes a single digit. (e.g. 12345=>1+2+3+4+5=15=>1+5=6) in c#Asp.net 4.0



Responses

#713764    Author: Shine S      Member Level: Gold      Member Rank: 29     Date: 25/Apr/2013   Rating: 2 out of 52 out of 5     Points: 4

you can call the function recursively till the sum become single char.
see the code below.


int sum = 0;
private void button1_Click(object sender, EventArgs e)
{
int res=GetSumOfDigits(12345);
}


private int GetSumOfDigits(int inputNum)
{
sum = 0;
char[] chars = inputNum.ToString().ToCharArray();

for (int i = 0; i < chars.Length; i++)
{
sum = sum + int.Parse(chars[i].ToString());

}
if (sum.ToString().Length > 1)
{
GetSumOfDigits(sum);
}

return sum;
}


Regards,
Shine


 
#713766    Author: navas      Member Level: Silver      Member Rank: 578     Date: 25/Apr/2013   Rating: 2 out of 52 out of 5     Points: 4

try this

static void Main(string[] args)
{
Console.WriteLine("Please enter a number");
string theNumber = Console.ReadLine();

int adder;

do
{
adder = 0;

foreach (char digit in theNumber)
{
adder += int.Parse(digit.ToString());
}

Console.WriteLine("Sum of digitis in {0} is: {1}", theNumber, adder);

if (adder < 10)
break;
else
theNumber = adder.ToString();

} while (true);

Console.WriteLine("Finished");
Console.ReadLine();
}



 
#713768    Author: Jivani      Member Level: Gold      Member Rank: 215     Date: 25/Apr/2013   Rating: 2 out of 52 out of 5     Points: 3

static void Main(string[] args)
{
Console.WriteLine("Please enter a number");
string theNumber = Console.ReadLine();
int num= Convert.ToInt32(thenumber);
int rem=0;
int sum=0;
while( num != 0 )
{
rem = num % 10;
sum = sum + rem;
num = num / 10;
}
string sum1=sum.tostring();
console.writeline(sum1);
}

I Hope To Use Full..
And Choice The Best Answers

php-wordpress Developer
you can any problem to php wordpress to contact me
jivanibhai7306@gmail.com
Thanks!!...





 
#713771    Author: Shakil      Member Level: Gold      Member Rank: 481     Date: 25/Apr/2013   Rating: 2 out of 52 out of 5     Points: 2

here is the code for C Language

void main()
{
int n,sum=10,n1;
clrscr();
printf("Enter a number :-");
scanf("%d",&n);
while(sum>=10)
{
sum=0;
while(n!=0)
{
n1=n%10;
sum=sum+n1;
n=n/10;
}
n=sum;
}
printf("Sum of digits :- %d ",sum);
}


thanks
regards

Regards,
Shakil Sama


 
#713774    Author: divya      Member Level: Bronze      Member Rank: 4471     Date: 25/Apr/2013   Rating: 2 out of 52 out of 5     Points: 1

in asp.net only

 
#713779    Author: Shine S      Member Level: Gold      Member Rank: 29     Date: 25/Apr/2013   Rating: 2 out of 52 out of 5     Points: 2

you can use the first example in asp.net. just call the function "private int GetSumOfDigits(int inputNum)" wherever required.

Regards,
Shine


 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Return to Discussion Forum
Start new thread

Subscribe to Subscribers
Active Members
Today
    Last 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India