Factoring of integers. Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 150 (which equals 2 * 3 * 5 * 5), the program should print: The factors of 150 are: 2 3 5 5

Respuesta :

Answer:

#include <iostream>

using namespace std;

int main()

{

   int num, i;

   cout << "Enter a positive integer: ";

  cin >> num;

  cout << "Factors of " << num << " are: " << endl;  

   for(i = 1; i <= num; ++i)

   {

if(num % i == 0)

     cout << i << endl;

   }

   return 0;

}

Explanation:

Above program is written in C++ language and which will calculate all possible factors of a integer number.

Following are the program to calculate the factor:

Program Explanation:

  • Declared "n" variable that uses "inputs with int" method that for the input value.
  • In the next step, an integer variable "i" is defined that holds a value "2", and uses two while loops.
  • In the outer loop, it uses the "i" variable that checks its value less than "n".
  • In the inner loop, it checks even number conditions and calculates and prints its value.

Program:

n = int(input('Enter a number: '))#defining n variable that inpts number value

print('Factors of n are: ')#print mesage

i = 2#defining an integer variable that hold a value

while i <= n:#defining a loop that check i less than equal to n

   while n % i == 0:#use loop that check number is even  

       print(i, end=' ')#print Factors

       n //= i#holding integer part of number

   i += 1#incrementing i value

Output:

Please find the attached file.

Learn more:

brainly.com/question/24689477

Ver imagen codiepienagoya