If you have any query feel free to chat us!
Happy Coding! Happy Learning!
To check whether a given number is prime or not, you can write a C++ program that takes an integer input from the user and checks for its divisibility by any numbers other than 1 and itself. A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself.
Here's a C++ program to check if a given number is prime or not:
cppCopy code
#include <iostream> bool isPrime(int number) { if (number <= 1) { return false; } for (int i = 2; i * i <= number; i++) { if (number % i == 0) { return false; } } return true; } int main() { int number; std::cout << "Enter a positive integer: "; std::cin >> number; if (isPrime(number)) { std::cout << number << " is a prime number." << std::endl; } else { std::cout << number << " is not a prime number." << std::endl; } return 0; }
Example output:
lessCopy code
Enter a positive integer: 7 7 is a prime number.
lessCopy code
Enter a positive integer: 10 10 is not a prime number.
In the program, we define a separate function
isPrime
that takes an integer as an argument and returns a boolean value. This function checks whether the number is prime or not by iterating from 2 up to the square root of the number. If the number is divisible by any value in this range, it returnsfalse
; otherwise, it returnstrue
. We handle the special cases of numbers less than or equal to 1, as they are not considered prime.Feel free to run this program with different positive integers to check if they are prime or not.
I bought this course, it worth it!
Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it
Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website
Quick answers to common questions about our courses, quizzes, and learning platform
SCIAKU Team please upload 1st video of TREE please please please, please