Select Articles

Finding factors of a number in C, C++, Java and Python

Program to find factors of a number is discussed here. Given a number, all the numbers that divide the given number are produced as output.

For example, the factors of number 15 are

1 * 15 = 15

3 * 5 = 15

5 * 3 = 15

15 * 1 = 15

1, 3, 5, 15 are the factors of 15.

Program to find factors of a number using loops

C

// C program to find factors of a number using loops

#include

int main()

{

int num;

printf(“\nEnter the number : “);

scanf(“%d”,#);

int i,count = 0;

printf(“\nThe factors of ?re : “,num);

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

{

if(num % i == 0)

{

++count;

printf(“%d “,i);

}

}

printf(“\n\nTotal factors of %d : %d\n”,num,count);

}

C++

// C++ program to find factors of a number using loops

#include

using namespace std;

int main()

{

int num;

cout << “\nEnter the number : “;

cin >> num;

int i,count = 0;

cout << “\nThe factors of ” << num>

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

{

if(num % i == 0)

{

++count;

cout << i>

}

}

cout << “\n\nTotal factors of ” << num>

return 0;

}

JAVA

// Java program to find factors of a number using loops

import java.util.*;

public class Main

{

public static void main(String[] args)

{

int num;

Scanner sc = new Scanner(System.in);

System.out.print(“\nEnter a number : “);

num = sc.nextInt();

int i, count = 0;

System.out.print(“\nThe factors of ” + num + ” are : ” );

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

{

if(num % i == 0)

{

count = count + 1;

System.out.print(i + ” “);

}

}

System.out.print(“\nTotal factors of ” + num + ” : ” + count);

}

}

PYTHON 3

# Python program to find factors of a number using loops

num = int(input(“Enter a number : “))

count = 0

print(“The factors of”,num,”are”,end=” “)

for i in range(1, num+1, 1):

if(num % i == 0):

count = count + 1

print(i, end = ” “)

print(“\nTotal factors of”,num,”:”,count)

Output

Input- Enter the number :60 Output- The factors of are 60 :1 2 3 4 5 6 10 12 15 20 30 60 Total factors of 60:12

Program to find factors of a number - an efficient approach

C

#include

#include <math.h>

int find_factors(int num)

{

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

{

if (num % i == 0)

{

if (num/i == i)

printf(“%d “, i);

else

printf(“%d %d “, i, num/i);

}

}

}

int main()

{

int num;

printf(“\nEnter the number : “);

scanf(“%d”,#);

int i,count = 0;

printf(“\nThe factors of ?re : “,num);

find_factors(num);

}

C++

#include

#include <math.h>

using namespace std;

int find_factors(int num)

{

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

{

if (num % i == 0)

{

if (num/i == i)

cout << i>

else

cout << ” ” << i>

}

}

}

int main()

{

int num;

cout << “\nEnter the number : “;

cin >> num;

int i;

cout << “\nThe factors of ” << num>

find_factors(num);

}

JAVA

import java.util.*;

public class Main

{

public static void main(String[] args)

{

int num;

Scanner sc = new Scanner(System.in);

System.out.print(“\nEnter a number : “);

num = sc.nextInt();

int i, count = 0;

System.out.print(“\nThe factors of ” + num + ” are : ” );

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

{

if(num % i == 0)

{

count = count + 1;

System.out.print(i + ” “);

}

}

System.out.print(“\nTotal factors of ” + num + ” : ” );

for (i=1; i<=Math.sqrt(num); i++)

{

if (num % i == 0)

{

if (num/i == i)

System.out.print(” ” + i + ” ” );

else

System.out.print(i + ” ” + num/i + ” “);

}

}

}

}

PYTHON 3

import math

num = int(input(“Enter a number : “))

count = 0

print(“The factors of”,num,”are”,end=” “)

i = 1

while(i <= int(math.sqrt(num))):

if (num % i == 0):

if (int(num/i) == i):

print(i,end = ” “)

else:

print(i,” “,int(num/i),end = ” “)

i = i + 1

Output

Input- Enter the number :60 Output- The factors of are 60 :1 2 3 4 5 6 10 12 15 20 30 60 Total factors of 60:12