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;
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;
}

6
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by NP-complete

First 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 and less than 32. How is b going to get creater than 1 if u start at 0?

First 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 till x <= maximumDivisor, x+=1) {
Factor = number / x; // Should typically be a floating-point value
checker = floor of firstFactor; // see <cmath>, and the function floor()
if(Factor == checker) // to see that Factor is a perfect factor of the given number
cout << number << " = " << Factor << " x " << x << '\n'; // Output it to the user
}``````

Now, for any given number, the maximum value of the factor it can have is always less than or equal to its square-root, so then we have to check all the numbers between 1 and the square root, and check if it divides the number perfectly. If it does, then output it in whatever form you want...

Hope this helped :)

Edited by amrith92: n/a

First You Call The Function After cin>>a
& Then
accepts values returened by function in b;
In function start Loop with 1 and less than a

also try this code:

``````#include<iostream>
using namespace std;
void function (int a);
void main ()
{
cout<<"This program finds the factor of a no."<<endl;
int a;
cin>>a;
function(a);
}
void function ( int a)
{
cout<<"the factors of "<<a<<" are :"<<endl;
for(int x=1; x<a ;x++)
{
if (a%x==0)
cout<<x<<endl;
}
}
``````

Edited by mike_2000_17: Fixed formatting

@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 (that too, except prime factors of primes) and not for general factors.

Edited by NP-complete: n/a

@NP-complete:
Perhaps I did not explain myself completely. With respect to the code, the variable `x` iterates between 1 and the maximum feasible number(the square-root) of that number(?), for which that number can have a factor.

Just try running the code, and you'll see that if you enter 16 as the number, you get the following output:

``````16 = 16 x 1
16 = 8 x 2
16 = 4 x 4``````

which covers all the factors of 16

Edited by amrith92: n/a

okay... i got the point but surely this line:

Now, for any given number, the maximum value of the factor it can have is always less than or equal to its square-root

is wrong.

You have implemented your logic in a different way, and that's why all the confusion.

In my opinion maryam ahmmed's code is slightly better in terms of presentation except that:
1) Its not properly indented :D
2) He uses void main() :@
3) The range of the loop should have been:

``for(int x=1; x<=[B]a/2[/B]; x++)``

cheers...

Edited by NP-complete: n/a

@NP-complete
I agree with you with that point :) Sorry if I caused any undue confusion. Thanks for clarifying it

Amrith

Edited by amrith92: n/a

my pleasure...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.