largest factor of a number

here is the code for Displaying Factors of a Number


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
using namespace std;
int main()
{
  int n,i,a;
  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;
  }
  for(i=1;i<=n;++i)
  
  
  return 0;
}




can anyone guess how to just print out largest factor...????
Hi,

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...
Last edited on
Working off of Programmer007 since factors are always in pairs you can stop at the second factor.

example #12
factors: 1 2 3 4 6 12
stop at 2 since you can do : 2*x=12 => 12/2 =6

That's a great approach!
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;


Or something like that.
Last edited on
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <iostream>
using namespace 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..

Last edited on
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>

using namespace 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.
Last edited on
@yawzheek your code didn't worked on Codeblocks and visual studio 2015 update 2
it says something like

[Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11


and for all who see i want to make a program that can make largest factor of an odd number like 9
Last edited on
At this point I believe you are just asking someone to do your thinking for you; user ajputnam has already given you the simplest, most correct answer: http://www.cplusplus.com/forum/beginner/193443/#msg930540

It takes a very simple loop with a break statement just like finding all the factors would; the only difference is stopping at the right spot.
Topic archived. No new replies allowed.