In: Computer Science
Write a code on C++ to find prime number series.
Please upvote if you are able to understand this and if there is any query do mention it in the comment section.
CODE:
#include <iostream>
using namespace std;
//creating a function for prime numbers and passing a number to
stop the series at that numner
int prime(int num) {
int flag;
for(int i = 1; i <= num; i++) {
flag = 0;//initialising flag with 0
for(int j = 1; j <= num; j++) {
if(i % j == 0) {
flag = flag + 1;//incrementing flag with 1 everytime the remainder
of i and j is 0
}
}
if(flag == 2) {
cout << "The prime number series is:" << endl;
cout << i << " ";//printing all the series
}
}
return 0;
}
int main()
{
int num, res = 0;
cout << "Enter number" << endl;//taking the input of
number from user
cin >> num;
cout << prime(num);//calling the function prime
return 0;
}
OUTPUT:
Please mention in the comment section if it was required to be done in any other way otherwise please upvote.