Program to Find All Factors of Number using While loop

Program to Find All Factors of Number using While loop

  • Write a program to Find All Factors of Number using While loop in C
  • Write a program to Find All Factors of Number using While loop in C++
  • Write a program to Find All Factors of Number using While loop in Python
  • Write a program to Find All Factors of Number using While loop in PHP
  • Write a program to Find All Factors of Number using While loop in Java
  • Write a program to Find All Factors of Number using While loop in Java Script
  • Write a program to Find All Factors of Number using While loop in C#

Explanation:

To find all the factors of a number using a While loop, the logic is:

Logic:

  1. Definition of a Factor:
    • A factor of a number n is any number i such that n%i = 0
  2. Iterate Through Possible Factors:
    • Use a While loop to iterate from 1 to n (inclusive).
    • For each number i, check if it divides n without a remainder.
  3. Store or Print the Factors:
    • If i is a factor, print or store it in a list.

Program to Find All Factors of 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;
}

#include <iostream>
using namespace std;

int main() {
    int num, i = 1;
    cout << "Enter a number: ";
    cin >> num;

    cout << "Factors of " << num << " are: ";
    while (i <= num) {
        if (num % i == 0) {
            cout << i << " ";
        }
        i++;
    }

    return 0;
}

num = int(input("Enter a number: "))
i = 1

print(f"Factors of {num} are: ", end="")
while i <= num:
    if num % i == 0:
        print(i, end=" ")
    i += 1

<?php
$num = intval(readline("Enter a number: "));
$i = 1;

echo "Factors of $num are: ";
while ($i <= $num) {
    if ($num % $i == 0) {
        echo "$i ";
    }
    $i++;
}
?>

import java.util.Scanner;

public class Factors {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        System.out.print("Factors of " + num + " are: ");
        int i = 1;
        while (i <= num) {
            if (num % i == 0) {
                System.out.print(i + " ");
            }
            i++;
        }
    }
}

let num = parseInt(prompt("Enter a number: "));
let i = 1;

console.log(`Factors of ${num} are:`);
while (i <= num) {
    if (num % i === 0) {
        console.log(i);
    }
    i++;
}

using System;

class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int num = int.Parse(Console.ReadLine());

        Console.Write("Factors of " + num + " are: ");
        int i = 1;
        while (i <= num) {
            if (num % i == 0) {
                Console.Write(i + " ");
            }
            i++;
        }
    }
}

List of All Programs