Program to Print All Divisor of Number using While loop
- Write a program to Print All Divisor of Number using While loop in C
- Write a program to Print All Divisor of Number using While loop in C++
- Write a program to Print All Divisor of Number using While loop in Python
- Write a program to Print All Divisor of Number using While loop in PHP
- Write a program to Print All Divisor of Number using While loop in Java
- Write a program to Print All Divisor of Number using While loop in Java Script
- Write a program to Print All Divisor of Number using While loop in C#
Explanation:
To print all divisors of a number using a for loop, follow these steps:
Steps:
- Take the input number (n).
- For loop: Iterate through all numbers from 1 to n (or up to n/2, as divisors greater than n/2 can’t divide n except n itself).
- Check divisibility: In each iteration, check if the number divides n without a remainder (n % i == 0).
- Print the divisor: If the condition is true, print the number.
Logic:
- Iterate from 1 to n (or n / 2).
- If i % n == 0, then i is a divisor of n.
Explanation:
- For input n = 12:
- The divisors of 12 are the numbers that divide 12 evenly, which are 1, 2, 3, 4, 6, and 12.
- The loop iterates from 1 to 12, checking each number to see if it divides 12 without a remainder.
This logic will print all divisors of any given number, including 1 and the number itself. If you want to optimize it, you can loop only up to n / 2, as no number greater than n / 2 can be a divisor of n except n itself.
Program to Print All Divisor of Number using While loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> int main() { int num, i = 1; printf("Enter a number: "); scanf("%d", &num); printf("Divisors of %d are: ", num); // Print all divisors of the number using a while loop 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 << "Divisors of " << num << " are: "; // Print all divisors of the number using a while loop while (i <= num) { if (num % i == 0) { cout << i << " "; } i++; } return 0; }
num = int(input("Enter a number: ")) i = 1 print(f"Divisors of {num} are: ", end="") # Print all divisors of the number using a while loop while i <= num: if num % i == 0: print(i, end=" ") i += 1
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><?php $num = (int)readline("Enter a number: "); $i = 1; echo "Divisors of $num are: "; # Print all divisors of the number using a while loop while ($i <= $num) { if ($num % $i == 0) { echo "$i "; } $i++; } ?>
import java.util.Scanner; public class Divisors { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int num = sc.nextInt(); int i = 1; System.out.print("Divisors of " + num + " are: "); // Print all divisors of the number using a while loop while (i <= num) { if (num % i == 0) { System.out.print(i + " "); } i++; } sc.close(); } }
let num = parseInt(prompt("Enter a number: ")); let i = 1; console.log(`Divisors 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()); int i = 1; Console.Write("Divisors of " + num + " are: "); // Print all divisors of the number using a while loop while (i <= num) { if (num % i == 0) { Console.Write(i + " "); } i++; } } }