if else in java with examples

0

conditional statements in java have 2 types, one of them is the if and else statements

it can be written in this way:

if(the condition)

{

statements

}

else{

statements

}

All Contributions

By side, a triangle has 3 types; equilateral if it has three equal sides, isosceles if it has two

equal sides and scalene if it has no equal sides. Write a Java program named TriangleType

that reads from the user 3 integer values l, m and n that represent the 3 sides of triangle, and

then determines and prints the triangle type (equilateral, isosceles, scalene). You have to

apply the triangle inequality for with the three entered triangle sides, i.e., you have to assure

that each side in a triangle is less than the sum of other two sides and greater than the

difference between them

Sample Run 1:

Enter 3 sides of a triangle: 2 18 4

Not a Triangle

=====================================

Sample Run 2:

Enter 3 sides of a triangle: 7 8 4

Triangle type: Scalene

=====================================

Sample Run 3:

Enter 3 sides of a triangle: 6 8 6

Triangle type: Isosceles

=====================================

Sample Run 4:

Enter 3 sides of a triangle: 5 5 5

Triangle type: Equilateral

=====================================

 

answer:

import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		System.out.println("Enter 3 sides of a Triangle:");
		Scanner input =new Scanner(System.in);
		int l, m, n;
		l= input.nextInt();
		m= input.nextInt();
		n= input.nextInt();
		if(l )
		{
		System.out.print("Triangle type: ");
		if (l==m==n)
		System.out.println("Equilateral");
		else if(l==m || m==n || l==n)
		System.out.println("isosceles");
		else if(l!=m && m!=n && l!=n)
		System.out.println("scalene");
		}
		else{
		    System.out.println("not a Triangle");
		}
	}
}

java program asks user to enter his numeric mark out of 100, then prints the equavalent grade:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("enter the mark:");
int mark=in.nextInt();
            
if(mark>100)
  System.out.println("invalid entry");
else if(mark>=90)
  System.out.println("A");
else if(mark>=80)
  System.out.print("B");
else if(mark>=70)
  System.out.print("C");
else if(mark>=60)
  System.out.println("D");
else
  System.out.println("F");
}
}

atm machine login screen, using java:

write java program for atm machine to ask user to enter his passkey, then check if passkey is correct or wrong:

import java.util.Scanner;

public class Main
{
 public static void main(String[] args) {
	Scanner input=new Scanner(System.in);
	int correctPasskey=3223;
	System.out.println("welcome client, please enter your passkey");
	int passkey=input.nextInt();
	if(passkey==correctPasskey)
	System.out.println("correct Passkey");
	else 
	System.out.println("wrong Passkey");
}

write java program to print result of student,

if student mark is 60 or higher then print "pass" , otherwise print "fail":

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
//write a program asks student to enter his mark, then checks if student passed of failed and print result 

Scanner input=new Scanner(System.in);
System.out.println("enter your mark:");
double mark=input.nextDouble();
if(mark >=60)
{
System.out.println("passed");
}
else
{
System.out.println("failed");
}

}
}

total contributions (4)

New to examplegeek.com?

Join us