473,406 Members | 2,281 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,406 software developers and data experts.

Prime number program help

2
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 solution....

#include<iostream>
#include <cmath>
using namespace std;

int main()
{
int n;
cout<<" Enter a number to find if its a prime number "<<endl;
cin>>n;

bool a =true;

for(int i=2;i<sqrt(n);i++)
{

if(n%i==0)
{
a=false;
break;
}
}


if(a==false)
cout<<n<<" is not a prime number"<<endl;
else
cout<<n<<" is a prime number"<<endl;

}

my question is how do i edit this program to take out bool, for and break? I ran the program and it works but my professor hasn't covered those completely.
Dec 3 '06 #1
3 10124
kotoro
32
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 solution....

#include<iostream>
#include <cmath>
using namespace std;

int main()
{
int n;
cout<<" Enter a number to find if its a prime number "<<endl;
cin>>n;

bool a =true;

for(int i=2;i<sqrt(n);i++)
{

if(n%i==0)
{
a=false;
break;
}
}


if(a==false)
cout<<n<<" is not a prime number"<<endl;
else
cout<<n<<" is a prime number"<<endl;

}

my question is how do i edit this program to take out bool, for and break? I ran the program and it works but my professor hasn't covered those completely.
I don't program in C but i'm pretty good at extrapolating code functions from their appearance and syntax, In any case, since this is an assignment I doubt they want you using any outside libraries or function calls (methods), etc to solve it, so you're going to be stuck with at least one loop.

I don't know if theres a C equivalent to the java "return;" command, but there probably is; I'd imagine its a standard in programming languages

You can eliminate the boolean flag by using the (cout<<n<<" is not a prime number"<<endl;) line where you were setting the boolean to false and immediately end the program at that point.

This way, if the program finishes the loop without finding a zero modulus (n%i==0) you KNOW the number is prime, so you don't have to check a flag.
This would eliminate the need for the second if/else statement.



P.S. I recommend attempting to write your own solutions. Don't try to "find" ready made answers, you dont learn as much
Dec 3 '06 #2
kotoro
32
The use of a for loop:
for(var name = value; condition; (iteration updater)){
(code)
}

iteration updater: whatever is here runs once at the end of each loop

functions almost exactly the same as

var name=value;
while(condition){
(code)
(iteration updater)
}

the only difference is that the variable created inside the for argument is considered "inside" the loop, though I don't know if that makes any difference in C++, its important in java because the garbage collector eats anything inside curly braces when you exit them
Dec 3 '06 #3
smayon
2
Thanks for the replies
Dec 4 '06 #4

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

Similar topics

11
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n...
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...
2
by: wudoug119 | last post by:
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 { ...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: Caffiend | last post by:
Well, I've been picking at learning python, got tired of reading, and figured I'd try to replicate my prime number generator I wrote (with much TSDN forum help) in C++. I've hit a stumbling block......
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...
3
by: rcarwise | last post by:
Iam having trouble getting started on this program and wanted to know if I could get help on writing a method and loops to get started. this is the program that I have to do: Write a...
6
by: sigkill9 | last post by:
I'm doing some reading in a python book and am doing one of the chapter exercises but I cant figure out how to get it to work and was hoping some of you python guru's could help out? Heres...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.