- 1C Hello World Program
- 2C Program to Print Your Own Name
- 3C Program to Print an Integer Entered by the User
- 4C Program to Add Two Numbers
- 5C Program to Multiply Two Floating‑Point Numbers
- 6C Program to Print the ASCII Value of a Character
- 7C Program to Swap Two Numbers
- 8C Program to Calculate Fahrenheit to Celsius
- 9C Program to Find the Size of int, float, double and char
- 10C Program - Add Two Complex Numbers
- 11C Program - Find Simple Interest
- 12C Program - Find Compound Interest
- 13C Program - Area And Perimeter Of Rectangle
- 14C Program - Check Whether a Number is Positive, Negative, or Zero
- 15C Program - Check Whether Number is Even or Odd
- 16C Program - Check Whether a Character is Vowel or Consonant
- 17C Program - Find Largest Number Among Three Numbers
- 18C Program - Calculate Sum of Natural Numbers
- 19C Program - Print Alphabets From A to Z Using Loop
- 20C Program - Make a Simple Calculator
- 21C Program - Generate Multiplication Table
- 22C Program - Reverse a Number
- 23C Program - Check whether the input number is a Neon Number
- 24C Program - Find All Factors of a Natural Number
C Program – Find All Factors of a Natural Number - Divisor Listing
Find All Factors of a Natural Number
The program finds and prints all positive factors of a given natural number. A factor divides the number without leaving a remainder. For example, factors of 12 are 1, 2, 3, 4, 6 and 12. We test each integer from 1 up to the number and print it if it divides evenly.
#include <stdio.h>
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factors of %d are: ", n);
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
Enter a positive integer: 12
Factors of 12 are: 1 2 3 4 6 12
Each integer from 1 to n is tested using the modulus operator. When n % i equals zero, i divides n exactly and is printed as a factor. This simple approach lists all divisors of the number.
⬅ Previous TopicC Program - Check whether the input number is a Neon Number
Next Topic ⮕C Program to Check Whether a Number is Prime or Not
Next Topic ⮕C Program to Check Whether a Number is Prime or Not
Comments
Loading comments...