import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MonthFinder {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
System.out.println("Enter a numeric month:");
String input = br.readLine();
int month = Integer.parseInt(input);
String strMonth;
switch (month) {
case 1:
strMonth = "January";
break;
case 2:
strMonth = "February";
break;
case 3:
strMonth = "March";
break;
case 4:
strMonth = "April";
break;
case 5:
strMonth = "May";
break;
case 6:
strMonth = "June";
break;
case 7:
strMonth = "July";
break;
case 8:
strMonth = "August";
break;
case 9:
strMonth = "September";
break;
case 10:
strMonth = "October";
break;
case 11:
strMonth = "November";
break;
case 12:
strMonth = "December";
break;
default:
strMonth = "Invalid Month";
}
System.out.println("Month " + month + " is:" + strMonth);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
Tag Archives: conditional operator
Java program to find grade
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Grade {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
System.out.println("Enter a valid score:");
String input = br.readLine();
int score = Integer.parseInt(input);
String grade = "";
/* if (score >= 90) {
grade = "A+";
} else if (score >= 80 && score < 90) {
grade = "A";
} else if (score >= 70 && score < 80) {
grade = "A-";
} else if (score >= 60 && score < 70) {
grade = "B+";
} else if (score >= 50 && score < 60) {
grade = "B";
} else if (score >= 40 && score < 50) {
grade = "B-";
} else {
grade = "F";
}*/
if (score >= 90) {
grade = "A+";
} else if (score >= 80) {
grade = "A";
} else if (score >= 70) {
grade = "A-";
} else if (score >= 60) {
grade = "B+";
} else if (score >= 50) {
grade = "B";
} else if (score >= 40) {
grade = "B-";
} else {
grade = "F";
}
System.out.println("score:" + score + " grade:" + grade);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
Java program to check whether the numbers are pythagorean triple or not
A Pythagorean triple consists of three positive integers x, y, and z, such that x2 + y2 = z2. Such a triple is commonly written (x, y, z), and a well-known example is (3, 4, 5) , i.e 32 + 42 = 9 + 16 = 25 = 52. If (x, y, z) is a Pythagorean triple, then so is (kx, ky, kz) for any positive integer k. A primitive Pythagorean triple is one in which x, y and z are coprime. A right triangle whose sides form a Pythagorean triple is called a Pythagorean triangle.
// java program to check whether the numbers are pythagorean triple or not
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PythagoreanTriple {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
System.out.println("Enter number1:");
String input = br.readLine();
int a = Integer.parseInt(input);
System.out.println("Enter number2:");
input = br.readLine();
int b = Integer.parseInt(input);
System.out.println("Enter number3:");
input = br.readLine();
int c = Integer.parseInt(input);
System.out.println("You Entered:" + a + " " + b + " " + c);
if ((Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2))
|| (Math.pow(b, 2) + Math.pow(c, 2) == Math.pow(a, 2))
|| (Math.pow(a, 2) + Math.pow(c, 2) == Math.pow(b, 2))) {
System.out.println("The Integers are pythagorean triple");
} else {
System.out.println("The Integers are not pythagorean triple");
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
Java program to calculate area of a triangle(Hero’s formula)
This is a Java Program to Find the Area of a Triangle Given Three Sides.Enter the length of three sides of triangle. Now we use the Heron’s formula to get the area of triangle.
In geometry, Heron’s formula also called Hero’s formula), gives the area of a triangle by requiring no arbitrary choice of side as base or vertex as origin, contrary to other formulas for the area of a triangle, such as half the base times the height or half the norm of a cross product of two sides.
where s is the semiperimeter of the triangle; that is,
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
public class CalcTriangleArea {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
System.out.println("Enter side1:");
String input = br.readLine();
double a = Double.parseDouble(input);
System.out.println("Enter side2:");
input = br.readLine();
double b = Double.parseDouble(input);
System.out.println("Enter side3:");
input = br.readLine();
double c = Double.parseDouble(input);
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("You Entered:" + a + " " + b + " " + c);
if ((a + b) > c && (b + c) > a && (a + c) > b) {
double s = (a + b + c) / 2;
double area = Math.sqrt((s * (s - a) * (s - b) * (s - c)));
System.out.println("Area:" + df.format(area));
} else {
System.out.println("Can't form a triangle with the gives side..");
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
Java program to find the smallest number of the three number
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class FindSmallest {
public static void main(String[] args) {
System.out.println("This program will find the smallest number of the three number");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
System.out.println("Enter number1:");
String input = br.readLine();
int num1 = Integer.parseInt(input);
System.out.println("Enter number2:");
input = br.readLine();
int num2 = Integer.parseInt(input);
System.out.println("Enter number3:");
input = br.readLine();
int num3 = Integer.parseInt(input);
int smallest = num1;
if (num2 < smallest) {
smallest = num2;
}
if (num3 < smallest) {
smallest = num3;
}
System.out.println("Number Entered:" + num1 + " " + num2 + " " + num3);
System.out.println("Smallest number:" + smallest);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}