Prime Number

Easy
0/40
5 upvotes
Asked in companies
CognizantShareChatTata Consultancy Services (TCS)

Problem statement

Write a Program to check whether the given number N is prime or not.

For a given number N check if it is prime or not. A prime number is a number that is only divisible by 1 and itself.
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The only line of input contains a single integer N.
Output Format :
The only line of the output prints either true or false.
Constraints :
0 <= N <= 10,000
Sample Input 1 :
5
Sample Output 1 :
true
Sample Input 2 :
10
Sample Output 2 :
false
Explanation of Input 2:
10 has 2 factors 2, 5.
Approaches (1)
Brute-force method

In the program, a for loop is iterated from i = 2 to i < n.

In each iteration, whether n is perfectly divisible by i is checked using:

if (n % i == 0)

If n is perfectly divisible by I, n is not a prime number. In this case, the flag is set to False.

After the loop, if n is a prime number, the flag will still be True. However, if n is a non-prime number, the flag will be False.

Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Prime Number
All tags
Sort by
Search icon

Interview problems

easy cpp

#include <bits/stdc++.h> 

using namespace std;

int sqrt(int N) {

    int low = 1, high = N/2;

    while (low <= high) {

        long long mid = low + (high - low) / 2; 

        if (mid * mid == N) return mid;

        else if (mid * mid < N) low = mid + 1;

         else high = mid - 1;

    }

    return low;

}

 

int main() {

    int n;

    cin>>n;

    if(n<=1) cout<< "false";

    else{

    int flag  = 0;

    int val = sqrt(n);

    for(int i=2;i<=val;i++){

          if (n % i == 0) {

            cout << "false";

            flag = 1;

            break;

          }

        }

        if (!flag)

          cout << "true";

    }

    return 0;

}

6 views
0 replies
0 upvotes

Interview problems

Easy Java solution

   public static boolean isPrime(int n) {

        if(n==1){

 

            return false;

 

        }

 

       

 

        for(int i=2;i<=Math.sqrt(n);i++){

 

            if(n%i==0 || n%(i+2)==0){

 

                return false;

 

            }

 

        }

 

        return true;

 

    }

java

7 views
0 replies
0 upvotes

Interview problems

Java code

import java.util.* ;

import java.io.*; 

class Solution {

     static boolean isPrime(int n){

 

        if(n==1){

 

            return false;

 

        }

 

        if(n==2||n==3){

 

            return true;

 

        }

 

        if(n%2==0||n%3==0){

 

            return false;

 

        }

 

        for(int i=5;i<=Math.sqrt(n);i=i+6){

 

            if(n%i==0 || n%(i+2)==0){

 

                return false;

 

            }

 

        }

 

        return true;

 

    }

    public static void main(String args[]) {

        

         Scanner sc = new Scanner(System.in);

 

        int n = sc.nextInt();

 

        System.out.println(isPrime(n));

        

    }

}

java

1 view
0 replies
0 upvotes

Interview problems

Optimal solution solve all test cases

#include <iostream>

using namespace std;

 

int main() {

    int n;

    cin >> n;

    

    if (n <= 1) {

        cout << "false";

        return 0;

    }

    

    bool isPrime = true;

    for (int i = 2; i * i <= n; i++) {

        if (n % i == 0) {

            isPrime = false;

            break;

        }

    }

    

    if (isPrime) {

        cout << "true";

    } else {

        cout << "false";

    }

 

    return 0;

}

 

55 views
0 replies
0 upvotes

Interview problems

Easy Java Solution

import java.util.Scanner;

 

class Solution {

    static boolean isPrime(int n){

        if(n==1){

            return false;

        }

        if(n==2||n==3){

            return true;

        }

        if(n%2==0||n%3==0){

            return false;

        }

        for(int i=5;i<=Math.sqrt(n);i=i+6){

            if(n%i==0 || n%(i+2)==0){

                return false;

            }

        }

        return true;

    }

    

    public static void main(String args[]) {

        

        // Write code here

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        System.out.println(isPrime(n));

    }

}

41 views
0 replies
0 upvotes
profile

Interview problems

Java Solution

import java.util.* ;

import java.io.*; 

class Solution {

    

    public static void main(String args[]) {

    

    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();

    

        

    int i=1;

    int temp=0;

    while(i<=n){

        if(n%i==0)

        temp++;

        i++;

    }

    if(temp==2){

        System.out.println("true");

    }

    else{

        System.out.println("false");

    }

}

}

52 views
0 replies
0 upvotes

Interview problems

C++ SOLUTION - BEST AND OPTIMAL

#include <bits/stdc++.h> 

#include <iostream>

using namespace std;

 

bool isprime(int n) {

    if (n  <2) {

        return false;

    }

    

    for (int i = 2; i <= sqrt(n); i++) {

        if (n % i == 0) {

            return false;

        }

    }

    

    return true;

}

 

int main() {

    long int n;

    cin >> n;

 

    if (isprime(n)) {

        cout << "true";

    } else {

        cout << "false";

    }

 

    return 0;

}

162 views
0 replies
0 upvotes

Interview problems

check prime or not [cpp]

#include <bits/stdc++.h>

using namespace std;

bool isPrime(int n){

    if(n<=1) return false;

    for(int i=2; i<=n/2; i++){

        if(n%i==0) return false;

    }

    return true;

}

int main() {

    int n;

    cin>>n;

    if(isPrime(n)) cout<<"true";

    else cout<<"false"; 

 

    return 0;

}

106 views
0 replies
0 upvotes

Interview problems

Easy cpp code solution

#include <bits/stdc++.h>

#include <iostream>

using namespace std;

 

int main() {

//Write your code here

int n;

cin>>n;

int flag=0;

vector<bool> prime(n+1, true);

prime[0]=prime[1]=false;

for(int i=2; i< n+1; i++){

if(prime[i]){

if(n==i){

cout<<"true";

flag=1;

break;

}

for(int j=2*i; j<n+1;j=j+i){

prime[j]=false;

}

}

}

if(flag==0){

cout<<"false";

}

}

173 views
0 replies
0 upvotes

Interview problems

Basic Approach by user defined function

#include <bits/stdc++.h>

using namespace std;

bool isPrime(int n)

{

    // Corner case

    if (n <= 1)

        return false;

    // Check from 2 to n-1

    for (int i = 2; i < n; i++)

        if (n % i == 0)

            return false;

    return true;

}

178 views
0 replies
0 upvotes
Full screen
Console