I wanted to write a program that finds the factor of a no. eg 2 and 5 are factors of 10. But that program is not working .Please tell me what is wrong with my program.Also tell me how to finally write like "10=2*5". Despite having learned functions and arrays i don;t seem to be able to put them to use. Please help me.
#include<iostream>
using namespace std;
int function (int a, int b);
int main ()
{
cout<<"This program finds the factor of a no."<<endl;
int a,b;
cout<<"Please input a no."<<endl;
cin>>a;
cout<< b <<"is a factor of " << a<<endl;
return 0;
}
int function ( int a,int b)
{
for(b=0;b>1&& b<32;b++)
{
if (a%b==0)
return b;
}
return 0;
}
Jack_1
0
Newbie Poster
Recommended Answers
Jump to PostFirst of all, use CODE TAGS when you are pasting code!
Secondly, you are never calling your function.
Finally, look at the loop that you have in your function.
You start b at 0, and you want the loop to run only if b is greater than 1 …
Jump to PostFirst of all, I think that there's a simpler way to achieve what you are trying to do here. The algorithm for finding factors of any given number goes something like this:
(In pseudo-code)maximumDivisor = squareroot of number; // The maximum possible divisor for(x = 1 …
Jump to Post@amrith92:
Now, for any given number, the maximum value of the factor it can have is always less than or equal to its square-root
That's wrong. For e.g. sqrt(16) = 4 but factors of 16 are 2,4,8 (and off course 1).
What you said is true for prime factors …
All 9 Replies
thelamb
163
Posting Pro in Training
amrith92
119
Junior Poster
Anilyargunta
0
Newbie Poster
maryam ahmad
0
Newbie Poster
NP-complete
42
Junior Poster
amrith92
119
Junior Poster
NP-complete
42
Junior Poster
amrith92
119
Junior Poster
NP-complete
42
Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.