Write a C++ program that computes the following series:
sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime
Your program should prompt the user to enter a number n. The program will compute and display sum based on the series defined above.
firstPrime: is 2
secondPrime: the first prime number after 2
thirdPrime: the third prime number
….
nth prime: the nth prime number
Your program must be organized as follows:
int main() {
//prompt the user to enter n
//read n from the user
//compute series
//print result
return 0;
}
bool isPrime(int num) {
// if num is prime return true
//else return false
}
In the question you have written sum as sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime
I think it should be
sum = 1/firstPrime + 2/secondPrime+…..+n/nthPrime where each term represents (n/nth prime)
or sum = 1/firstPrime + 1/secondPrime+…..+1/nthPrime where each term represents (1/nth prime)
i have written code for both, you just have to comment the line depending on which you want.
-------------------------------------------------------------------------------------------
I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.
--------------------main.cpp-------------------
#include <iostream>
#include <string.h>
using namespace std;
bool isPrime(int num);
int main()
{
int n;
cout << "\n Enter the value of n: ";
cin >> n;
cout << "\n sum is ";
double sum = 0;
int prime_count = 0; //to count number of primes
found so far
int num = 1; //number to be checked for prime
while(prime_count < n)
{
if(isPrime(num)) //check if number
is prime, if not prime move to next number
{
prime_count++;
//if prime num then increment prime_count
sum = sum +
prime_count/(double)num; //Series: 1/p1 + 2/p2 + 3/p3 + ... +
n/pn
if(prime_count
< n)
cout << prime_count << "/" <<
num << " + ";
else
cout << prime_count << "/" <<
num << " = ";
/*sum = sum +
1/(double)num; //if Series: 1/p1 + 1/p2 + 1/p3 + ... + 1/pn
if(prime_count
< n)
cout << 1 << "/" << num
<< " + ";
else
cout << 1 << "/" << num
<< " = "; */
}
num++; //move to next number
}
cout << sum << endl;
return 0;
}
bool isPrime(int num){ //checks if an input number is
prime
if(num < 2) //if num is less than 2 it is not
prime
return false;
for(int i = 2; i <= num/2; i++){ //check if
number is divided by any smaller number
if(num % i == 0) //if any number
divides then num is not prime
return
false;
}
return true; //if no number divides, then number is
prime
}
--------------------Screenshots main.cpp-------------------

--------------------Output-------------------

----------------------------------------------------
I hope this helps you,
Please rate this answer if it helped you,
Thanks for the opportunity
Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program...
CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...
Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod { public static void main(String[] args) { String input; // To hold keyboard input String message; // Message...
C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished) { printf("Enter n > 0 or quit\n"); scanf("%10s", buffer); if (strcmp(buffer, "quit") == 0) { finished = true; } else { // Convert input to number and compute n-th prime num = atoi(buffer); if (num > 0) { res = nth_prime(num); printf("Prime #%d is %d\n", num, res); }...
C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () { int numDucks,numCats; getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...
Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...
1. (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n) For example, sumDigits (234) returns 9 (2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10(= 4). To remove 4 from 234, use 234 / 10(= 23)....
Change your C++ average temperatures program to: In a non range-based loop Prompt the user for 5 temperatures. Store them in an array called temperatures. Use a Range-Based loop to find the average. Print the average to the console using two decimals of precision. Do not use any magic numbers. #include<iostream> using namespace std; void averageTemperature() // function definition starts here { float avg; // variable declaration int num,sum=0,count=0; /* 'count' is the int accumulator for number of...
Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...
Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices. 1. Find the number of digits in an integer. 2. Find the nth digit in an integer. 3. Find the sum of all digits of an integer. 4. Is the integer a palindrome? 5. Quit Enter a choice: Page 1 of 4 For each of the choices (1, 3, 4), Read a positive integer number and call a function that processes the menu choice...
c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...