C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print all factors of a number

Introduction

A factor of a number is an integer that divides the number completely without leaving a remainder.

Examples:

  • Factors of 12: 1, 2, 3, 4, 6, 12
  • Factors of 15: 1, 3, 5, 15

This program prints all factors of a given positive integer using a for, while and do..while loop.

 

C Program: Print all factors of a number

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int num, i;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num <= 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

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

 

    // Loop through numbers from 1 to num

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

        if (num % i == 0) {

            printf("%d ", i);

        }

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 12
Factors of 12 are: 1 2 3 4 6 12

OUTPUT 2 :
Enter a positive integer: 15
Factors of 15 are: 1 3 5 15


Explanation

  1. User inputs a positive integer num.
  2. The for loop iterates from 1 to num.
  3. Each number i is checked: if num % i == 0, then i is a factor.
  4. The program prints all such numbers.

 

C Program: Print all factors of a number

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int num, i = 1;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num <= 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

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

 

    // Loop through numbers from 1 to num

    while (i <= num) {

        if (num % i == 0) {

            printf("%d ", i);

        }

        i++;

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 12
Factors of 12 are: 1 2 3 4 6 12

OUTPUT 2 :
Enter a positive integer: 15
Factors of 15 are: 1 3 5 15


Explanation

  1. User inputs a positive integer num.
  2. Initialize i = 1.
  3. The while loop iterates until i <= num.
  4. For each i, check if num % i == 0. If yes, i is a factor and is printed.
  5. Increment i after each iteration.

 

C Program: Print all factors of a number

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int num, i = 1;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num <= 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

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

 

    // Loop through numbers from 1 to num using do...while

    if (i <= num) {

        do {

            if (num % i == 0) {

                printf("%d ", i);

            }

            i++;

        } while (i <= num);

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer: 12
Factors of 12 are: 1 2 3 4 6 12

OUTPUT 2 :
Enter a positive integer: 15
Factors of 15 are: 1 3 5 15


Explanation

  1. User inputs a positive integer num.
  2. Initialize i = 1.
  3. The ..while loop iterates from 1 to num.
  4. For each i, check if num % i == 0. If yes, i is a factor and is printed.
  5. Increment i after each iteration.
  6. Ensures the loop executes at least once even if num is 1.