Java program that will take a number and print its even factors.
Answers
Answer:
This is the required Java program for this question.
import java.util.*;
public class Java {
public static void main(String[] args) {
int i, n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a positive number: ");
n=sc.nextInt();
if(n%2==1)
System.out.println("Odd number. Has no even factor.");
else {
System.out.println("Even factors are: ");
for(i=2;i<=n;i+=2) {
if(n%i==0)
System.out.print(i+" ");
}
}
sc.close();
}
}
Explanation:
- We will ask the user to enter the number. Check if the number is odd. If true, number has no even factors. If false, iterate a loop in the range I = 2 to N. Check if N is divisible by 'I' or not. If true, display the value of 'I' and then increment it's value by 2.
Answer:-
//Java program to print the even factors of a number
package Programmer;
import java.util.*;
public class Factor{
public static void main (String ar []){
Scanner sc=new Scanner (System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
for(int i=1;i<=n;i++){
if((n%i==0)&&(i%2==0))
System.out.print(i+" ");
}
}
}