4.Write a c# program to find sum of digits in a given number.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sumdigits
{
class Program
{
static void Main(string[] args)
{
int n, sum;
Console.WriteLine("Please enter the number");
n=int.Parse(Console.ReadLine());
sum = 0;
while (n != 0)
{
sum += n % 10;
n /= 10;
}
Console.WriteLine("the sum of digits is " + sum);
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sumdigits
{
class Program
{
static void Main(string[] args)
{
int n, sum;
Console.WriteLine("Please enter the number");
n=int.Parse(Console.ReadLine());
sum = 0;
while (n != 0)
{
sum += n % 10;
n /= 10;
}
Console.WriteLine("the sum of digits is " + sum);
}
}
}
all is well with this site but it would be more better if there is an explanation for the programs
ReplyDeletethanks,
nikkan
a great collection...thanks a lot
ReplyDeleteit would be really helpful if there is an output for every program...
ReplyDeleteWhat is system.Linq, explain any one
ReplyDelete