62: Write a program in C++ to find prime number within a range

 

// Write a program in C++ to find prime number within a range. Go to the editor
//     Input number for starting range: 1
//     Input number for ending range: 100
//     The prime numbers between 1 and 100 are:
//     2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
//     The total number of prime numbers between 1 to 100 is: 25
//     **************************************************************************************************

#include <iostream>
using namespace std;
int main()
{
    int start, end;
    cout << "\nInput number for starting range: ";
    cin >> start;
    cout << "Input number for ending range: ";
    cin >> end;
    cout << "\n**********************************************************\n\n";

    cout << "The Prime numbers between " << start << " and " << end << " are: ";
    int a,b=0;
    for (int i = start; i <= end; i++)
    {
        a = 0;
        for (int j = 1; j <= i; j++)
        {
            if (i % j == 0)
            {
                a++;
            }
        }
        if (a == 2)
        {
            cout << i << ' ';
            b++;
        }
    }
    cout << "\nThe total number of Prime numbers between " << start << " and " << end << " are: " << b;
    cout << "\n\n**********************************************************\n\n";

    return 0;
}


Comments

Popular posts from this blog

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_