Find factors of a number in C, C++, Java and Python | faceprep

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 program to find factors of a number using loops

#include <stdio.h>

int main()
{
int num;
printf(“\nEnter the number : “);
scanf(“%d”,&num);
int i,count = 0;
printf(“\nThe factors of %d are : “,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++ program to find factors of a number using loops

#include <iostream>
using namespace std;

int main()
{
int num;
cout << “\nEnter the number : “;
cin >> num;
int i,count = 0;
cout << “\nThe factors of ” << num << ” are : “;
for(i = 1; i <= num; i++)
{
if(num % i == 0)
{
++count;
cout << i << ” “;
}
}
cout << “\n\nTotal factors of ” << num << “: ” << count << endl;
return 0;
}

// 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 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)

 

Program to find factors of a number - an efficient approach

#include <stdio.h>
#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”,&num);
int i,count = 0;
printf(“\nThe factors of %d are : “,num);
find_factors(num);

}

#include <iostream>
#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 << ” ” << num/i;
}
}
}

int main()
{
int num;
cout << “\nEnter the number : “;
cin >> num;
int i;
cout << “\nThe factors of ” << num << ” are : “;
find_factors(num);

}

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 + ” “);
}
}

}
}

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

factors of a number