Prime Number | Basics of Input/Output

Problem Statement :
You are given an integer N. You need to print the series of all prime numbers till N.

Problem Link : 
https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/prime-number-8/

Input Format :
The first and only line of the input contains a single integer N denoting the number till where you need to find the series of prime number.

Output Format :
Print the desired output in single line separated by spaces.

Constraints :
1<=N<=1000

Solution : 

// Code for the Problem
#include <iostream>

using namespace std;

int isPrime(int n)
{   
    for(int i=2;i<=n/2;i++)
    {
        if(n%i==0)
        {
            return 0;
        }
    
    }
   return 1;
}
int main()
{
    int num;
    cin>>num;
    for(int i=2;i<num;i++)
    {
        if(isPrime(i)==1)
         cout<<i<<" ";
    }
    return 0;
}


Code Snippet
Submission

Solution Explained :
Here we have defined a function isPrime() to check if the number passed to this function is Prime or not. If  Prime, this function returns True else returns False. We take input the number in Num. Now we run a loop from i=2 to i<Num incrementing i by +1 each time. For every iteration of the Loop, we pass value of i to isPrime() and if it is Prime...we simply print it.
This completes our code.
Do comment your response plz.

FOR ANY QUERY REGARDING SOLUTION
free feel to contact
Rahul Badgujar
WhatsApp : +917775919753
Email: rahulrb15899@gmail.com

Comments

Popular posts from this blog

Seating Arrangement | Basics of Input/Output

Palindromic String | Basics of Input/Output