











C++ program to find the factors of a number
Program to find Factors of a number
Here we will discuss how to find the factors of a number using C++ programming language.
Factor is a number that when multiplied with another number produces a given number.
To find the Factor of a number we will use a loop in which we start dividing the number with 1 up to the number itself and the numbers which perfectly divides the number are the factors.
For Example: 21
Factors are: 1, 3, 7, 21




Algorithm:-
- Take input number to factorize.
- Input is stored in an int type variable say num.
- A factor of num can be found in range 1 to num
- Initialize factor=1
- Run a loop from factor=1 to num
- if num%factor==0 (if num is divisible by factor)
- print factor
- increment factor , factor ++
- if num%factor==0 (if num is divisible by factor)
C++ Code:-
//C++ Program
//Factors of a number
#include <iostream>
using namespace std;
//main Program
int main()
{
int num;
cout << “Enter a positive number: “;
//user input
cin >> num;
cout << “Factors of “ << num << ” are: “ << endl;
//finding and printing factors
for(int i = 1; i <= num; i++)
{
if(num % i == 0)
cout << i << “\t“;
}
return 0;
}
Output
Enter a positive number: 36
Factors of 36 are:
1 2 3 4 6 9 12 18 36
- Positive or Negative number: C | C++ | Java
- Even or Odd number: C | C++ | Java
- Sum of First N Natural numbers: C | C++ | Java
- Sum of N natural numbers: C | C++ | Java
- Sum of numbers in a given range: C | C++ | Java
- Greatest of two numbers: C | C++ | Java
- Greatest of the Three numbers: C | C++ | Java
- Leap year or not: C | C++ | Java
- Prime number: C | C++ | Java
- Prime number within a given range: C | C++ | Java
- Factors of a number: C | C++ | Java
- Sum of digits of a number: C | C++ | Java
- Reverse of a number : C | C++ | Java
- Palindrome number: C | C++ | Java
- Armstrong number : C | C++ | Java
- Armstrong number in a given range : C | C++ | Java
- Fibonacci Series upto nth term : C | C++ | Java
- Factorial of a number : C | C++ | Java
- Power of a number : C | C++ | Java
- Factor of a number : C | C++ | Java
- Strong number : C | C++ | Java
- Perfect number : C | C++ | Java
- Automorphic number : C | C++ | Java
- Harshad number : C | C++ | Java
- Abundant number : C| C++ | Java
- Friendly pair : C | C++ | Java


Login/Signup to comment