- This program should get input from user to select the case. Create two cases.
ID: 3545499 • Letter: #
Question
- This program should get input from user to select the case. Create two cases.
- If user selects first case, then program should do the following:
o Program asks user to enter any number, multiply every element of the array z by that number, and print the results.
o If user selects second case, then the program should print all the elements in array z which are either divisible by 2 or 5.{ Hint: you can use logical OR operator for a test condition. }
- Include the default case in your program that prints a message
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int[] z = new int[]{8,15,22,28,36,40,43,45,48,59,66,70} ;
int input,multi;
Scanner scan = new Scanner(System.in);
System.out.println("Enter 1 to multiply every element in the array with given number ");
System.out.println("Enter 2 to print all the numbers in the array that are divisible by 2 or 5");
System.out.print("Enter input : ");
input = scan.nextInt();
switch (input)
{
case 1:
{
System.out.print("Enter number to be multiplied : ");
multi = scan.nextInt();
System.out.println("Final array");
System.out.print("{");
for(int i=0;i<12;i++)
{
z[i]=multi*z[i];
System.out.print(z[i]);
if(i!=11)
System.out.print(" , ");
}
System.out.println(" } ");
}break;
case 2 :
{
System.out.print("{ ");
for(int i=0;i<12;i++)
{
if(z[i]%2==0 || z[i]%5==0)
{
System.out.print(z[i]);
if(i!=11)
System.out.print(" , ");
}
}
System.out.println(" } ");
}break;
default : System.out.println("Array not changed");
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.