1public class Prime {
2 public static void main(String[] args) {
3
4 int num = 29;
5 boolean flag = false;
6 for(int i = 2; i <= num/2; ++i){
7 if(num % i == 0){
8 flag = true;
9 break;
10 }
11 }
12
13 if (!flag)
14 System.out.println(num + " is a prime number.");
15 else
16 System.out.println(num + " is not a prime number.");
17}}
1Numbers -- Prime Number
2Write a method that can check if a number is prime or not
3
4Solution:
5public static boolean primeNumber(int num) {
6
7 if(num <= 1){
8 return false;
9 }
10
11 for(int i = 2; i < num; i++) {
12 if(num % i == 0) {
13 return false;
14 }
15 }
16
17 return true;
18
19}
1def is_prime(n):
2 if(n<0):
3 print("n is not a nature number!")
4 return False
5 for i in range(1,ceil(n/2)+1):
6 if (i != n and i != 1 and n%i == 0):
7 return False
8 return True
9print(is_prime(int(input("number>>"))))
1a number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11)
2suppose a number 'n' it can be divided by if '1' and only by itself then it can be a prime number
1// PRIME NUMBER
2// 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.
3
4let number = 5;
5if(number < 2){
6 console.log("less then 2 not prime")
7}
8let flag = false;
9for(let i=2;i<number;i++){
10 if(number % i === 0){
11 flag = true;
12 }
13}
14if(flag){
15 console.log("Not prime ")
16}
17else{
18 console.log("Prime")
19}
20
21
1#include<bits/stdc++.h>
2using namespace std;
3bool Is_Prime(long long x){
4 if(x%2==0)return false;
5 for(int i=3;i*i<=x;i+=2)
6 if(x%i==0)return false;
7 return true;
8}
9int main(){
10 long long x;
11 cin>>x;
12 if(Is_Prime(x))cout<<"Is Prime";
13 else cout<<"Is not Prime";
14}