write a java program to print the factors of a number and also the product
Answers
Answer:
The following Java program prints the factors of a number and also the product:
public class Factors{
public static void main(String[] args)
{
int num=10;
int i;
int prod = 1;
for(i=1;i<=10:i++)
{
if(num%i==0)
System.out.print(i);
prod = prod * i;
}
System.out.printn(prod):
}
}
Explanation:
The factor of a number:
A factor of a number is defined as that number that can divide the number completely.
For example: 10 can be completely divided by 1, 2, and 5. So 1, 2, and 5 are the factors of 10.
Now let's understand the program that we have written for finding the factors of a number:
- First, we create a public class named Factors and inside that class, we define our main function.
- Then we write our number in the num variable.
- A for loop is used that will run from 1 to the number and every time we check whether the value of i can divide the number or not.
- If i can divide a number completely it means that i is a factor of the number, otherwise not.
- Product is calculated for every factor.
- Lastly, we print the output.
#SPJ2
Answer:
java program to print the factors of a number
public class Main {
public static void main(String[] args) {
// positive number
int number = 60;
System. out.print("Factors of " + number + " are: ");
// loop runs from 1 to 60
for (int i = 1; i <= number; ++i) {
// if the number is divided by i
// i is the factor
if (number % i == 0) {
System.out.print(i + " ");
}
}
}
}
OUTPUT:
Factors of 60 are : 1 2 3 4 5 6 10 12 15 20 30 60
- In the above program, a number whose elements are to be discovered is saved withinside the variable number (60).
- The for loop is iterated until i <= number is false. In every iteration, whether or not the range is precisely divisible with the aid of using i is checked (the situation for i to be the aspect of number) and the value of i is incremented with the aid of using 1
#SPJ2