Write a program to Find Prime Factors of Number using For loop in C
Write a program to Find Prime Factors of Number using For loop in C++
Write a program to Find Prime Factors of Number using For loop in Python
Write a program to Find Prime Factors of Number using For loop in PHP
Write a program to Find Prime Factors of Number using For loop in Java
Write a program to Find Prime Factors of Number using For loop in Java Script
Write a program to Find Prime Factors of Number using For loop in C#
Explanation:
Logic:
Definition of Prime Factors:
Prime factors of a number are prime numbers that divide the number without leaving a remainder.
Divide by Smallest Primes:
Start with the smallest prime number (2) and check divisibility.
If divisible, divide the number and continue checking for the same factor until it no longer divides.
Iterate Through Possible Divisors:
Use a for loop to check divisibility for all numbers from 2 to the square root of the number.
If a divisor is found, reduce the number and repeat until it becomes 1.
Program to Find Prime Factors of Number using For loop
C
C++
Python
PHP
JAVA
Java Script
C#
#include <stdio.h>
void findPrimeFactors(int num) {
printf("Prime factors of %d are: ", num);
for (int i = 2; i <= num; i++) {
while (num % i == 0) {
printf("%d ", i);
num /= i;
}
}
printf("\n");
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 1) {
findPrimeFactors(num);
} else {
printf("No prime factors for numbers less than 2.\n");
}
return 0;
}
#include <iostream>
using namespace std;
void findPrimeFactors(int num) {
cout << "Prime factors of " << num << " are: ";
for (int i = 2; i <= num; i++) {
while (num % i == 0) {
cout << i << " ";
num /= i;
}
}
cout << endl;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 1) {
findPrimeFactors(num);
} else {
cout << "No prime factors for numbers less than 2." << endl;
}
return 0;
}
num = int(input("Enter a number: "))
if num > 1:
print(f"Prime factors of {num} are: ", end="")
for i in range(2, num + 1):
while num % i == 0:
print(i, end=" ")
num //= i
print()
else:
print("No prime factors for numbers less than 2.")
<?php
function findPrimeFactors($num) {
echo "Prime factors of $num are: ";
for ($i = 2; $i <= $num; $i++) {
while ($num % $i == 0) {
echo "$i ";
$num /= $i;
}
}
echo "\n";
}
$num = (int)readline("Enter a number: ");
if ($num > 1) {
findPrimeFactors($num);
} else {
echo "No prime factors for numbers less than 2.\n";
}
?>
import java.util.Scanner;
public class PrimeFactors {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num > 1) {
System.out.print("Prime factors of " + num + " are: ");
for (int i = 2; i <= num; i++) {
while (num % i == 0) {
System.out.print(i + " ");
num /= i;
}
}
System.out.println();
} else {
System.out.println("No prime factors for numbers less than 2.");
}
sc.close();
}
}
let num = parseInt(prompt("Enter a number: "));
if (num > 1) {
console.log(`Prime factors of ${num} are:`);
for (let i = 2; i <= num; i++) {
while (num % i === 0) {
console.log(i);
num /= i;
}
}
} else {
console.log("No prime factors for numbers less than 2.");
}
using System;
class Program {
static void Main() {
Console.Write("Enter a number: ");
int num = int.Parse(Console.ReadLine());
if (num > 1) {
Console.Write($"Prime factors of {num} are: ");
for (int i = 2; i <= num; i++) {
while (num % i == 0) {
Console.Write(i + " ");
num /= i;
}
}
Console.WriteLine();
} else {
Console.WriteLine("No prime factors for numbers less than 2.");
}
}
}