Object:-Write a Program to display factors of a number
#include <stdio.h>
int main()
{
int number, i;
printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: ", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",i);
}
}
return 0;
}
Output
Enter a positive integer: 25
Factors of 25 are: 1 5 25
Explanation
- In the program, a positive integer entered by the user is stored in variable number.
- The for loop is iterated until i <= number is false. In each iteration, whether number is exactly divisible by i is checked (condition for i to be the factor of number) and the value of i is incremented by 1.