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 = ...

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>

Python

Indexing--  it will help u to fetch single character  string= "Python" string[2] # slicing process of fetching a sub-string from the given string  #sequence of charater we can fetch means more than 1 character string="Divyanshu" string[2:4+1]   #basically here number is index value #string[start_index:end+index+1] string = "Hunny"   #indexing agr positive hai to left se count hoga #right se negative me string[:]  #it will give u entire string #now If i want to any characterwise index like string is Hunny and I want un only #string[start_index:end_index+1:step] string[1:4:2] #reverse your string #string[:: -1] string="Baba hunny" string[:: -1] # to convert into lowecase string="New Divyanshu" new_string=string.lower()  #new_string becase we've to create another string print(new_string) s1={1,2,3,4,5}    s2={3,2,8,67,85} s3=s1.union(s2) s3   #isme add hota hai whole value lekin common value update nhi hongi #intersection - ...