C++ Program:

- To print all the factors of a number

Example:

User Input: 15

Output:
Factors of 15 : 1 3 5 15

#include <iostream>
using namespace std;

int main() {
  int num;
  cout<<"Enter your number : ";
  cin>>num;
  cout<<"Factors of "<<num<<" : ";

  for(int i=1; i<=num/2; i++) {
    if(num % i==0)
    cout<<i<<" ";
  }
  cout<<num;
  return 0;
}

Output:

  Enter your number : 18
  Factors of 18 : 1 2 3 6 9 18
Program
number

You should know first :

Factor of N : All numbers which perfectly divides N


Main Logic :

for(int i=1; i<=num/2; i++) {
  if(num % i==0)  // Checking for the factor
  cout<<i<<" ";
}
cout<<num;

We are running the loop num/2 Since, any number can have only factors under its half, except itself

Concept

Coming Soon !

Quick
Tutorial

Quote

Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.