
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to find the sum of digits of a number using Recursion
Let’s say we have set the number for which we will find the sum of digits −
int val = 789; Console.WriteLine("Number:",val);
The following will find the sum of digits by entering the number and checking it recursively −
public int addFunc(int val) { if (val != 0) { return (val % 10 + addFunc(val / 10)); } else { return 0; } }
Example
The following is our code to find the sum of digits of a number using Recursion in C#.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class MyApplication { static void Main(string[] args) { int val, result; Calc cal = new Calc(); val = 789; Console.WriteLine("Number:",val); result = cal.addFunc(val); Console.WriteLine("Sum of Digits in {0} = {1}", val, result); Console.ReadLine(); } } class Calc { public int addFunc(int val) { if (val != 0) { return (val % 10 + addFunc(val / 10)); } else { return 0; } } } }
Output
Number: 789 Sum of Digits in 789 = 24
- Related Articles
- Java Program to Find Sum of Digits of a Number using Recursion
- Haskell Program to Find Sum of Digits of a Number using Recursion
- Python Program to Find the Sum of Digits in a Number without Recursion
- How to find the sum of digits of a number using recursion in C#?
- C++ Program to Find Factorial of a Number using Recursion
- Java Program to Find Factorial of a Number Using Recursion
- Haskell Program to Find Factorial of a Number Using Recursion
- Java Program to Find the Sum of Natural Numbers using Recursion
- Golang Program to Find the Sum of Natural Numbers using Recursion
- Golang Program to Find the Sum of N Numbers using Recursion
- Haskell Program to Find the Sum of Natural Numbers using Recursion
- C++ program to Find Sum of Natural Numbers using Recursion
- Java Program to Find Sum of N Numbers Using Recursion
- Haskell Program to Find Sum of N Numbers Using Recursion
- C program to find sum of digits of a five digit number

Advertisements