Program to Print All Prime Numbers Between 1 and n using While loop

Program to Print All Prime Numbers Between 1 and n using While loop

  • Write a program to Print All Prime Numbers Between 1 and n using While loop in C
  • Write a program to Print All Prime Numbers Between 1 and n using While loop in C++
  • Write a program to Print All Prime Numbers Between 1 and n using While loop in Python
  • Write a program to Print All Prime Numbers Between 1 and n using While loop in PHP
  • Write a program to Print All Prime Numbers Between 1 and n using While loop in Java
  • Write a program to Print All Prime Numbers Between 1 and n using While loop in Java Script
  • Write a program to Print All Prime Numbers Between 1 and n using While loop in C#

Explanation:

Steps

  1. Input: Take the upper limit n.
  2. Prime number check:
    • A prime number is greater than 1 and divisible only by 1 and itself.
    • For each number i from 2 to n, check if it is prime:
      • Loop through numbers from 2 to sqrt(i) to see if any number divides i.
      • If no number divides i, it is prime.
  3. Print primes: Display all numbers identified as prime.

Logic

  • Use a loop to iterate from 2 to n.
  • For each number, check divisibility up to its square root.
  • Print the number if it is prime.

Explanation

  • For n = 20:
    • Check numbers 2 through 20.
    • Primes are: 2, 3, 5, 7, 11, 13, 17.

Efficiency Tip

  • By checking divisibility only up to sqrt(num), the program reduces unnecessary checks for larger divisors, making it more efficient.

Program to Print All Prime Numbers Between 1 and n using While loop

#include <stdio.h>

int main() {
    int n, num = 2;

    printf("Enter the value of n: ");
    scanf("%d", &n);

    printf("Prime numbers between 1 and %d are: ", n);

    // Print all prime numbers between 1 and n using a while loop
    while (num <= n) {
        int isPrime = 1;
        for (int i = 2; i <= num / 2; i++) {
            if (num % i == 0) {
                isPrime = 0;
                break;
            }
        }
        if (isPrime && num > 1) {
            printf("%d ", num);
        }
        num++;
    }

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int n, num = 2;

    cout << "Enter the value of n: ";
    cin >> n;

    cout << "Prime numbers between 1 and " << n << " are: ";

    // Print all prime numbers between 1 and n using a while loop
    while (num <= n) {
        bool isPrime = true;
        for (int i = 2; i <= num / 2; i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime && num > 1) {
            cout << num << " ";
        }
        num++;
    }

    return 0;
}

n = int(input("Enter the value of n: "))
num = 2

print(f"Prime numbers between 1 and {n} are: ", end="")

# Print all prime numbers between 1 and n using a while loop
while num <= n:
    is_prime = True
    for i in range(2, num // 2 + 1):
        if num % i == 0:
            is_prime = False
            break
    if is_prime and num > 1:
        print(num, end=" ")
    num += 1

<?php
$n = (int)readline("Enter the value of n: ");
$num = 2;

echo "Prime numbers between 1 and $n are: ";

# Print all prime numbers between 1 and n using a while loop
while ($num <= $n) {
    $isPrime = true;
    for ($i = 2; $i <= $num / 2; $i++) {
        if ($num % $i == 0) {
            $isPrime = false;
            break;
        }
    }
    if ($isPrime && $num > 1) {
        echo "$num ";
    }
    $num++;
}
?>

import java.util.Scanner;

public class PrimeNumbers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the value of n: ");
        int n = sc.nextInt();
        int num = 2;

        System.out.print("Prime numbers between 1 and " + n + " are: ");

        // Print all prime numbers between 1 and n using a while loop
        while (num <= n) {
            boolean isPrime = true;
            for (int i = 2; i <= num / 2; i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime && num > 1) {
                System.out.print(num + " ");
            }
            num++;
        }

        sc.close();
    }
}

let n = parseInt(prompt("Enter the value of n: "));
let num = 2;

console.log(`Prime numbers between 1 and ${n} are:`);
while (num <= n) {
    let isPrime = true;
    for (let i = 2; i <= num / 2; i++) {
        if (num % i === 0) {
            isPrime = false;
            break;
        }
    }
    if (isPrime && num > 1) {
        console.log(num);
    }
    num++;
}

using System;

class Program {
    static void Main() {
        Console.Write("Enter the value of n: ");
        int n = int.Parse(Console.ReadLine());
        int num = 2;

        Console.Write("Prime numbers between 1 and " + n + " are: ");

        // Print all prime numbers between 1 and n using a while loop
        while (num <= n) {
            bool isPrime = true;
            for (int i = 2; i <= num / 2; i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime && num > 1) {
                Console.Write(num + " ");
            }
            num++;
        }
    }
}

List of All Programs