View All C Programs

 Previews Example  View All C Programs Next Example 

Program to Display Factors of a Number in C

This program takes a positive integer from an user and displays all the factors of that number.
#include <stdio.h>
int main()
{
  int n,i;
  printf("Enter a positive integer: ");
  scanf("%d",&n);
  printf("Factors of %d are: ", n);
  for(i=1;i<=n;++i)
  {
      if(n%i==0)
         printf("%d ",i);
  }
  return 0;
}
Result
Enter a positive integer: 60
Factors of 60 are: 1 2 3 4 5 6 12 15 20 30 60