262,774 views
33 votes
33 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 TheCrzyMan
by
2.7k points

1 Answer

7 votes
7 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 Destroyer
by
2.5k points