C Program to Find Factors of a Number using For loop | Function | Recursion | Pointers
C program to find factors of a number using for loop, function, recursion, pointers, and do-while loop.
In this article, you will learn how to find factors of a number in c using for loop, function, recursion, pointers, and do-while loop.
Example-1
Input: 60
The factors of the 60 are: 1 2 3 4 5 6 10 12 15 20 30 60
Example-2
The factors of the 70 are: 1 2 5 7 10 14 35 70
What are the factors of a number?
The factors of a number are defined as numbers that divided the original number without leaving any remainder (left reminder = 0).
You should have knowledge of the following topics in c programming to understand these programs:
- C main()function
- C printf()function
- C forloop statement
- C whileloop statement
- C Functions
- C Pointers
In this article, we solve this problem in six methods:
- Using the For loop
- Using the Function
- Using the Recursion
- Using the Pointers
- Using the While loop
- Using the Do-while loop
C Program to Find Factors of a Number using For loop
// C Program to Find Factors of a Number using For loop
#include <stdio.h>
int main() {
    int x, i;
    printf("-----Enter the positive integer number-----\n");
    scanf("%d", &x);
    printf("\nThe factors of the %d are: ", x);
    for (i = 1; i <= x; ++i) {
        if (x % i == 0) {
            printf("%d ", i);
        }
    }
    printf("\n");
    return 0;
}
Output
-----Enter the positive integer number-----
90
The factors of the 90 are: 1 2 3 5 6 9 10 15 18 30 45 90
Explanation
In these given programs, we have taken input 90 a random number then applied the while loop and makes a calculation on this random number.
With It self reminder zero to find the possible factors of this random number.
The same calculation applied to the second program with for loop.
C Program to Find Factors of a Number using Function
// C Program to Find Factors of a Number using Function
#include <stdio.h>
// This function will display the factors
void Factors(int x) {
    printf("\nThe factors of the %d are: ", x);
    for (int i = 1; i <= x; ++i) {
        if (x % i == 0) {
            printf("%d ", i);
        }
    }
    printf("\n");
}
// It's the driver function
int main() {
    int x;
    printf("-----Enter the positive integer number-----\n");
    scanf("%d", &x);
    Factors(x);
    return 0;
}
C Program to Find Factors of a Number using Recursion
// C Program to Find Factors of a Number using Recursion
#include <stdio.h>
// It's the recursive function
// It will display the factors
void Factors(int x, int i) {
    
    if (i <= x) {
        if (x % i == 0) {
            printf("%d ", i);
        }
        // Calling function itself to print the next number
        Factors(x, i + 1);
    }
}
// It's the driver function
int main() {
    int x;
    printf("-----Enter the positive integer number-----\n");
    scanf("%d", &x);
    printf("\nThe factors of the %d are: ", x);
    Factors(x, 1);
    printf("\n");
    return 0;
}
Output
-----Enter the positive integer number-----
120
The factors of the 120 are: 1 2 3 4 5 6 8 10 12 15 20 24 30 40 60 120
You should also read these things
Find Factors of a Number in Java
Python Program to Find Factors of a Number
C Program to Find Factors of a Number using Pointers
// C Program to Find Factors of a Number using Pointers
#include <stdio.h>
int main() {
    int x, i, *ptr;
    printf("-----Enter the positive integer number-----\n");
    scanf("%d", &x);
    // To swap the address of `x` variable
    ptr = &x;
    printf("\nThe factors of the %d are: ", x);
    for (i = 1; i <= *ptr; ++i) {
        if ((*ptr) % i == 0) {
            printf("%d ", i);
        }
    }
    printf("\n");
    return 0;
}
C Program to Find Factors of a Number using While loop
// C Program to Find Factors of a Number using While loop
#include <stdio.h>
int main() {
    int x, i = 1;
    printf("-----Enter the positive integer number-----\n");
    scanf("%d", &x);
    printf("\nThe factors of the %d are: ", x);
    while (i <= x) {
    	if (x % i == 0) {
            printf("%d ", i);
        }
        ++i;
    }
    printf("\n");
    return 0;
}
Output
-----Enter the positive integer number-----
70
The factors of the 70 are: 1 2 5 7 10 14 35 70
C Program to Find Factors of a Number using Do While loop
// C Program to Find Factors of a Number using Do While loop
#include <stdio.h>
int main() {
    int x, i = 1;
    printf("-----Enter the positive integer number-----\n");
    scanf("%d", &x);
    printf("\nThe factors of the %d are: ", x);
    do {
        if (x % i == 0) {
            printf("%d ", i);
        }
        ++i;
    } while (i <= x);
    
    printf("\n");
    return 0;
}
Also, visit these links
C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade
C++ Program to Calculate Percentage and Grade
Java Program to Calculate Grade of students
Python Program to Calculate Total Marks Percentage and Grade of a Student