Join Regular Classroom : Visit ClassroomTech

Accenture Coding Question | Sum Of Divisor | CodeWindow

Sum of divisors

Print the sum of the divisors of the integer number N.

Input specification:
Input 1: The integer ‘n’.

Output Specification:
Return the sum of divisors of ‘n’.

Example 1:
Input 1: 6

Output: 12
Explanation:
Divisors of 6 are 1,2,3,6. Sum of number (1+2+3+6)=12

Example 2:
Input 1: 36

Output: 91
Explanation:
Divisors of 36 are 1,2,3,4,6,9,12,18,36. Sum of number (1+2+3+4+6+9+12+18+36)=12

Solution in Python 3:

#https://codewindow.in
#join our telegram channel

n=int(input())
count=0
for i in range(1,n+1):
    if n%i==0:
        count=count+i
    else:
        pass
print(count)

# Telegram @codewindow

Solution in C++ :

//https://codewindow.in
//join our telegram channel @codewindow

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int sum = 0;
    for(int i=1;i<=n;i++) 
        if(n % i == 0)
            sum += i;
    cout << sum;
}

Solution in C :

//https://codewindow.in
//join our telegram channel @codewindow

#include <stdio.h>

int main()
{
    int n;
    scanf("%d",&n);
    int sum = 0;
    for(int i=1;i<=n;i++) 
        if(n % i == 0)
            sum += i;
    printf("%d",sum);
}

Solution in JAVA :

//https://codewindow.in
//join our telegram channel @codewindow

import java.util.*;

class CodeWindow {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        for(int i=1;i<=n;i++) 
            if(n % i == 0)
                sum += i;
        System.out.println(sum);
    }
}

Output:

36
91
6
12
Recent Posts
Pages