Wednesday, April 5, 2023

Write a C++ Program to Display Prime Numbers Between Two Intervals

Displaying Prime Numbers Between Two Intervals Using C++ Write a C++ Program to Display Prime Numbers Between Two Intervals.

A prime number is a positive integer greater than 1 that has no positive integer divisors other than 1 and itself. In other words, a prime number can only be evenly divided by 1 and itself.

For example, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31 are the first 11 prime numbers. 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, and 21 are not prime numbers, because they have positive integer divisors other than 1 and themselves (e.g. 4 can be divided evenly by 2, 6 can be divided evenly by 2 and 3, etc.).

Prime numbers are important in mathematics and computer science because they have some interesting properties and applications. For example, they are used in cryptography (e.g. RSA encryption), random number generation, and number theory. There are also many unsolved problems related to prime numbers, such as the Riemann hypothesis, which is considered one of the most important open problems in mathematics.


#include <iostream>
using namespace std;

bool isPrime(int num) {
    if (num < 2) {
        return false;
    }
    for (int i = 2; i <= num / 2; ++i) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

int main() {
    int start, end;
    cout << "Enter the starting number: ";
    cin >> start;
    cout << "Enter the ending number: ";
    cin >> end;
    cout << "Prime numbers between " << start << " and " << end << " are: ";
    for (int i = start; i <= end; ++i) {
        if (isPrime(i)) {
            cout << i << " ";
        }
    }
    cout << endl;
    return 0;
}


//===========
OUTPUT:
Enter the first number of the interval: 10
Enter the second number of the interval: 50
Prime numbers between 10 and 50 are: 11 13 17 19 23 29 31 37 41 43 47

In this program, the isPrime function checks if a given number is prime or not by iterating through all numbers from 2 to half of the number, and checking if it's divisible by any of them. If it's not divisible by any number, then it's a prime number.

In the main function, the program asks the user to input two numbers for the starting and ending points of the interval. It then calls the isPrime function for each number in the interval and prints out the prime numbers found.

The program's objective is to find and display all prime numbers between two intervals. It does so by first defining a function isPrime which takes an integer as an argument and returns a boolean value based on whether the number is prime or not.

The function isPrime works by first checking if the number is less than 2, which is not a prime number. If it's less than 2, the function immediately returns false. Next, the function iterates from 2 to half of the number, and for each number in that range, it checks if the number is divisible by that number. If it's divisible by any number in that range, it's not a prime number, so the function returns false. If it's not divisible by any number, then it's a prime number, and the function returns true.

In the main function, the program first prompts the user to input two numbers, the starting and ending points of the interval. It then calls the isPrime function for each number in the interval and prints out the prime numbers found.

Here are some notes on the program:

  • * The using namespace std; statement at the beginning of the program allows the use of standard library functions and objects without having to specify the std:: prefix. This is a common practice in C++ programs.

  • * The bool return type for the isPrime function means that the function returns either true or false.

  • * The for loop in the isPrime function starts from 2 and goes up to half of the number, because any factor of the number beyond half of the number would be greater than the other factor, and therefore redundant.

  • * The if (isPrime(i)) statement in the main function calls the isPrime function for each number in the interval and prints out the prime numbers found.

  • * The endl at the end of the cout statement in the main function is used to insert a newline character and flush the output buffer.

  • * It's worth noting that this program has a time complexity of O(n^2), where n is the number of integers in the given interval. This is because the isPrime function has a time complexity of O(n/2), and it's called for each number in the interval. For larger intervals, this program may take a long time to run. There are more efficient algorithms for finding prime numbers, such as the Sieve of Eratosthenes, but they are beyond the scope of this program.

No comments:

Post a Comment

Write a c++ program to show the difference between an array and a list.

Array vs List Example Write a c++ program to show the difference between an array and a list. Arrays are useful when you know the number ...