Write a Java program that prints asks the user to enter a positive value (1 less
ID: 668106 • Letter: W
Question
Write a Java program that prints asks the user to enter a positive value (1 less than or equal to 200) and print each number up to that line, 1 per line with the following changes (SNAP CRACKLE POP).
If a number is divisible by 2 (but not 3 or 5) print out SNAP instead of the integer.
If a number is divisible by 3 (but not 2 or 5) print out CRACKLE instead of the integer.
If a number is divisible by 5 (but not 2 or 3) print out POP instead of the integer.
If a number is divisible by 2 and 3 (but not 5) print out SNAPCRACKLE instead of the integer.
If a number is divisible by 2 and 5 (but not 3) print out SNAPPOP instead of the integer.
If a number is divisible by 3 and 5 (but not 2) print out CRACKLEPOP instead of the integer.
If a number is divisible by 2, 3, and 5 print out SNAPCRACKLEPOP instead of the integer.
Explanation / Answer
import java.io.*;
class prg
{
public static void main(String args[])
{
int n,i;
DataInputStream o = new DataInputStream(system.in);
System.out.println("Enter Positive number less than 200");
String s1=o.readLine();
n= Integer.parseInt(s1);
for(i=1;i<=n;i++)
{
System.out.println(i);
}
if(n%2==0 && n%3 !=0 && n%5!=0)
{
System.out.println("SNAP");
}
if(n%2!=0 && n%3 ==0 && n%5!=0)
{
System.out.println("CRACKLE");
}
if(n%2!=0 && n%3 !=0 && n%5==0)
{
System.out.println("POP");
}
if(n%2==0 && n%3 ==0 && n%5!=0)
{
System.out.println("SNAPCRACKLE");
}
if(n%2==0 && n%3 !=0 && n%5==0)
{
System.out.println("SNAPPOP");
}
if(n%2!=0 && n%3 ==0 && n%5==0)
{
System.out.println("CRACKLEPOP");
}
if(n%2==0 && n%3 ==0 && n%5==0)
{
System.out.println("SNAPCRACKLEPOP");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.