C Programming – HackerRank Solution | Divisor summation 1 |

(HackerRank) Write a Modular C Programming code to solve Divisor summation 1, Given a natural number n output the summation of all its proper divisors.

Given a natural number n output the summation of all its proper divisors.

Definition: A proper divisor of a natural number is the divisor that is strictly less than the number. e.g. number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.

Input Format

Natural number n

Constraints

n should be positive

Output Format

Divisor summation of n

Sample Input 0

20

Sample Output 0

Divisor summation is 22

Sample Input 1

-5

Sample Output 1

Invalid natural number

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

void divisor(int n)
{
    int sum=0,i;
    for(i=1;i<n;i++)
    {
        if(n%i==0)
        {
            sum=sum+i;
        }
    }
    printf("Divisor summation is %d\n",sum);
}

int main() {

    int n;
    scanf("%d",&n);
    if(n<1)
    {
        printf("Invalid natural number\n");
        exit(0);
    }
    else
    {
        divisor(n);
    }
    return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

20
Your Output (stdout)

Divisor summation is 22
Expected Output

Divisor summation is 22

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!