Answer to Question #287081 in C++ for Shani

Question #287081

How can we find the first three factors of an integer?

1
Expert's answer
2022-01-12T18:10:46-0500
#include<iostream>

using namespace std;

int main()
{
	int num;
	int factors[3] = {0,0,0};
	cout << "Please, enter an integer: ";
	cin >> num;
	int count=0;
	for (int i = 1; i <= num; i++)
	{
		if (num%i == 0)
		{
			factors[count] = i;
			count++;
		}
		if (count == 3)break;
	}
	cout << "\nFirst three factors of " << num;
	for (int i = 0; i < 3; i++)
	{
		if(factors[i]!=0)
			cout << "\nFactor " << i + 1 << " is " << factors[i];
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment