Question

In: Computer Science

Write a code on C++ to find prime number series.

Write a code on C++ to find prime number series.

Solutions

Expert Solution

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.


Related Solutions

This is a c++ code. Write a series of assignment statements (complete executable code) that find...
This is a c++ code. Write a series of assignment statements (complete executable code) that find the first three positions of the string “and” in a string variable sentence. The positions should be stored in int variables called first, second and third. You may declare additional variables if necessary. The contents of sentence should remain unchanged. The sentence can be completely random. Im having a large problem with this. It's a simple c++ code, but im unable to get it....
Write a C++ program to check whether a number is prime or not. A prime number...
Write a C++ program to check whether a number is prime or not. A prime number is a positive integer that has exactly two positive integer factors, 1 and itself. Eg: 2, 3, 5, 7, 11, 13, 17, ... Sample Output: Input a number to check prime or not: 13 The entered number is a prime number. Input a number to check prime or not: 28 The entered number is not a prime number. Enhance the program to list all...
write a code using c++. Find the first 10 prime numbers and store them in an...
write a code using c++. Find the first 10 prime numbers and store them in an array.
Write a program in C++ to check whether a number is prime or not
Write a program in C++ to check whether a number is prime or not
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater than one whose only factors are one and itself. For example, the first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. A twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes. For example, the five twin prime pairs are (3, 5),...
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
*Code in C* Write a function that checks if a number is a perfect cube. Write...
*Code in C* Write a function that checks if a number is a perfect cube. Write another function that calculates the integer cubic root. Under the main program: Prompt the user to input a number Tell the user if the number is a perfect cube or not Print the cubic root if the inputted number is a perfect cube.
Problem 2: (10 marks) Write a program in C or C++ that takes a number series...
Problem 2: Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print...
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT