Write a c program to find all factors of a number. In this C programming example, we will see, how to find all factors of a number using for loop.
Sample input
Enter a positive number: 10
Sample output
Factors of 10 are: 1 2 5 10
What is a factor?
A number that divides by another number with zero remainders is known as a factor. for example, 6 is a factor of 6 because = 6 ÷3=2 exactly, the other factor of 6 is 1, 2, 3, and 6.
Algorithm to find factors of a number
- Declare two variables.
- Then take one input and one output.
- Take a for loop and if statement.
- Give the condition in both and increment of a variable.
- Take another print in if statement.
C program to find all factors of a number
#include<stdio.h> void main (){ int i; int n; printf("enter positive number :"); scanf("%d",&n); printf("factor of %d are: ",n); for(i=1;i<=n;i++){ if(n%i==0){ printf("%d ",i); } } }
Explanation of this C program
Step 1: Start.
Step 2: Create a header file and include the library (studio. h).
Step 3: Create the main function.
Step 4: Then declares two variables that are i and n.
Step 5: Create an input with the help of scanf for taken number from user to find factors.
Step 6: Take a printf for print factors of a number.
Step 7: Create a for loop and make an if statement in the loop, give the condition to both, initialize a variable i in loop and increment of i and take a printf for output in the if statement.
Step 8: End.
Output
In this way, we learned how to write a C program to find all factors of a number.