chore(CPlusPlus): add program to find prime number (#500)
Co-authored-by: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com> Co-authored-by: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com>pull/504/head^2
parent
ce53af33ca
commit
213aa584fa
|
@ -0,0 +1,34 @@
|
||||||
|
// It is a prime number program in c language.
|
||||||
|
// it is used to check whether a given number is prime or not
|
||||||
|
|
||||||
|
#include <iostream> //header
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
int main() { //main function
|
||||||
|
int num, i, a = 0; //define variable
|
||||||
|
cin >> num; //taking input from user
|
||||||
|
|
||||||
|
for (i = 2; i <= num / 2; i++) { //for loop started
|
||||||
|
if (num % i == 0) { //if condition
|
||||||
|
a++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (num < 2) {
|
||||||
|
a++;
|
||||||
|
}
|
||||||
|
if (a == 0) {
|
||||||
|
cout << "Prime"; // printing if the number is prime
|
||||||
|
} else {
|
||||||
|
cout << "Non-Prime"; // printing if the number is non-prime
|
||||||
|
}
|
||||||
|
return 0; // returning from main function
|
||||||
|
}
|
||||||
|
|
||||||
|
// sample input || sample output
|
||||||
|
// 1 || Nom-Prime
|
||||||
|
// 2 || Prime
|
||||||
|
// 3 || Prime
|
||||||
|
// 4 || Non-Prime
|
||||||
|
// 5 || Prime
|
||||||
|
|
||||||
|
// time complexity of this program is O(n).
|
|
@ -130,6 +130,8 @@
|
||||||
8. [Reverse digit of a number](Maths/reverse-digits.cpp)
|
8. [Reverse digit of a number](Maths/reverse-digits.cpp)
|
||||||
9. [Missing number](Maths/missing-number.cpp)
|
9. [Missing number](Maths/missing-number.cpp)
|
||||||
10. [Factorial of a number](Maths/factorial.cpp)
|
10. [Factorial of a number](Maths/factorial.cpp)
|
||||||
|
11. [Prime-number](Maths/prime-number.cpp)
|
||||||
|
|
||||||
|
|
||||||
# Recursion
|
# Recursion
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue