Tuesday, 18 February 2014

Write a C function "int sum_factor(int m)" that accepts one positive integer m and returns the sum of all factors of m. e.g. if m=28 then the function returns 1+2+4+7+14+28=56.

#include<stdio.h>

int sum_factor(int m)
{
    int i=1,sum=0;
    while(i<=m)
    {
        if(m%i==0)
        {
            sum=sum+i;
        }
        i++;
    }
    return sum;
}

main()
{
    int n;
    printf("\nEnter the number=");
    scanf("%d",&n);   
    printf("Sum of the factors of %d=%d",n,sum_factor(n));
}

No comments:

Post a Comment