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

Block vs Inline Elements In HTML

  Block: Block element always start in New Line  Block element always start in new line occupy complete width available  Example: h2, p, div Inline Element: They do not start in new line occuply width as much required  Example: img, anchor a,span There are no different between div and span, only difference is div is block element and span is Inline, it will help you to group your content or elements

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>

Find Unique Number in Array Java , XOR

 Question:  WAP to find unique number in Array Java package aRray ; import java . util .*; public class practicalArray { public static void main ( String [] args ) { Scanner scan = new Scanner ( System . in ) ; // Step 1: Input size of the array from the user int size = scan . nextInt () ; // Step 2: Create an integer array of the specified size int [] arr = new int [ size ] ; // Step 3: Initialize 'res' with the first element of the array int res = arr [ 0 ] ; // Step 4: Loop to read array elements and find the unique number for ( int i = 1 ; i < size ; i ++ ) { // Read an integer from the user and store it in the array arr [ i ] = scan . nextInt () ; // Step 5: Use XOR (^) operation to find the unique number res = res ^ arr [ i ] ; } // Step 6: Print the unique number System . out . print...