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

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.
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
A number is a palindromic prime if it is a prime number as well as a...
A number is a palindromic prime if it is a prime number as well as a palindromic number (ie. it is the same number when the digits are reversed). For example, 10301 is a palindromic prime. Write a Python program to ask the user how many palindromic primes they would like to compute, and output the values with a maximum of 10 values per line. Your program should include the following functions: isPrime(number) - returns True or False isPalindrome(number) -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT