Sum of prime numbers between 2 numbers

Sum of prime numbers between 2 numbers in c++

Page 1 of 1

4 Replies - 9430 Views - Last Post: 20 November 2008 - 09:45 AM Rate Topic: -----

#1 Kasra_olips  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-November 08

Sum of prime numbers between 2 numbers

Post icon  Posted 20 November 2008 - 08:02 AM

Hi dears.
I have a simple question about c++.

how I can sum of prime numbers between 50 & 100 and print it?
what is codes?

tanx a lot.
Is This A Good Question/Topic? 0
  • +

Replies To: Sum of prime numbers between 2 numbers

#2 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Sum of prime numbers between 2 numbers

Posted 20 November 2008 - 08:44 AM

[rules]
[/rules]
Was This Post Helpful? 0
  • +
  • -

#3 Kasra_olips  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-November 08

Re: Sum of prime numbers between 2 numbers

Posted 20 November 2008 - 09:01 AM

View Postmuballitmitte, on 20 Nov, 2008 - 07:44 AM, said:

[rules]
[/rules]

see this thread:
http://dreamincode.c...wtopic25403.htm
its about vb, and you did it. But now u cant? :cry2: its unfair.

Its not my homework, one of my freinds ask it.
I know this code to find prime numbers between 1 & 1000:
 for i:=2 to 1000 do begin
  sw:=0;
  for j:=2 to (i div 2)+1 do begin
	if (i mod j)=0 then begin sw:=1; break; end
  end;
  if sw=0 then writeln(i);
 end;


Now I want sum of prime numbers between 50 & 100 and print it.
:cry2:
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Sum of prime numbers between 2 numbers

Posted 20 November 2008 - 09:11 AM

Post the code that you have completed in your attempt to fulfill this projects requirements.

Regardless of whether this is for a friend or you, you must show at least a minimum amount of effort at solving the problem.
Was This Post Helpful? 0
  • +
  • -

#5 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 285
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Sum of prime numbers between 2 numbers

Posted 20 November 2008 - 09:45 AM

#include <iostream>


using namespace std;

void printPrime(int x, int y);

int main()
{
    printf("============================\nPrimes Program\nCoder: bbq@dreamincode.com\n============================\n\n");
    printPrime(150,50);    //print primes up to for 50 - 150

    getchar();
    return 0;
}

//function
void printPrime(int x, int y)
{
    int maximum = x;
    int initial = y;
    int j ,counter;

    while(initial <= maximum)
    {
        counter = 0;    //reset to zero each loop
        for(j = 1; j <= initial; j++)
        {
            if( initial%j == 0 )
            counter++;
        }

        if(counter == 2)
        printf("%d	", initial);

        initial++;
    }
    printf("\n");

}


This post has been edited by bbq: 20 November 2008 - 09:45 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1