Found 267 Articles for Java8

Java program to count the number of vowels in a given sentence

Lakshmi Srinivas
Updated on 03-Nov-2023 03:21:12

27K+ Views

To count the number of vowels in a given sentence: Read a sentence from the user Create a variable (count) initialize it with 0; Compare each character in the sentence with the characters {'a', 'e', 'i', 'o', 'u' } If a match occurs increment the count. Finally print count. Example import java.util.Scanner; public class CountingVowels {    public static void main(String args[]){       int count = 0;       System.out.println("Enter a sentence :");       Scanner sc = new Scanner(System.in);       String sentence = sc.nextLine();       for (int i=0 ; i

Java program to print the Armstrong numbers between two numbers

Ankith Reddy
Updated on 18-Jun-2024 15:34:13

14K+ Views

An Armstrong number is a number which equals to the sum of the cubes of its individual digits. For example, 153 is an Armstrong number as −153 = (1)3 + (5)3 + (3)3 153 1 + 125 + 27 154 153Algorithm1. Take integer variable Arms. 2. Assign a value to the variable. 3. Split all digits of Arms. 4. Find cube-value of each digit. 5. Add all cube-values together. 6. Save the output to Sum variable. 7. If Sum equals to Arms print Armstrong Number. 8. If Sum does not equal to Arms print Not Armstrong Number.Example Below is an ... Read More

Java program to count the number of consonants in a given sentence

karthikeya Boyini
Updated on 21-Jun-2024 14:36:09

8K+ Views

To count the number of consonants in a given sentence: Read a sentence from the user. Create a variable (count) initialize it with 0; Compare each character in the sentence with the characters {'a', 'e', 'i', 'o', 'u' } If match doesn't occurs increment the count. Finally print count.ExampleBelow is an example to count the number of consonants in a given sentence in Javaimport java.util.Scanner; public class CountingConsonants {    public static void main(String args[]){       int count = 0;       System.out.println("Enter a sentence :");       Scanner sc = new Scanner(System.in);       String sentence = sc.nextLine();       for (int i=0 ; i

Java program to find whether the given character is an alphabet or not

Samual Sam
Updated on 13-Mar-2020 10:01:20

1K+ Views

Read a character from user verify whether it lies between a and z (both small and capital). If it does it is an alphabet.Exampleimport java.util.Scanner; public class AlphabetOrNot {    public static void main(String args[]){       System.out.println("Enter a character :: ");       Scanner sc = new Scanner(System.in);       char ch = sc.next().charAt(0);       if(((ch >= 'A' && ch = 'a' && ch

Java program to find largest of the three numbers using ternary operators

Arjun Thakur
Updated on 13-Mar-2020 10:00:30

451 Views

The conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as −variable x = (expression) ? value if true : value if falseExampleLive Demopublic class LargestOf3Nums_TernaryOperator {    public static void main(String args[]) {       int a, b, c, temp, result;       a = 10;       b = 20;       c = 30;       temp = a < b ? a:b;       result = c < temp ? c:temp;       System.out.println("Largest number is ::"+result);    } }OutputLargest number is ::30

Java program to find smallest of the three numbers using ternary operators

Lakshmi Srinivas
Updated on 13-Mar-2020 09:59:17

742 Views

The conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as −variable x = (expression) ? value if true: value if falseExampleLive Demopublic class SmallestOf3NumsUsingTernary {    public static void main(String args[]) {       int a, b, c, temp, result;       a = 10;       b = 20;       c = 30;       temp = a > b ? a:b;       result = c > temp ? c:temp;       System.out.println("Smallest number is ::"+result);    } }OutputSmallest number is ::10

Java program to calculate student grades

karthikeya Boyini
Updated on 13-Mar-2020 09:57:09

13K+ Views

The following program accepts average from the user, calculates the grade and prints it.Examplepublic class CalculateStudentGrades {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter average of your marks (less than 100)::");       int average = sc.nextInt();       char grade;       if(average>=80){          grade = 'A';       }else if(average>=60 && average=40 && average

Java program to generate a calculator using the switch case

Chandu yadav
Updated on 21-Jun-2024 11:32:02

9K+ Views

The following program accepts two integer variables, takes an operator regarding the operation. According to the selected operator, the program performs the respective operation and print the result.Exampleimport java.util.Scanner; public class ab39_CalculatorUsingSwitch {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter value of 1st number ::");       int a = sc.nextInt();       System.out.println("Enter value of 2nd number ::");       int b = sc.nextInt();       System.out.println("Select operation");       System.out.println("Addition-a: Subtraction-s: Multiplication-m: Division-d: ");       char ch ... Read More

Java program to reverse an array

Samual Sam
Updated on 30-Jul-2019 22:30:22

639 Views

Stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc.A stack is first in first out, it has two main operations push and pop. Push inserts data into it and pop retrieves data from it.To reverse an array using stack initially push all elements into the stack using the push() method then, retrieve them back using the pop() method into another array.Exampleimport java.util.Arrays; import java.util.Stack; public class ab38_ReverseOfArray { public ... Read More

Java program to Largest number among three numbers

Lakshmi Srinivas
Updated on 13-Mar-2020 09:53:45

4K+ Views

Comparing three integer variables is one of the simplest programs you can write at ease. Take two integer variables, say A, B& C Assign values to variables If A is greater than B & C, Display A is the largest value If B is greater than A & C, Display B is the largest value If C is greater than A & B, Display A is the largest value Otherwise, Display A, B & C are not unique valuesExampleimport java.util.Scanner; public class LargestOfThreeNumbers {    public static void main(String args[]){       Scanner sc =new Scanner(System.in);       System.out.println("Enter 1st number :");     ... Read More

Advertisements