Found 338 Articles for Java Programming

Java program to print a multiplication table for any number

Arjun Thakur
Updated on 30-Jul-2019 22:30:22

1K+ Views

Following is a Java program which accepts an integer variable from user and prints the multiplication table of that particular integer.Exampleimport java.util.Scanner; public class MultiplicationTable { public static void main(String args[]) { System.out.println("Enter an integer variable :: "); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); for(int i=1; i

Java program to find the LCM of two numbers

karthikeya Boyini
Updated on 13-Mar-2020 10:29:51

7K+ Views

L.C.M. or Least Common Multiple of two values is the smallest positive value which the multiple of both values.For example multiples of 3 and 4 are:3 → 3, 6, 9, 12, 15 ... 4 → 4, 8, 12, 16, 20 ...The smallest multiple of both is 12, hence the LCM of 3 and 4 is 12.AlgorithmInitialize A and B with positive integers. Store maximum of A & B to the max. Check if max is divisible by A and B. If divisible, Display max as LCM. If not divisible then step increase max, go to step 3.Examplepublic class LCMOfTwoNumbers {    public static void ... Read More

Java program to find the GCD or HCF of two numbers

Chandu yadav
Updated on 14-Jun-2024 13:21:32

28K+ Views

An H.C.F or Highest Common Factor, is the largest common factor of two or more values.For example factors of 12 and 16 are −12 → 1, 2, 3, 4, 6, 12 16 → 1, 2, 4, 8, 16The common factors are 1, 2, 4 and the highest common factor is 4.AlgorithmDefine two variables - A, BSet loop from 1 to max of A, BCheck if both are completely divided by same loop number, if yes, store itDisplay the stored number is HCFExample: Using Java for loop import java.util.Scanner; public class GCDOfTwoNumbers {    public static void main(String args[]){   ... Read More

Java program to display English alphabets

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

290 Views

Following is an example to display English alphabets A to Z.ExampleLive Demopublic class DisplayingAtoZ {    public static void main(String args[]){       char ch;       System.out.println("List of alphabets are ::");       for(ch = 'A'; ch

Java program to Count the number of digits in a given integer

George John
Updated on 18-Jun-2024 15:07:07

16K+ Views

Read a number from user. Create an integer (count) initialize it with 0. Divide the number with 10. till the number is 0 and for each turn increment the count.Exampleimport java.util.Scanner; public class CountingDigitsInInteger {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       int count = 0;       System.out.println("Enter a number ::");       int num = sc.nextInt();       while(num!=0){          num = num/10;          count++;       }       System.out.println("Number of digits in the entered integer are :: "+count);    } }OutputEnter a number :: 1254566 Number of digits in the entered integer are :: 7

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

Advertisements