Wednesday, 12 August 2026

Factors of a Number in C

 Option 1: Factors of a Number in C Using for Loop

#include <stdio.h>

 

int main() {

 

    // Declare an integer type variable

    int num;

 

    // Take input from user

    printf("Enter a number: ");

    scanf("%d", &num);

 

    // Display message

    printf("Factors of %d are: ", num);

 

    // Loop from 1 to num

    for (int i = 1; i <= num; i++) {

 

        // Check if i divides num

        // completely

        if (num % i == 0) {

 

            // Print factor

            printf("%d ", i);

        }

    }

 

    return 0; 

}

Run Code

 

Output:

Enter a number: 12

Factors of 12 are: 1 2 3 4 6 12

 

Explanation:

This C program to find factors of a number iterates from 1 to the given number (num) and checks divisibility using the modulo operator (%).

 

If num % i == 0, i is a factor of the number.

 

This approach is straightforward and works well for small to medium-sized numbers. It is a beginner-friendly method to display factors of a number in C.

 

 

Time Complexity Analysis

Time Complexity : O(n)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 2: C Program for Factors of a Number Using while Loop

#include <stdio.h>

 

int main() {

    int num, i = 1;

 

    printf("Enter a number: ");

    scanf("%d", &num);

 

    printf("Factors of %d are: ", num);

    while (i <= num) {

        if (num % i == 0) {

            printf("%d ", i);

        }

        i++;

    }

 

    return 0;

}

Run Code

 

Output:

Enter a number: 99

Factors of 99 are: 1 3 9 11 33 99

 

Explanation:

This C program to display factors of a number uses a while loop, starting from 1 and incrementing until the given number. It checks divisibility similarly to the for-loop method. Using a while loop provides flexibility for scenarios where the stopping condition might not be predefined, such as dynamic ranges.

 

You have finished this program, and if you want to practice more, you can explore our collection of C programs.

 

 

Time Complexity Analysis

Time Complexity : O(n)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 3: Factors of a Number in C Using Function

#include <stdio.h>

 

void displayFactors(int num) {

    printf("Factors of %d are: ", num);

    for (int i = 1; i <= num; i++) {

        if (num % i == 0) {

            printf("%d ", i);

        }

    }

    printf("\n");

}

 

int main() {

    int num;

 

    printf("Enter a number: ");

    scanf("%d", &num);

 

    displayFactors(num);

 

    return 0;

}

Run Code

 

Output:

Enter a number: 56

Factors of 56 are: 1 2 4 7 8 14 28 56

 

Explanation:

This C program to find factors of a number encapsulates the logic within a reusable function, displayFactors(). The function takes the number as a parameter and performs the same divisibility check using a for loop.

 

This modular approach simplifies the main program and is ideal for scenarios requiring repeated use of the factor-finding logic.

 

This program gives a basic understanding, but C has many more concepts. Refer to a full C tutorial for deeper learning.

 

 

Time Complexity Analysis

Time Complexity : O(n)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 4: Find Factors of Number Using Square Root Optimization

#include <stdio.h>

#include <math.h>

 

int main() {

 

    int num;

 

    printf("Enter a number: ");

    scanf("%d", &num);

 

    // Display message

    printf("Factors of %d are: ", num);

 

    // Loop only up to sqrt(num)

    for (int i = 1; i <= sqrt(num); i++) {

 

        // Check if i is a factor

        if (num % i == 0) {

 

            // Print first factor

            printf("%d ", i);

 

            // Print paired factor if different

            if (i != num / i) {

                printf("%d ", num / i);

            }

        }

    }

Run Code

 

Output:

Enter a number: 12

Factors of 12 are: 1 12 2 6 3 4

 

Explanation:

This optimized C program to display factors of a number reduces the number of iterations by checking divisibility only up to the square root of the number. For every factor i, its pair num / i is also a factor, which is printed simultaneously. This approach significantly improves performance for large numbers.

 

 

Time Complexity Analysis

Time Complexity : O(√n)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 5: Factors of a Number in C Using Recursion

#include <stdio.h>

 

void findFactors(int num, int i) {

    if (i > num) {

        return;

    }

    if (num % i == 0) {

        printf("%d ", i);

    }

    findFactors(num, i + 1);

}

 

int main() {

    int num;

 

    printf("Enter a number: ");

    scanf("%d", &num);

 

    printf("Factors of %d are: ", num);

    findFactors(num, 1);

 

    return 0;

}

Run Code

 

Output:

Enter a number: 1000

Factors of 1000 are: 1 2 4 5 8 10 20 25 40 50 100 125 200 250 500 1000

 

Explanation:

This C program to find factors of a number uses recursion, where the function findFactors() checks divisibility and calls itself with the next number (i + 1). The base case stops the recursion when i > num. Recursion provides an elegant solution but may not be efficient for very large numbers.

 

 

Time Complexity Analysis

Time Complexity : O(n)

 

 

Space Complexity Analysis

Space Complexity : O(n)

 

Option 6: Factors of a Number in C Using Prime Factorization

#include <stdio.h>

 

int main() {

 

    int num, factors[100], count = 0;

 

    // Take input

    printf("Enter a number: ");

    scanf("%d", &num);

 

    // Store factors in array

    for (int i = 1; i <= num; i++) {

 

        if (num % i == 0) {

 

            // Store factor in array

            factors[count++] = i;

        }

    }

 

    // Print stored factors

    printf("Factors of %d are: ", num);

 

    for (int i = 0; i < count; i++) {

        printf("%d ", factors[i]);

    }

 

    return 0;

}

Run Code

 

Output:

Enter a number: 12

Factors of 12 are: 1 2 3 4 6 12

 

Explanation:

The program takes a number as input and uses a loop from 1 to that number to check which values divide it completely (factors).

 

Each valid factor is stored in an array factors[], and the variable count keeps track of how many factors are found.

 

Finally, the program prints all stored factors by looping through the array from index 0 to count - 1.

 

 

Time Complexity Analysis

Time Complexity : O(n)

 

 

Space Complexity Analysis

Space Complexity : O(n)

 

No comments:

Post a Comment