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

Excel For Data Analysis

 sum = Add numbers, it is basically sumission  =SUM(number1, [number2], ...) Example: =SUM(A1:A7)  ⚡ Tips: You can use AutoSum for quick addition: Select a cell → click Home → AutoSum (Σ) → press Enter . Shortcut key: Alt + = (automatically inserts =SUM() ). Min = TO FIND MINIMUM VALUE  =MIN(A1:A12)  max = TO FIND MAXIMUM VALUE =MAX(A1:A13)  Right  ExamPle: =RIGHT(E2,10)  basically mere pas number hai 91 country code ke sath, main chahta hu 91 code ke bad right se 10 numbers mujhe nikal ke aa jaye yaha E2 mera column number hai and 10 jo mujhe chahiye right se Cases: किसी product code ke last digits निकालने के लिए  किसी date ya year ke last 2 digits निकालने के लिए किसी naam ke last letters निकालने के लिए Note: if I want to paste horizontally number then will use 'paste ke option me transpose left  == left se digits extract krne ke liye will use this  =LEFT(E2,2)   E2 IS CELL AND 2 IS DIG...

Statistics Data Science

  Types of Data: - Majorly deal with 2 types of data : 1) Numberical Data[continuos data] ex: age, salary, height                                                             Objective: Quantative Study                                                              2) Categorical Data[Dispreet Data]: ex: Gender, City etc                                                ...

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>