473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

I have a program that decides if a given number is prime or not but it isnt working

This is my code and it will take any number that I input and say it is a prime number. Please help me...



int Prime(int prime) //declares isPrime as a function using integers
{

int primeCount = 2; //declares primeCount as an int variable and sets it equal to 2
while(primeCount < prime) //checks if primeCount is less than prime
{
if((prime % primeCount) == 0) //checks if prime divided by primeCount has a remainder
{
cout<<"This number is not prime"<<endl; //prints "The number is not prime." on the screen
return 0; //ends program
}
else //if prime divided by primecount does have a remainder
primeCount++; //adds one to primeCount
} //once primeCount is equal to prime...
cout<<"This number is prime."<<endl; //prints "The number is prime." on the screen
return 0; //ends program

}
Nov 12 '06 #1
2 4215
horace1
1,510 Expert 1GB
when I run you code it appears to work OK, are you sure you are calling the function Prime() with the correct data? - put a statement inside Prime to print the value of the parameter to check it is correct, e.g.
cout << prime << endl;
Nov 12 '06 #2
manontheedge
175 100+
i don't quite follow what you're trying to do, but part of the problem is that you're not checking if it's divisible by 3. Here's is some code that finds if ANY number is a prime number. It should help you out...

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main() 
  6. {
  7.      int number(0); // sets the input number to 0
  8.  
  9.      while ( number != 99 )  // just picked a random condition to keep it looping
  10.      {
  11.           cin >> number;
  12.  
  13.           if ( number == 2 || number == 3 ) // 2 and 3 are always prime numbers
  14.           {
  15.                cout << "this number is prime" << endl;
  16.           }
  17.  
  18.           else if( number % 2 == 0 || number % 3 == 0 ) // if # is divisible by 2 or 3
  19.           {
  20.                cout << "This number is not prime" << endl;
  21.           }
  22.  
  23.           else             
  24.           {
  25.                cout << "This number is prime." << endl;
  26.           }
  27.  
  28.      }        
  29.  
  30.      return 0; 
  31. }
  32.  
  33.  
Nov 12 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

72
by: Wim Vanhoof | last post by:
Dear all, I would like to announce that the department of computer science of the University of Namur, Belgium, is seeking a post-doctoral researcher for a one-year fellowship in the area of ...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
7
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
3
by: smayon | last post by:
Here's the problem- Write a C++ program that prompts the user to input a positive integer then output the number and a message indicating whether the number is a prime number. I found this...
6
by: zacariaz | last post by:
Once again i have tryed my lyck with classes and it seem that im getting the hang of it, but before i get too self confident, i would like to get an expert oppinion. I have made a working...
2
by: jknox13 | last post by:
Hi, I have to find the divisors of all numbers less than a number inputed by the user and state if a number is prime if no divisors. I have this code so far, but I'm getting the wrong output and...
2
by: QHorizon | last post by:
Hello, I'm new to Python (I've learned everything up to iterators so far) and fairly new to Programming. This would be my first real program: #Coordinate Geometry (The whole program is not...
5
by: cchris | last post by:
Hi, I'm a beginning C student and I'm working on a program that is supposed to display all the prime numbers between 50 and 100. I'm having trouble though. Can anyone help me out. This is what I have...
6
by: UofFprogrammer | last post by:
For a fun challenge, I am trying to create a C++ program that will determine if a number is prime. I am getting to problems when I reach a large values (over 9 or so digits). How can I increase the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.