I need to print out the prime numbers of an integer entered in. I am new to C++ but wrote this program with my partner and while it compiles, that's about all it does. I'm pretty sure it gets lost in the for loop, or atleast that's where I get lost. 
#include <iostream>

using namespace std;


int main()


{

int valueToFactor;


cout << "Enter Number to Factor: "<< endl;

cin >> valueToFactor;




for(int n=2; n <= valueToFactor;){

if(valueToFactor%n == 0){


cout << "The Prime Factors are:" << n << endl;

valueToFactor = valueToFactor/n;

}

else


++n;

}


return(0);

}

I was wondering if anyone had any hints to get me moving again.
Thanks.
RyeGuy
12 1594 sicarie 4,677
Recognized Expert Moderator Specialist
I need to print out the prime numbers of an integer entered in. I am new to C++ but wrote this program with my partner and while it compiles, that's about all it does. I'm pretty sure it gets lost in the for loop, or atleast that's where I get lost. 
#include <iostream>

using namespace std;


int main()


{

int valueToFactor;


cout << "Enter Number to Factor: "<< endl;

cin >> valueToFactor;




for(int n=2; n <= valueToFactor;){

if(valueToFactor%n == 0){

cout << "The Prime Factors are:" << n << endl;

valueToFactor = valueToFactor/n;

}

else

++n;

}

return(0);

}

I was wondering if anyone had any hints to get me moving again.
Thanks.
RyeGuy
You only increment n in the 'else' statement, which means that if you enter the 'if' portion, n stays the same through the loop (as soon as it's even, it gets stuck). You could just increment n no matter what, and print out each factor individually.
well, i have two things to mention.
first off, it doesnt look like youre set up to count in primes. it looks like youre trying to use the iterator for your for loop to factor the input number when you need to use a different variable. (youre counting by 2,3,4,5,6, . . . and you need to be factoring by 2,3,5,7,11, . . . )
i think you also need some sort of data structure to hold the factors youve already found so far too. you need to keep track of how many times you can divide by two, and then three, and five . . . etc.
You only increment n in the 'else' statement, which means that if you enter the 'if' portion, n stays the same through the loop (as soon as it's even, it gets stuck). You could just increment n no matter what, and print out each factor individually.
I was thinking of this, you mean increment it in the for loop? I'm not sure how i would get each factor to print out then.
sicarie 4,677
Recognized Expert Moderator Specialist
I was thinking of this, you mean increment it in the for loop? I'm not sure how i would get each factor to print out then.
I would recommend a for loop  then you can specify an action for each separate iteration of that loop.
Also look at Roonie's post  I think you're missing something with your algorithm, you're only counting the divisors, not the primes (though I think that would be the first step, if you stored those, then you could find the ones that were primes with an extra iteration),
well, i have two things to mention.
first off, it doesnt look like youre set up to count in primes. it looks like youre trying to use the iterator for your for loop to factor the input number when you need to use a different variable. (youre counting by 2,3,4,5,6, . . . and you need to be factoring by 2,3,5,7,11, . . . )
i think you also need some sort of data structure to hold the factors youve already found so far too. you need to keep track of how many times you can divide by two, and then three, and five . . . etc.
Thanks For the quick responses!
Roonie would it be easier for me to have n start at 1 and then increments by 2? Also I am not sure how I would add something to hold the prime factors, sorry im not understanding very much, this is all new to me.
thanks again.
ryeguy
sicarie 4,677
Recognized Expert Moderator Specialist
Thanks For the quick responses!
Roonie would it be easier for me to have n start at 1 and then increments by 2? Also I am not sure how I would add something to hold the prime factors, sorry im not understanding very much, this is all new to me.
thanks again.
ryeguy
An integer array would be able to store the numbers  it depends on what else you want to do with them, but I'm pretty sure an array would be easiest.
yeah, i would say that an int[] where each successive member represented how many times each of the primes factored would do nicely to store that information.
and as for counting in primes, that is something that professional computer programmers try to solve in their work. is there any upper limit to your input? if so, it might be easiest to simply hardcode the primes up to that limit. if not, im sure i can scratch up a reference or two to some primenumber finders . . .
An integer array would be able to store the numbers  it depends on what else you want to do with them, but I'm pretty sure an array would be easiest.
I just recently learned about arrays but wouldn't i need to know how many prime integers there were to start the array?
yeah, i would say that an int[] where each successive member represented how many times each of the primes factored would do nicely to store that information.
and as for counting in primes, that is something that professional computer programmers try to solve in their work. is there any upper limit to your input? if so, it might be easiest to simply hardcode the primes up to that limit. if not, im sure i can scratch up a reference or two to some primenumber finders . . .
I'm not sure if there was meant to be an upper limit but there was none given.
Sign in to post your reply or Sign up for a free account.
Similar topics 
by: Dag 
last post by:
Is there a python module that includes functions for working with prime
numbers? I mainly need A function that returns the Nth prime number and
that returns how many prime numbers are less than N, but a prime number
tester would also be nice. I'm dealing with numbers in the 10^610^8 range
so it would have to fairly efficient
Dag

by: Greg Brunet 
last post by:
In doing some testing of different but simple algorithms for getting a
list of prime numbers, I ended up getting some results that seem a bit
contradictory. Given the following test program (testPrimes.py) with
two algorithms that both check for primes by testing only odd numbers
using factors up to the square root of the value, where Primes1 is based
on all of the existing primes so far, and Primes2 is based on all odd
numbers, I would...

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 (inclusivly) evenly
divides n.

by: Chris 
last post by:
Hello!
I have been doing some research to see if C++ has some sort of
function to work with prime numbers.
For example, I would like to input integers and have the program check
to see if the integers are prime numbers.
I have been unsuccessful in finding anything. I am sure I can probably
figure out a long way to do it, but I was curious to see if C++ had

by: Tuvas 
last post by:
I have made and recently posted a libary I made to do Modular
Arithmetic and Prime numbers on my website at
http://www.geocities.com/brp13/Python/index.html . I am currently in a
crypotology class, and am working on building a RSA public key
cryptology system for a class project. I am building the librarys just
to get the experience to do so. However, I would ask if any of you know
of any gaping security holes that can easily be seen from...
 
by: johnmsimon 
last post by:
i need to develop a code that finds a prime right number between 2 and
100000. and print one line of text that indicates if the int. is right
prime. i am in beginning programing so complex is complicated. we are
on a functions chapter. i am usung the square root to find the prime.
so far i have accomplished this but i cant fgure how to divide this by
ten so i can find the right prime. i am using c.
#include <stdio.h>

by: newstips6706 
last post by:
1, 2, 3, 5, 7... PRIME Numbers
________________________________
Definitions
What is a PRIME Number ?

by: fabien.lyon 
last post by:
Message d'origine
De : fabien.lyon
Envoyé : mercredi 30 mai 2007 20:16
À : 'pythonlist@python.org'
Objet : RE: embeded python progam into visual C++ application crash
2.5.1 is compiled with Visual Studio 2005  I hope you had no problems
with VC++ 6.0?

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 number of digits i can use?
For example, when i use the number 4362206209 (which I know for a fact is prime) it returns 2 as a factor, which it isn't.
Eventually, I would like to get to long (50+ digit) numbers. I know this calculation would take...

by: marktang 
last post by:
ONU (Optical Network Unit) is one of the key components for providing highspeed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...

by: Oralloy 
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bitfields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++12 std=c++20 Wnarrowing bit_field.cpp
Here is the code in...
 
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...

by: agi2029 
last post by:
Let's talk about the concept of autonomous AI software engineers and nocode agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...

by: isladogs 
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...

by: TSSRALBI 
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LANtoLAN VPNs.
The last exercise I practiced was to create a LANtoLAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

by: 6302768590 
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

by: muto222 
last post by:
How can i add a mobile payment intergratation into php mysql website.
 
by: bsmnconsultancy 
last post by:
In today's digital era, a welldesigned website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
 