153k views
4 votes
۷۲ Q1. Write a C++ program that takes a positive integer number and displays all the factors of that number. Thus, for example, if number=12, the program will print 1,2,3,4,6,12

User Cutie
by
5.3k points

1 Answer

3 votes

Answer:

#include <iostream>

using namespace std;

int main() {

int x;

cin>>x;

if (x>0){

for (int i=1;i<=x;i++){

if(x%i==0){

cout<<i<<" ";

}

}

}

else {

cout<<"write a positive num";

}

return 0;

}

Step-by-step explanation:

i first checked if the num is positvie if no you ask the user for a positive num

i used a loop to find the factors by dividing the entered number on every num from 1 to the num

User Zoltan
by
5.8k points