Skip to main content

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

Popular posts from this blog

Python Final Lectures

 Q- how to Print Hello World print("Hello World") Variables in python ------- age = 30   #variable should be intutive so that we can learn any time print(age) Note: Shift+Enter is shortcut to run command 2) ' #' this is for writing the comment in python Rules for Variables--- Variable can not be start with any number like - 1age  Number can use in between and end with variable like - age1 age2 Special characters are not allowed expect _ (underscore) like - age_my Space not allowed in variable  Python is case sensitive  Way to define Variable --- age1,age2 = 30,25  age1 = 30 age2 = 25 age1=age2=30   #if 30 age for both variable   >> Data type the type of data is basically data type Integer = age1 to age3 is basically integer   , Integer is basically full number lets check = type(age1)  #it will give u print int float=  basically decimal values Interest =  30.24 type(Interest) #answer is float Message = ...

SQL and rest python for Data analysis

SQL (Structured Query Language) की ओर — ये डेटा हैंडलिंग का अगला स्टेप है, जहाँ हम database से data को fetch, update, delete, filter, aur organize करना सीखते हैं। 💾 SQL क्या है (What is SQL)? SQL का मतलब है Structured Query Language — ये एक database language है जिसका इस्तेमाल data को store, access, और manage करने के लिए किया जाता है। जैसे Excel में data sheets होती हैं, वैसे SQL में tables होती हैं। Type Keyword Use 1️⃣ DDL (Data Definition Language) CREATE , ALTER , DROP Database structure change करने के लिए 2️⃣ DML (Data Manipulation Language) INSERT , UPDATE , DELETE Data change करने के लिए 3️⃣ DQL (Data Query Language) SELECT Data निकालने के लिए 4️⃣ DCL (Data Control Language) GRANT , REVOKE Permission देने या हटाने के लिए 5️⃣ TCL (Transaction Control Language) COMMIT , ROLLBACK Changes को confirm या cancel करने के लिए Download MY SQL From Google >>>Lets learn this concept compare with excel Concept in SQL Excel Equivalen...

Add CSS using external CSS

>>> U just need to create a another page and save it with the name style.css >>> and then go to link that style page with your html docs how to link your css with html page ? >>> You can find code below , it will help you to link your external page with your html docs <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Divyanshu Khare || MERN Developer</title> <meta description = "description" content="Divyanshu Khare's website"> <link rel="stylesheet" type="text/css" href="style.css">   <!----------link external css page ---------> </head> <body> </body> </html>