Using Java Check whether a number is even or odd using if…else statement Check whether a number is even or odd using ternary operator
Continue reading Program to Check Whether a Number is Even or OddProgram to Check Whether a Number is Even or Odd
Using Java
Check whether a number is even or odd using if…else statement
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = reader.nextInt();
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
Output
Enter a number: 12
12 is even
Check whether a number is even or odd using ternary operator
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = reader.nextInt();
String evenOdd = (num % 2 == 0) ? "even" : "odd";
System.out.println(num + " is " + evenOdd);
}
}
Output
Enter a number: 13
13 is odd
Using Java Swap two numbers using temporary variable Swap two numbers without using temporary variable
Continue reading Program to Swap Two NumbersThe Java Math class provides more advanced mathematical calculations than what the basic Java math operators provide. The Math class contains methods for finding the maximum or minimum of two values, rounding values, logarithmic functions, square root, and trigonometric functions (sin, cos, tan etc.). The Math is located in the java.lang package, and not in the java.math package. Thus, the fully qualified class name of …
Continue reading Math Class in javaUsing Java Here we have two variables vcount and ccount to keep the count of vowels and consonants respectively. We have converted each char of the string to lowercase using toLowerCase() method for easy comparison. We are then comparing each char of the string to vowels ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ using charAt() method and if..else..if statement, if a match is found then we …
Continue reading Program to Count Vowels and Consonants in a StringConvert Char To String and a String to char
Continue reading String ProgramsUsing Java convert char to String We have following two ways for char to String conversion in JavaMethod 1: Using toString() methodMethod 2: Usng valueOf() method Converting String to Char We can convert a String to char using charAt() method
Continue reading Convert Char To String and a String to charUsing Java calculate power of a number using for loop Here number is the base and p is the power (exponent). So we are calculating the result of number^p calculate power of a number using pow() function
Continue reading Program to Calculate Power of a NumberThe alphabets A, E, I, O and U (smallcase and uppercase) are known as Vowels and rest of the alphabets are known as consonants Using Java
Continue reading Program to check Vowel or Consonant using Switch CaseThis operator evaluates a boolean expression and assign the value based on the result. variable num1 = (expression) ? value if true : value if false Using Java
Continue reading Program to find the smallest of three numbers using ternary operatorThe number which is only divisible by itself and 1 is known as prime number. For example 2, 3, 5, 7…are prime numbers. Here we will see two programs: 1) First program will print the prime numbers between 1 and 100 Using Java
Continue reading Program to display the prime numbers from 1 to 100