Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Java program named Homework8 . Design a method printOddNumbers that pri

ID: 3601332 • Letter: C

Question

Create a Java program named Homework8.

Design a method printOddNumbers that prints all positive, odd numbers up to a given max.

Design a method printPrimeFactors that prints all positive prime factors of a given number.
-For example, 12 has the following factors: 1, 2, 3, 4, 6, 12. However, the only prime numbers are 2 and 3. I should expect to see 2 and 3.

Design a method printCorrectRange that accepts a positive integer number. If the number is between 1 (inclusive) and 69 (inclusive), print "You have failed the class". If the number is between 70 (inclusive) and 79 (inclusive) , print "You have barely passed". If the number is between 80 (inclusive) and 100 (inclusive), print "You have mastered the class". If the number is greater than 100, print "You're off the charts".

Explanation / Answer


import java.io.*;
import java.lang.Math;

public class GFG
{
  
  
public static void printOddNumbers(int n)
{

  

for (int i = 1; i <= n; i+= 2)
{
  
System.out.print(i + " ");

  
}

}
  
  
  

public static void printprimeFactors(int n)
{

while (n%2==0)
{
System.out.print(2 + " ");
n /= 2;
}

for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
while (n%i == 0)
{
System.out.print(i + " ");
n /= i;
}
}

if (n > 2)
System.out.print(n);
}
  
  
  
public static void printCorrectRange(int n)
{
if(n>=1 && n<=69)
System.out.print("You have failed the class");
else if(n>=70 && n<=79)
System.out.print("You have barely passed");
else if(n>=80 && n<=100)
System.out.print("You have mastered the class");
else if(n>100)
System.out.print("You're off the charts");

}






public static void main (String[] args)
{
int n = 58;
System.out.print("Odd numbers upto "+ n + " are ");
printOddNumbers(n);
System.out.print(" Prime factors of "+ n + " are ");
printprimeFactors(n);
System.out.print(" Status : ");
printCorrectRange(n);
}
}