Computer Science, asked by ankita711, 3 months ago

write a program in java to enter a number and find the factor of that no.
using 'for' loop.
I need the answer.Help me .​

Answers

Answered by janam2004
1

Answer:

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 + " ");

}

}

}

}

Answered by goldfinger
1

Answer:

// Java Program to Find factors of a number using For Loop

package SimplerPrograms;

import java.util.Scanner;

public class FactorsOfNumberUsingFor {

private static Scanner sc;

public static void main(String[] args) {

int Number, i;

sc = new Scanner(System.in);

System.out.println("Please Enter any number to Find Factors: ");

Number = sc.nextInt();

for(i = 1; i <= Number; i++) {

if(Number%i == 0) {

System.out.format(" %d ", i);

}

}

}

}

Java Program to Find Factors of a Number First Iteration

For the first Iteration, Number = 6 and i = 1

Condition inside the For loop (1 <= 6) is TRUE. So, the compiler will start executing statements inside the For loop

Within the for loop, we have the If Statement and the condition if (6 % 1 == 0) is TRUE. So, System.out.format(” %d “, i); statement will be printed

Lastly, i will increment by 1. Please refer to Increment and Decrement Operators in Java article to understand the ++ notation

Similar questions