can anyone guess how to just print out largest factor...????
technically the largest factor is the number itself so you are better off printing the number itself
so cout<<n will do the magic!
but if you are trying to find out the largest factor (apart from) the number itself you may use a array and then print the second last digit, and if there are only 2 digit then the number is prime and then it won't have largest factor (well except the number itself)
also, it would be great if you could show us your code/attempt...
Well since a factor (other than the number itself) can never really be more than half the value of an integer, say n, you could test until another integer i is <= i/n and i%n == 0, I would think. Surely there are better ways, but that just sprang to mind quickly. Store them in a vector v and print v[v.size() - 1]
EDIT: Though the vector is probably overkill. You could probably just use a local variable and rewrite it as needed, then print that.
1 2 3
int greatest_factor{0};
for(size_t i = 0; i <= n/2; ++i) if (i % n == 0) greatest_factor = i;
cout << "The greatest factor of the number " << n << " is " << greatest_factor << endl;
i was talking about largest factor of a number apart from the number itself and question arised when we see that if a number is even logic number/2 perfectly works but in case of odd number it can't work so required to make a good logic
#include <iostream>
usingnamespace std;
int main()
{
int i,n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factors of " << n << " are: " << endl;
for(i=1;i<=n;++i)
{
if(n%i==0)
cout << i << endl;
}
cout <<"\n largest factor of number is " <<endl;
cout <<n/2;
/* problem is that when you have to
find greatest factor of an odd number
this logic n/2 does not work , there
should be something else */
return 0;
}
output is like
if i enter 8
it gives factor are 1,2,4,8
and
largest factor of number is 4
but in case of odd no. like 9
it gives factors are 1,3,9
and
largest factor is 4 //this is error..
I'm... not sure what you're asking for? I just ran it in Shell, and I received:
Enter a positive integer: 9
Factors of 9 are:
1
3
9
And nothing more, which is still correct. Division by 2 - even taking into account modulo arithmetic - isn't going to change the result for odd numbers, because 4.5? Not really an acceptable answer. Even running it with an even number:
Enter a positive integer: 8
Factors of 8 are:
1
2
4
8
You'll always receive the original number. If you ONLY want the LARGEST factor that ISN'T the original number (because otherwise n == n, yes?) Well...
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main()
{
cout << "Enter a positive number: ";
int n{ 0 };
cin >> n;
int largest_factor{ 0 };
for (unsigned i = 1; i <= n / 2; ++i) if (n % i == 0) largest_factor = i;
cout << "The largest factor of " << n << " == " << largest_factor << endl;
return 0;
}
Is that what you're looking for?
EDIT: Removed an #include of vector - copy/pasted from VS from something I was working on previously. Sorry.