Computer Science, asked by souravpoddar903132, 8 months ago

Print the sum of all the factors of a number in java​

Answers

Answered by AnubhavGhosh1
2

Answer:

Factors of a Positive Integer

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 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, number whose factors are to be found is stored in the variable number (60).

The for loop is iterated until i <= number is false. In each iteration, whether number is exactly divisible by i is checked (condition for i to be the factor of number) and the value of i is incremented by 1.

Answered by singhdivyansh9303
0

Answer:

system. out. println it is the

Similar questions