Question #16595

a c++ program that calculates and displays the sum of all prime numbers in a user-defined closed interval [a,b] of positive integers using both pre-defined and user-defined functions.

Expert's answer

#include "iostream"

using namespace std;

int isprime(int valuex){

int x,j;

x=1;

& for (j=2 ; j<valuex; j++)

{ if ((valuex%j)==0) x=0; }

& if(x!=0) return 1;

& else return 0;

}

int a,b;

void main(){

cout<<"primes from "<<a<<" to "<<b<<":\n";

a=10; b = 101;

for (int i=a; i<=b; i++)

& if (isprime(i)) cout<<i<<" ";

cout<<"\n";

cout<<"enter a:";

cin>>a;

cout<<"enter b:";

cin>>b;

cout<<"primes from "<<a<<" to "<<b<<":\n";

for (i=a; i<=b; i++)

& if (isprime(i)) cout<<i<<" ";

cout<<"\n";

system("pause");

}

using namespace std;

int isprime(int valuex){

int x,j;

x=1;

& for (j=2 ; j<valuex; j++)

{ if ((valuex%j)==0) x=0; }

& if(x!=0) return 1;

& else return 0;

}

int a,b;

void main(){

cout<<"primes from "<<a<<" to "<<b<<":\n";

a=10; b = 101;

for (int i=a; i<=b; i++)

& if (isprime(i)) cout<<i<<" ";

cout<<"\n";

cout<<"enter a:";

cin>>a;

cout<<"enter b:";

cin>>b;

cout<<"primes from "<<a<<" to "<<b<<":\n";

for (i=a; i<=b; i++)

& if (isprime(i)) cout<<i<<" ";

cout<<"\n";

system("pause");

}

## Comments

## Leave a comment