programming-examples/c-sharp/Functions/C# Program to Find Sum of Digits of a Number using Recursion.cs
2019-11-15 12:59:38 +01:00

36 lines
728 B
C#

/*
* C# Program to Find Sum of Digits of a Number using Recursion
*/
using System;
class program
{
public static void Main()
{
int num, result;
pro pg = new pro();
Console.WriteLine("Enter the Number : ");
num=int.Parse(Console.ReadLine());
result =pg.sum(num);
Console.WriteLine("Sum of Digits in {0} is {1}", num, result);
Console.ReadLine();
}
}
class pro
{
public int sum(int num)
{
if (num != 0)
{
return (num % 10 + sum(num / 10));
}
else
{
return 0;
}
}
}
/*
Enter the Number :
234
Sum of Digits in 234 is 9