Found 9150 Articles for Object Oriented Programming

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

Shriansh Kumar
Updated on 30-Jul-2024 16:45:53

32K+ Views

Given a sentence (string), let's say str, count the total number of vowels in it. In Java, the String is a non-primitive data type which is used to define sequence of characters. And, the English letters a, e, i, o, and u are known as vowels. To count the number of vowels in a given sentence, create a variable named count and initialize it with 0. Store the number of occurrences in this variable. Next, compare each character in the sentence with the vowels. If a match occurs increment the count and print it. Counting Number of Vowels In ... Read More

Java program to print the Armstrong numbers between two numbers

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

19K+ 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 08-Aug-2024 17:37:15

10K+ Views

In this article, we will count the number of consonants in a given sentence using Java to achieve this, we will use the Scanner class for user input and a for loop to iterate through each character in the sentence. By comparing each character with the vowels a, e, i, o, u, and ignoring spaces, we will identify consonants and increment a counter accordingly. The Scanner class is part of java.util package and is commonly used to obtain input from the user. Problem Statement Given a sentence, write a Java program to count the number of consonants. As given below ... Read More

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

Shriansh Kumar
Updated on 05-Aug-2024 18:09:20

3K+ Views

For a character "ch", write a Java program to verify whether it lies between a and z (both small and capital). If it does it is an alphabet otherwise, not. Alphabets are the set of letters written to represent particular sounds in a spoken language like English. Example Scenario 1: Input: character = 'e'; Output: Yes given character is an alphabet Example Scenario 2: Input: character = '4'; Output: No given character is not an alphabet Using ASCII Values The term ASCII stands for American Standard Code for Information Interchange. Every English letters has an ASCII value ... Read More

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

Arjun Thakur
Updated on 11-Dec-2024 19:33:48

1K+ Views

In this article, we’ll learn to find the largest of three numbers using the ternary operator in Java. This simple yet effective approach allows you to write concise and readable code. Finding the largest of three numbers is a common programming task, and Java provides an efficient way to do this using the ternary operator. This concise approach reduces code complexity while maintaining readability. What is the Ternary Operator in Java?  The ternary operator is a shorthand for if-else statements in Java. It uses the syntax: condition ? value_if_true : value_if_false This operator is useful for simple conditional checks and ... Read More

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

Shriansh Kumar
Updated on 11-Sep-2024 11:02:27

2K+ Views

To find the smallest from three given numbers using ternary operator, create a temporary variable to store the result of first comparison operation between two numbers. Then, perform second comparison operation between the result of first operation (i.e. temp) and third number to get the final result. Let's understand the problem statement with an example − Example Scenario: Input: nums = 20, 35, 10; Output: res = 10 What is Ternary Operator? The conditional operator in Java is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal ... Read More

Java program to calculate student grades

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

17K+ 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 reverse an array

Alshifa Hasnain
Updated on 20-Jan-2025 19:19:59

1K+ Views

In this article, we will learn to reverse an array in Java. Reversing an array is a classic problem that helps understand essential concepts like data structures and in-place manipulation. This operation is commonly required in programming tasks, such as data transformations, algorithm implementations, and solving logical problems. Problem Statement Reversing an array involves rearranging its elements so that the first element becomes the last, the second becomes the second-to-last, and so on. Input int[] myArray = {23, 93, 56, 92, 39}; Output {39, 92, 56, 93, 23} Different Approaches Following are the two different approaches to reverse an array ... Read More

Java program to find whether given character is vowel or consonant using switch case

George John
Updated on 13-Sep-2024 10:21:14

3K+ Views

A switch statement in Java allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. To verify whether given character is a vowel read a character from the user into a variable (say ch). Problem Statement Given a character, write a Java program to determine whether it is a vowel or a consonant using a switch statement. Input Enter a character : a Output Given character is an vowel Steps to check whether given character is vowel or ... 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