Home » C program to find the sum of factors of a number

C program to find the sum of factors of a number

Learn about C program to find the sum of factors of a number in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

C program to find the sum of factors of a number

The below program is quite similar to finding factors of a number. Once we find the factors of the number we simply add them. To find the factors of a number refer : Program to find factor Using while loop

Sometimes we need to find the factors of a number as well as the sum of the factors. The Code below finds factors and the sum of a number’s factors.

Source code:

#include<stdio.h>
int main()
{
  int num, sum=0;

  printf("Enter number: ");
  scanf("%d",&num);

  printf("Factors of %d are:\n", num);
  for(int i=1; i<=num/2; i++)
  {
    if(num%i==0)
    {
      sum += i;
      printf("%d\t",i);
    }
  }

  printf("\nSum of %d factors is: %d\n", num, sum);

  return 0;
}

Output:

Enter number: 8
Factors of 8 are:
1 2 4
Sum of 8 factors is: 7

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Program to Find Factors of a Number in C Using the do-while loop
Program to find factor Using while loop