Display Factors of a Number in C
C program to Display factors of a Number
Factors of a number are the ones which divide the original number without leaving any remainder.In this section, we will learn how to write a C program to display factors of a number in a simple and easy manner.Below you will find the steps to calculate factors of a number.
Algorithm to find factors of a number
Step 1 : Take the number as n.
Step 2 : Start a for loop from i=0 to i<=n.
Step 3 : if (n % i == 0)
then, i is a factor of n
else
then, i is not a factor of n.
For example:
The factors of 30 are : 1 , 2 , 3 , 5 , 6 , 10 , 15 , 30
The factors of 42 are : 1 , 2 , 3 , 6 , 7 , 14 , 21 , 42
These are two example codes given below to display factors of a number in C:
Example 1:
#include<stdio.h>
int main() {
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i) {
if (num % i == 0) {
printf("%d ", i);
}
}
return 0;
}
Output :
Enter a positive integer: 60 Factors of 60 are: 1 2 3 4 5 6 10 12 15 20 30 60
Example 2:
#include<stdio.h>
void printFactors (int n)
{
for (int i = 1; i <= n; i++)
if (n % i == 0)
printf ("%d ", i);
}
int main ()
{
printf ("The factors of 100 are: \n");
printFactors (100);
return 0;
}
Output :
The factors of 100 are: 1 2 4 5 10 20 25 50 100
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment