The repository contains a collection of different programming snippets to different programming languages from own developments as well as other projects
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
programming-examples/c-sharp/Functions/C# Program to Find Sum of D...

36 lines
728 B

/*
* 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