Wrox Programmer Forums
|
BOOK: Beginning Programming
This is the forum to discuss the Wrox book Beginning Programming by Adrian Kingsley-Hughes, Kathie Kingsley-Hughes; ISBN: 9780764584060
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 7th, 2006, 11:15 PM
Registered User
 
Join Date: May 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Prime Number Program

While reading this book, I came upon an idea to create a simple C++ program that would list all the prime numbers between two inputted numbers, and I figured this is possible with the codes I learned in this book only. I'm having problems though. Here's the chunk of my code that checks for prime numbers. numStart and numEnd are obviously the imputted numbers. My logic is that I start a loop of all the numbers between the start and end, and inside that loop I take every number below my number from the first loop, and divide them. I then check if that answer is an integer, and if it is, then the program does nothing. I'm unsure what to do to tell the program to display counter if check is never equal to 1. Can you help? This is what I have so far:
Code:
int counter;
int counter2;
float ansFloat;
int ansInt;
float check;
check = 1;
for (counter = numStart; counter <= numEnd; counter++)
{
for (counter2 = 2; counter2 < counter; counter2++)
{
ansFloat = counter / counter2;
ansInt = counter / counter2;
check = ansInt / ansFloat;
while (check == 1)
{
// Don't do anything.
}
}
}
 
Old June 30th, 2006, 09:53 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

If I've understood your question correctly, and assuming you're using java

if (check != 1) {
  System.out.println(counter);
}

If it's c or c++ you'd import STDIO at the tope of your file and do:

if ($check != 1) {
  printf("%d",$counter);
}

Incidentally you may want to check out the modulo operator (%) which gives the remainder of a division so:

3%2; // would be 1
4%2; // would be 0

This would save you creating all those temporary variables and running three instructions per number you print, which could slow things down on a large set of numbers ;)

You may also want to look at a mathematical method for finding the prime numbers in a set, called the "Net of Erostothenes"

Cheers,
Charlie

--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation For Phone Number and Mobile Number dhruthi.ram99 Javascript How-To 12 October 30th, 2011 07:24 AM
Setup Project: Program not added in Start>Program arif_1947 VS.NET 2002/2003 2 March 31st, 2005 06:40 AM
prime numbers code error.. amahja56 C++ Programming 1 January 19th, 2005 01:33 PM
Start a program inside another program Silje Classic ASP Professional 1 November 16th, 2004 02:08 AM
Prime Number Counting. Help!! ajm235 C++ Programming 3 August 27th, 2004 12:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.