Find LCM of two Numbers

LCM using while Loop and if Statement Output In this program, the two numbers whose LCM is to be found are stored in variables n1 and n2 respectively. Then, we initially set lcm to the largest of the two numbers. This is because, LCM cannot be less than the largest number. Inside the infinite while loop (while(true)), we check if lcm perfectly divides… Continue reading Find LCM of two Numbers

Check Leap Year

A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. Java Program to Check a Leap Year Output In the above example, we are checking if the year 1900 is a leap year or not. Since 1900 is… Continue reading Check Leap Year

Roots of a Quadratic Equation

The standard form of a quadratic equation is: Here, a, b, and c are real numbers and a can’t be equal to 0. We can calculate the root of a quadratic by using the formula: The ± sign indicates that there will be two roots: The term b2-4ac is known as the determinant of a quadratic equation. It specifies the nature of roots. That is, if determinant >… Continue reading Roots of a Quadratic Equation

Check Vowel or Consonant

Check vowel or consonant using if..else statement Output In the above program, ‘i’ is stored in a char variable ch. In Java, you use double quotes (” “) for strings and single quotes (‘ ‘) for characters. Now, to check whether ch is vowel or not, we check if ch is any of: (‘a’, ‘e’, ‘i’, ‘o’, ‘u’). This is done using a simple if..else statement.

Check Even or Odd

Output In the above program, a Scanner object, reader is created to read a number from the user’s keyboard. The entered number is then stored in a variable num. Now, to check whether num is even or odd, we calculate its remainder using % operator and check if it is divisible by 2 or not. For this, we use if…else statement in Java. If num is divisible by 2, we print num is… Continue reading Check Even or Odd