Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num, sum = 0;
Console.WriteLine("Enter a number :\n");
num = Convert.ToInt32(Console.ReadLine());
if (num > 0)
{
for (int temp = num; temp != 0; temp /= 10)
sum += temp % 10;
Console.WriteLine("Sum of the digits of " + num + " is : " + sum);
}
else
Console.WriteLine("Enter A valid Number :\n");
Console.ReadKey();
}
}
}
Output:
Enter a number :
151
Sum of the digits of 151 is : 7
Comments
Post a Comment