IF ELSE , MODULO & BOOLEAN QUESTION WITH SWITCH BUTTON | PRACTICE SERIES 001 (PART 02) | A JAVA JOURNEY BY DIVYANSHU KHARE
Question: Ask the user to enter the number of the month & print the name of the month. For eg - For ‘1’ print ‘January’, ‘2’ print ‘February’ & so on. Ans: First I'm going to use switch control statement to solve this question .
package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int month = scan.nextInt();
switch (month) {
case 1 : System.out.print("January");
break;
case 2 : System.out.print("February");
break;
case 3 : System.out.print("March");
break;
case 4 : System.out.print("April");
break;
case 5 : System.out.print("May");
break;
case 6 : System.out.print("June");
break;
case 7 : System.out.print("July");
break;
case 8 : System.out.print("August");
break;
case 9 : System.out.print("September");
break;
case 10 : System.out.print("October");
break;
case 11 : System.out.print("November");
break;
case 12 : System.out.print("December");
break;
default : System.out.print("Invalid");;
}
}
}
This is code if you are using Switch control statement
there is a another way to solve this question , You can use if else statement also to solve this question.
package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int month = scan.nextInt();
if (month == 1) {
System.out.print("January");
}
else if (month == 2){
System.out.print("February");
}
else if (month == 3){
System.out.print("March");
}
else if (month == 4) {
System.out.print("April");
}
else if (month == 5){
System.out.print("may");
}
else if (month == 6){
System.out.print("June");
}
else if (month == 7) {
System.out.print("July");
}
else if (month == 8){
System.out.print("August");
}
else if (month == 9) {
System.out.print("September");
}
else if (month == 10) {
System.out.print("October");
}
else if (month == 11) {
System.out.print("November");
}
else if (month == 12) {
System.out.print("December");
}
else {
System.out.print("Invalid");
}
}
}
This is if else and else if statement yaha hamne else if lagaya hai kyu ki ek se jyada if ke statement me else if ka use krte hai .
Question:Grading System: Write a Java program that takes a student's score as input and prints their corresponding grade according to the following scale: 90-100: A 80-89: B 70-79: C 60-69: D Below 60: F Ans: package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
if(i >= 90 && i <= 100) {
System.out.print("A");
}
else if (i >= 80 && i <= 89) {
System.out.print("C");
}
else if (i >= 70 && i <=79) {
System.out.print("C");
}
else if (i >=60 && i <= 69) {
System.out.print("D");
} else if (i < 60){
System.out.print("F");
} else {
System.out.print("Invalid");
}
}
}
Question: Positive, Negative, or Zero: Write a Java program that takes a number as input and prints whether it's positive, negative, or zero. Ans: package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
if(n ==0) {
System.out.print("Zero");
}
else if (n >0) {
System.out.print("Positive");
}
else if (n<0) {
System.out.print("Negative");
} else {
System.out.print("Enter Your Valid Number");
}
}
}
Question: Maximum of Three Numbers: Write a Java program that takes three numbers as input and prints the largest of the three.
Ans:
package HelloWorld;
import java.util.Scanner;
public class LearningJava {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
if (a >b && a>c) {
System.out.print("a" + " " + "is Largest Number");
}
else if (b >a && b>c) {
System.out.print("b" + " " +"is Largest Number");
} else if (c >a && c >b) {
System.out.print("c" + " " + "is Largest Number");
} else {
System.out.print("Numbers are not distinct");
}
}
}
-----------------------------------------------------------------------------------------------
Questions: Day of the Week: Write a program that takes a number (1-7) as input and outputs the corresponding day of the week.
Ans: package IfElseQuestions;
import java.util.Scanner;
public class Question01 {
public static void main(String[] args) {
// Question: Day of the Week: Write a program that takes a number (1-7)
// as input and outputs the corresponding day of the week.
Scanner scan = new Scanner(System.in);
int day = scan.nextInt();
switch (day) {
case 1: System.out.print("Monday");
break;
case 2: System.out.print("Tuesday");
break;
case 3: System.out.print("Wednesday");
break;
case 4: System.out.print("Thursday");
break;
case 5: System.out.print("Friday");
break;
case 6: System.out.print("Saturday");
break;
case 7: System.out.print("Sunday");
}
}
}
------------------------------------------------------------------------------------------------
Questtion; Color Classification: Write a program that takes a color code (R, G, B) as input and outputs the corresponding color name (Red, Green, Blue).
Ans: package IfElseQuestions;
import java.util.Scanner;
public class colorCode {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter color code (R, G, B): ");
char ch = scan.next().charAt(0);
switch (ch){
case 'R': System.out.print("Red");
break;
case 'G': System.out.print("Green");
break;
case 'B': System.out.print("Blue");
break;
}
}
}
Comments
Post a Comment