C# Program to Find Factors of a Number
To find factors of a number in C#, use a
for loop from 1 to the number and check if the number modulo the loop variable is zero with if (number % i == 0) to print the factor.Examples
Input6
OutputFactors of 6 are: 1 2 3 6
Input13
OutputFactors of 13 are: 1 13
Input1
OutputFactors of 1 are: 1
How to Think About It
To find factors of a number, think about all numbers from 1 up to that number. For each, check if dividing the number by it leaves no remainder. If yes, that number is a factor because it divides evenly.
Algorithm
1
Get the input number from the user.2
Start a loop from 1 to the input number.3
For each number in the loop, check if the input number modulo this number is zero.4
If yes, print this number as a factor.5
End the loop after reaching the input number.Code
csharp
using System; class Program { static void Main() { Console.Write("Enter a number: "); int number = int.Parse(Console.ReadLine()); Console.Write($"Factors of {number} are: "); for (int i = 1; i <= number; i++) { if (number % i == 0) { Console.Write(i + " "); } } } }
Output
Enter a number: 6
Factors of 6 are: 1 2 3 6
Dry Run
Let's trace the input 6 through the code to find its factors.
1
Input number
User enters 6, so number = 6
2
Start loop
Loop i from 1 to 6
3
Check i=1
6 % 1 == 0, so print 1
4
Check i=2
6 % 2 == 0, so print 2
5
Check i=3
6 % 3 == 0, so print 3
6
Check i=4
6 % 4 != 0, skip
7
Check i=5
6 % 5 != 0, skip
8
Check i=6
6 % 6 == 0, so print 6
| i | number % i | Is factor? |
|---|---|---|
| 1 | 0 | Yes |
| 2 | 0 | Yes |
| 3 | 0 | Yes |
| 4 | 2 | No |
| 5 | 1 | No |
| 6 | 0 | Yes |
Why This Works
Step 1: Loop through numbers
We check every number from 1 to the input number to see if it divides evenly.
Step 2: Use modulo operator
The % operator gives the remainder. If remainder is zero, it means the number divides evenly.
Step 3: Print factors
When a number divides evenly, we print it as a factor.
Alternative Approaches
Check factors up to square root
csharp
using System; class Program { static void Main() { Console.Write("Enter a number: "); int number = int.Parse(Console.ReadLine()); Console.Write($"Factors of {number} are: "); for (int i = 1; i * i <= number; i++) { if (number % i == 0) { Console.Write(i + " "); if (i != number / i) { Console.Write((number / i) + " "); } } } } }
This method is faster for large numbers because it only checks up to the square root, but factors may print in no particular order.
Using List to store factors
csharp
using System; using System.Collections.Generic; class Program { static void Main() { Console.Write("Enter a number: "); int number = int.Parse(Console.ReadLine()); List<int> factors = new List<int>(); for (int i = 1; i <= number; i++) { if (number % i == 0) { factors.Add(i); } } Console.WriteLine($"Factors of {number} are: {string.Join(" ", factors)}"); } }
This approach collects factors first, which can be useful if you want to use them later, but uses extra memory.
Complexity: O(n) time, O(1) space
Time Complexity
The program checks every number from 1 to n, so it runs in linear time O(n).
Space Complexity
It uses constant extra space O(1) since it prints factors directly without storing them.
Which Approach is Fastest?
Checking up to the square root of n is faster (O(√n)) but more complex to implement and may print factors unordered.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Check all numbers 1 to n | O(n) | O(1) | Simplicity and small numbers |
| Check up to square root | O(√n) | O(1) | Large numbers and performance |
| Store factors in list | O(n) | O(n) | When factors need reuse or sorting |
Use the modulo operator
% to check if a number divides evenly to find factors.Beginners often forget to include the number itself as a factor or start the loop from 0 causing division errors.