--- In [email protected], "peternilsson42" <[EMAIL PROTECTED]>
wrote:
>
> "supersensitive_ad" <supersensitive_ad@> wrote:
> > Steve Searle <steve@> wrote:
> > > supersensitive_ad scrawled:
> > >
> > > > Hi to everyone! I m a new user of this informative
> > > > group.
>
> Did you read the welcome message you would have received
> when you joined?
>
> There is a copy in the C-Prog Files section. In part,
> it says...
>
> If you are enrolled in a class and want homework help,
> c-prog members prefer that you mention the specific
> difficulty you have with the assignment, along with
> the relevant C/C++ code you have so far. Posting
> assignments with the expectation that others will do
> them for you will not get your homework done.
>
> Before asking a question, run a few Google searches
> first. Members are much more willing to help people
> who show that they are putting forth effort.
>
> The principle applies even if you are not actually
> enrolled in a class.
>
> > > > I want a program whose description is below:
> > > > A positive integer is entered through the
> > > > keyboard. A function is written in the program to
> > > > obtain the prime factors of the number
> > > > "RECURSIVELY" ! For example, the prime factors of
> > > > 24 are 2,2,2,3 while those of 35 are 7 and 5 or
> > > > those of 121 are 11 and 11.
> > > >
> > > > Anybody who knows about this, please help me in
> > > > this as soon as possible! Thank You!
> > >
> > > Shouldn't you do your own homework?
> >
> > I dont know wht u r tryin to say
>
> He's saying you should do your own homework. I agree
> with him.
>
> > but i m asking for a program for finding Prime Factors
> > of a number "RECURSIVELY"! Please help me in
>
> We will gladly help you, but only if you demonstrate
> that you have made some attempt to solve your homework
> on your own. For instance, it is trivial to find simple
> algorithms and code that factorise small integers. You
> need only post your attempt to modify such code to be
> recursive, and we will assist.
>
> --
> Peter
>
Thanks Peter for your suggestions. Now i can understand how to do
this. Look, I have been trying for last 5 days to find some valid
solution of that prime factor question. I will show you one or two
of my attempts:
#include < iostream>
using namespace std;
int prime_factors(int num);
void main()
{
int num;
cout << "Enter a Positive Integer: ";
cin >> num;
if (num>0)
{
prime_factors(num);
}
}
int prime_factors(int num)
{
while (num % 2 == 0)
{
cout<<"\n "<<"2 is one of its Prime Factors"<<endl;
num = num/2;
}
while (num % 3 == 0)
{
cout<<"\n "<<"3 is one of its Prime Factors"<<endl;
num = num/3;
}
while (num % 5 == 0)
{
cout<<"\n "<<"5 is one of its Prime Factors"<<endl;
num = num/5;
}
while (num % 7 == 0)
{
cout<<"\n "<<"7 is one of its Prime Factors"<<endl;
num = num/7;
}
if (num > 1) // base case when new 'num' is greater than '1'.
{
cout<<"\n "<<num<<" is also one of its Prime Factors"; //
recursive call when base case occurs.
cout << endl;
}
return 0;
}
This program appears to be right but has some problems e.g. it is
not in an actual "Recursive format". Moreover at some numbers like
121, it doesn't display the right output!
One more attempt is this one:
#include <iostream>
using namespace std;
int prime_factors(int num);
int next_prime();
void main()
{
int num;
cout << "Enter a Positive Integer: ";
cin >> num;
if (num>0)
{
prime_factors(num);
next_prime();
}
}
int prime_factors(int num)
{
int prime = 2;
while(num % prime == 0)
{
cout<<"\n "<<"2 is one of its Prime Factors"<<endl;
num = num/prime;
}
prime++;
return 0;
}
int next_prime()
{
int prime=3;
while (num % prime == 0)
{
cout<<"\n "<<prime<<" is one of its Prime
Factors"<<endl;
num = num/prime;
prime++;
}
return 0;
}
This program is I think farther nearer to the gight way but dont
know how to modify it...!