Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# Sum of all the factors of a number

// Simple C# program to
// find sum of all divisors
// of a natural number
using System;
 
class GFG {
 
    // Function to calculate sum of all
    //divisors of a given number
    static int divSum(int n)
    {
        if(n == 1)
           return 1;
 
        // Final result of summation
        // of divisors
        int result = 0;
     
        // find all divisors which divides 'num'
        for (int i = 2; i <= Math.Sqrt(n); i++)
        {
            // if 'i' is divisor of 'n'
            if (n % i == 0)
            {
                // if both divisors are same
                // then add it once else add
                // both
                if (i == (n / i))
                    result += i;
                else
                    result += (i + n / i);
            }
        }
     
        // Add 1 and n to result as above loop
        // considers proper divisors greater
        // than 1.
        return (result + n + 1);
    }
     
    // Driver program to run the case
    public static void Main()
    {
         
        int n = 30;
         
        Console.WriteLine(divSum(n));
    }
}
 
// This code is contributed by vt_m.
Comment

PREVIOUS NEXT
Code Example
Csharp :: Block iFrames | How to Stop Your Website From Being iFramed 
Csharp :: disable alt + f4 in c# forms 
Csharp :: unity create a textbox in inspector 
Csharp :: .net mvc foreach index 
Csharp :: unity how to make gamemanager instance 
Csharp :: unity easing out 
Csharp :: drawing default serializedproperty unity 
Csharp :: C# How to display text in console 
Csharp :: get file upload file size in MB c# 
Csharp :: camera in raylib c# 
Csharp :: c# ip address to string 
Csharp :: instantiate an array in c# 
Csharp :: c# file to byte array 
Csharp :: linq contains 
Csharp :: faucongz 
Csharp :: list with search bar uwp c# 
Csharp :: dataset empty check C# 
Csharp :: Generic Stack in c# 
Csharp :: c# return two values 
Csharp :: elasticsearch nested aggregation in c# 
Csharp :: c# textbox only numbers 
Csharp :: ontriggerenter unity not working 
Csharp :: loop in c# 
Csharp :: c# code snippets 
Csharp :: Create a list of 3 Orders c# 
Csharp :: How to remove an element from Array List in C#? 
Csharp :: asp.net store list in web.config 
Csharp :: read only variable in c# 
Csharp :: constant interpolated string 
Csharp :: Camera follow player script unity 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =