Write a program in Java to print factors from 1 to 100 using 1 loop.
Note:Use only 1 loop
Example factors of
14=2,7,14
Output must be attached with the answer.
Answers
# Python Program to find the factors of a number
# This function computes the factor of the argument passed
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num = 100
print_factors(num)
please mark as brainliest★:)
Solution:
The given problem is solved using language - Java.
public class Main{
public static void main(String args[]){
display(1);
}
static void display(int i){
if(i<=100){
factors(i);
display(++i);
}
}
static void factors(int n){
System.out.print("Factors of "+n+" are: ");
for(int i=1;i<=n;i++){
if(n%i==0)
System.out.print(i+" ");
}
System.out.println("\n--------------------------");
}
}
Explanation:
Basically, we cannot use more than 2 loops to display the factors of all the numbers from 1 to 100. So, we have to implement recursive algorithm for solving this problem.
Recursive function is a function that calls itself a finite number of times.
Here, display() is a recursive function which calls itself 100 times displaying the factors of number from 1 to 100 by calling factors() function.
See attachment for output.