java program to print odd factors of a number
PLS GIVE CORRECT PROGRAM
LS GIVE IT FAST
Answers
Answered by
4
Required Answer:-
Question:
- Write a Java program to print odd factors of a number.
Solution:
Here is the program.
import java.util.*;
public class OddFactors {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
int n=sc.nextInt();
System.out.println("Odd factors are...");
for(int i=1;i<=n;i+=2) {
if(n%i==0)
System.out.print(i+" ");
}
sc.close();
}
}
Explanation:
- We will ask the user to enter the number. Then, we will iterate a loop. Here, the control variable is 'i'. Initially, the value of 'i' is 1. After each iteration, value of 'i' is incremented by 2 as we are working only with odd numbers. At the same time, we will check if the number is divisible by 'i' or not. If it's divisible, then it is an odd factor else not.
Output is attached.
Attachments:
Similar questions