examples of if else in java | what is the output of ?

0
give us examples of if else in java | what is the output of ?

All Contributions

Coin face/image selection game:

/*(Game: heads or tails) Write a program that lets the user guess whether the flip of
a coin results in heads or tails. The program randomly generates an integer 0 or 1,
which represents head or tail. The program prompts the user to enter a guess and
reports whether the guess is correct or incorrect.*/
		
Scanner s = new Scanner(System.in);
int randomNumber, guess;
		  
System.out.print("Enter a guess(0 or 1):");
guess = s.nextInt();
		  
if(guess == 0 || guess == 1) {
        randomNumber = (int)(Math.random() * 2);
		   
   if(guess == randomNumber) {
	System.out.print("You won.");
   } 
   else {
	System.out.print("You lose.");
   }
} 
else 
{
	System.out.print("The number you entered is not valid.");
}

A program that generates a random number between 1 and 12 and then prints the name of the month corresponding to the number:

/*(Random month) Write a program that randomly generates an integer between 1
and 12 and displays the English month name January, February, …, December for
the number 1, 2, …, 12, accordingly.*/
int month=1+(int)(Math.random()*(12-1+1));
if(month==1)
	System.out.println("january");
else if(month==2)
	System.out.println("february");
else if(month==3)
	System.out.println("march");
else if(month==4)
	System.out.println("april");
else if(month==5)
	System.out.println("may");
else if(month==6)
	System.out.println("june");
else if(month==7)
	System.out.println("july");
else if(month==8)
	System.out.println("august");
else if(month==9)
	System.out.println("september");
else if(month==10)
	System.out.println("october");
else if(month==11)
	System.out.println("november");
else if(month==12)
	System.out.println("december");

Arrange 3 numbers in ascending order from smallest to largest:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
/*(Sort three integers) Write a program that prompts the user to enter three integers
and display the integers in non-decreasing order.*/

Scanner input = new Scanner(System.in);

		// Prompt the user to enter three integers
		System.out.print("Enter three integers: ");
		int number1 = input.nextInt();
		int number2 = input.nextInt();
		int number3 = input.nextInt();

		// Sort numbers
		int temp;
		if (number2 < number1 || number3 < number1)
		{
			if (number2 < number1)
			{
				temp = number1;
				number1 = number2;
				number2 = temp; 
			}
			if (number3 < number1)
			{
				temp = number1;
				number1 = number3;
				number3 = temp;
			}
		}
		if (number3 < number2)
		{
			temp = number2;
			number2 = number3;
			number3 = temp;
		}

		// Display numbers in accending order
		System.out.println(number1 + " " + number2 + " " + number3);
}
}

A program that solves quadratic equations:

/*
(Algebra: solve quadratic equations) The two roots of a quadratic equation
ax^2 + bx + c = 0 can be obtained using the following formula:

b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the
equation has two real roots. If it is zero, the equation has one root. If it is negative,
the equation has no real roots.
Write a program that prompts the user to enter values for a, b, and c and displays
the result based on the discriminant. If the discriminant is positive, display two
roots. If the discriminant is 0, display one root. Otherwise, display “The equation
has no real roots”.
Note that you can use Math.pow(x, 0.5) to compute 2x.
*/
import java.util.Scanner;

public class Exercise_03_01 {
 public static void main(String[] args) {
       
// Create a Scanner object
  Scanner input = new Scanner(System.in);

  // Prompt the user to enter values for a, b and c.
  System.out.print("Enter a, b, c: ");
  double a = input.nextDouble();
  double b = input.nextDouble();
  double c = input.nextDouble();

  // Compute the discriminant of the quadriatic equation.
  double discriminant = Math.pow(b, 2) - 4 * a * c;

  // Compute the real roots of the quadriatic equation if any.
  System.out.print("The equation has ");
  if (discriminant > 0)
  {
   double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);  
   double root2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);  
   System.out.println("two roots " + root1 + " and " + root2);
  }
  else if (discriminant == 0)
  {
   double root1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);
   System.out.println("one root " + root1);
  }
  else
   System.out.println("no real roots");
 }
}

A program that asks the user to enter the day number and prints the name of the corresponding day:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
//write java program that ask user to enter the number of week day and prints the //equiavalent week day name
        Scanner in=new Scanner(System.in);
        System.out.println("enter the day number:");
        int daynumber=in.nextInt();
        if(daynumber==1)
            System.out.println("saturday");
        else if(daynumber==2)
            System.out.println("sunday");
        else if(daynumber==3)
            System.out.println("monday");
        else if(daynumber==4)
            System.out.println("tuesday");
        else if(daynumber==5)
            System.out.println("wednsday");
        else if(daynumber==6)
            System.out.println("thursday");
        else if(daynumber==7)
            System.out.println("friday");
        else System.out.println("invalid entry");
}
}

total contributions (7)

New to examplegeek.com?

Join us