Program to Check if Given Number is Prime or Not using For loop

Program to Check if Given Number is Prime or Not using For loop

  • Write a program to Check if Given Number is Prime or Not using For loop in C
  • Write a program to Check if Given Number is Prime or Not using For loop in C++
  • Write a program to Check if Given Number is Prime or Not using For loop in Python
  • Write a program to Check if Given Number is Prime or Not using For loop in PHP
  • Write a program to Check if Given Number is Prime or Not using For loop in Java
  • Write a program to Check if Given Number is Prime or Not using For loop in Java Script
  • Write a program to Check if Given Number is Prime or Not using For loop in C#

Explanation:

To check if a given number is prime or not using a for loop, follow these steps:

Steps:

  • Take the input number n.
  • Prime number check: A prime number is a number greater than 1 that is divisible only by 1 and itself.
  • Loop from 2 to sqrt(n) (you can stop at sqrt(n) because a larger factor of n must be paired with a smaller factor that has already been checked).
  • If any number divides n evenly (i.e., n % i == 0), then n is not prime.
  • If no such number divides n, then n is prime.

Logic:

  • If the number is less than 2, it is not prime.
  • For numbers greater than 2, check if they are divisible by any number from 2 to sqrt(n).
  • If divisible, it is not prime, otherwise, it is prime.

Explanation:

  • For input n = 7:
  • Loop iterates through numbers 2 to sqrt(7) (approximately 2.65), checking if 7 is divisible by any of these numbers.
  • Since no number divides 7 evenly, 7 is a prime number.

Why Looping Until sqrt(n)?

  • Any divisor of n greater than sqrt(n) must have a corresponding smaller divisor (less than sqrt(n)). So, by checking divisibility up to sqrt(n), you reduce unnecessary checks, improving efficiency.

Program to Check if Given Number is Prime or Not using For loop

#include <stdio.h>

int main() {
    int num, i;
    int isPrime = 1;

    printf("Enter a number: ");
    scanf("%d", &num);

    // Edge case for numbers less than 2
    if (num <= 1) {
        isPrime = 0;
    }

    // Loop to check if the number is prime
    for (i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            isPrime = 0;
            break;
        }
    }

    if (isPrime) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is not a prime number.\n", num);
    }

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int num;
    bool isPrime = true;

    cout << "Enter a number: ";
    cin >> num;

    // Edge case for numbers less than 2
    if (num <= 1) {
        isPrime = false;
    }

    // Loop to check if the number is prime
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            isPrime = false;
            break;
        }
    }

    if (isPrime) {
        cout << num << " is a prime number." << endl;
    } else {
        cout << num << " is not a prime number." << endl;
    }

    return 0;
}

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

# Edge case for numbers less than 2
if num <= 1:
    print(f"{num} is not a prime number.")
else:
    is_prime = True
    # Loop to check if the number is prime
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            is_prime = False
            break

    if is_prime:
        print(f"{num} is a prime number.")
    else:
        print(f"{num} is not a prime number.")

<?php
$num = (int)readline("Enter a number: ");
$isPrime = true;

// Edge case for numbers less than 2
if ($num <= 1) {
    $isPrime = false;
}

// Loop to check if the number is prime
for ($i = 2; $i * $i <= $num; $i++) {
    if ($num % $i == 0) {
        $isPrime = false;
        break;
    }
}

if ($isPrime) {
    echo "$num is a prime number.\n";
} else {
    echo "$num is not a prime number.\n";
}
?>

import java.util.Scanner;

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

        // Edge case for numbers less than 2
        if (num <= 1) {
            isPrime = false;
        }

        // Loop to check if the number is prime
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }

        if (isPrime) {
            System.out.println(num + " is a prime number.");
        } else {
            System.out.println(num + " is not a prime number.");
        }

        scanner.close();
    }
}

let num = parseInt(prompt("Enter a number:"));
let isPrime = true;

// Edge case for numbers less than 2
if (num <= 1) {
    isPrime = false;
}

// Loop to check if the number is prime
for (let i = 2; i * i <= num; i++) {
    if (num % i === 0) {
        isPrime = false;
        break;
    }
}

if (isPrime) {
    console.log(`${num} is a prime number.`);
} else {
    console.log(`${num} is not a prime number.`);
}

using System;

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

        // Edge case for numbers less than 2
        if (num <= 1) {
            isPrime = false;
        }

        // Loop to check if the number is prime
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }

        if (isPrime) {
            Console.WriteLine($"{num} is a prime number.");
        } else {
            Console.WriteLine($"{num} is not a prime number.");
        }
    }
}

List of All Programs