C#.Net Program 4 :Get a Number and Display the Sum of the Digits
Program Explanation:C#.net Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpExamples
{
class SumOfDigits
{
//12345 = 1+2+3+4+5= 15
static void Main(string[] args)
{
int num, sum = 0, r; //variables
Console.WriteLine("Enter a Number : ");
//reading the number
num = int.Parse(Console.ReadLine());
//condition up to not equal to zero
while (num != 0)
{
r = num % 10;
num = num / 10;
sum = sum + r;
}
Console.WriteLine("Sum of Digits of the Number : " + sum);
Console.ReadLine();
}
}
}
If you have any queries or suggestions, please feel free to ask in comments section.
Post a Comment
Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.