In this program, we are going to share a Simple C# program to find sum of all divisors of a natural number. If you are a beginner and want to start learning the C# programming, then keep your close attention in this tutorial as I am going to share a Simple C# program to find sum of all divisors of a natural number with the output.
Copy the below c# program and execute it in your Microsoft Visual Studio IDE (Integrated Development Environment ). At the end of this program, I have shared the output program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; class naturalNumberProgram { static int divSum(int n) { int result = 0; for (int i = 2; i <= Math.Sqrt(n); i++) { if (n % i == 0) { if (i == (n / i)) result += i; else result += (i + n / i); } } return (result + n + 1); } public static void Main() { int n = 30; Console.WriteLine(divSum(n)); } } |
72
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.