Posts

Showing posts from December, 2021

Java Program to Count Number of Digits in an Integer - The Coding Shala

Home >> Java Programs >> Count Number of Digits in an Integer  In this post, we will learn how to count the number of digits in an integer using a Java program. Java Program to Count Number of Digits in an Integer You have given an integer, return the number of digits in that using Java program. The given number is above 0. Example 1: Input: 34 Output: 2 Example 2: Input: 01 Output: 1 Approach When we divide a number by 10, we get one digit or we remove one digit from a given number so here we will be using a loop and divide the given number by 10 until it becomes 0. Example:  The number is 23, so after first iteration  number = 23/10 = 2, and count = 1 second iteration => number = 2/10 = 0, count = 2. Java Program:  import java.util.Scanner ; /** * https://www.thecodingshala.com/ */ public class Main { public static void printDigits ( int num ) { System . out . print ( "Number of Digits in " + num + " are: &q

Java Program to Print Alphabets (A-Z and a-z) - The Coding Shala

Home >> Java Programs >> Print Alphabets  In this post, we will learn how to display alphabets using the Java program. Java Program to Print Alphabets Write a Java program to print the alphabets (A-Z and a-z). We can use a for loop or while loop to print the alphabets. Java Program:  /** * https://www.thecodingshala.com/ */ public class Main { public static void main ( String [] args ) { System . out . println ( "A-Z alphabets are: " ); for ( char ch = 'A' ; ch <= 'Z' ; ch ++) { System . out . print ( ch + " " ); } System . out . println (); System . out . println ( "a-z alphabets are: " ); for ( char ch = 'a' ; ch <= 'z' ; ch ++) { System . out . print ( ch + " " ); } } } Output:  A - Z alphabets are: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Java Program to Find the Frequency of Characters in a String - The Coding Shala

Home >> Java Programs >> Find the frequency of characters in a string  In this post, we will learn how to write a Java program to find the frequency of characters in a string. Java Program to Find the Frequency of Characters in a String Write a Java Program to find the frequency of characters in a string. The given string contains a-z characters.  Example 1: Input: "hello" output: e - 1                 h - 1                 l - 2                 o - 1 Approach 1 The given string contains only a-z characters that mean a maximum of 26 characters are there. We will create an integer array of size 26 and will store the count of each character in this array.  The indexes of the count array will represent the characters. For example, index 0 will represent a, 1 as b, and so on. To find the indexes from characters will follow the below steps: For example char = 'b', ASCII value of 'b' is 98. If we convert 'b' into int then we will get 98 a

Java Program to Print Array Elements Present at Even Positions - The Coding Shala

Home >> Java Programs >> Print Array Elements Present at Even Positions  In this post, we will learn how to write a Java program to print the array elements that are available at even positions. Java Program to Print Array Elements Present at Even Positions Write a Java program to print array elements that are available at even indexes. Positions are starting from 0 in the array. Example 1: Input: [1, 2, 3, 4, 5] Output: 1, 3, 5 Approach: We will start the for/while loop from index 0 and move the pointer by 2 indexes.  Java Program:  /** * https://www.thecodingshala.com/ */ public class Main { public static void printEvenIndexElements ( int [] arr ) { for ( int i = 0 ; i < arr . length ; i = i + 2 ) { System . out . println ( arr [ i ]); } } public static void main ( String [] args ) { int [] arr = { 1 , 4 , 2 , 5 , 2 , 6 , 8 }; printEvenIndexElements ( arr );

Java Program to Check if given Number is Positive or Negative - The Coding Shala

Home >> Java Programs >> Check if the given number is positive or negative  In this post, we will learn How to write a Java program to check if the given number is positive or negative. Java Program to Check if given Number is Positive or Negative Write a Java program to check if the given number is positive or negative. Example 1: Input: 22 Output: 22 is a positive number Approach: If the given number is smaller than 0 then it's negative otherwise it is positive, using if condition we will check that. Java Program:  /** * https://www.thecodingshala.com/ */ import java.util.Scanner ; public class Main { public static void checkNum ( int num ) { if ( num < 0 ) { System . out . println ( num + " is a negative number" ); } else { System . out . println ( num + " is a positive number" ); } } public static void main ( String [] args ) {

Java Program to Print Prime Numbers between 1 to N - The Coding Shala

Home >> Java Programs >> Print Prime Numbers between 1 to N  In this post, we will learn how to display prime numbers between 1 to n using the Java program. Java Program to Print Prime Numbers between 1 to N Write a Java Program to print prime numbers between 1 to given n. Example 1: Input: 10 Output: 2                 3                5                7 Approach:  We are going to check every number from 1 to n, if it is prime then will print it. Prime numbers are only divisible by 1 and the number itself, so if the given number is divisible by any other numbers from 2 to n-1 then it's not a prime number. Java Program:  /** * https://www.thecodingshala.com/ */ import java.util.Scanner ; public class Main { public static boolean checkPrime ( int num ) { if ( num < 2 ) return false ; for ( int i = 2 ; i * i <= num ; i ++) { if ( num % i == 0 ) return false ; } return

Java Program to Print Odd Numbers from 1 to N - The Coding Shala

Home >> Java Programs >> Java Program to Print Odd Numbers from 1 to N  In this post, we will learn How to Display Odd Numbers from 1 to N using Java Program. Java Program to Print Odd Numbers from 1 to N Write a Java Program to print odd numbers from 1 to given n.  Example 1: Input: 5 Output: 1              3              5 Approach: We can use a for loop from 1 to given n and will check if the current number is divisible by 2 then it is an even number else it is an odd number. We will print all the odd numbers. We can also start the for loop from 1 and increase it by 2 and print all the numbers till n. Java Program:  /** * https://www.thecodingshala.com/ */ import java.util.Scanner ; public class Main { public static void printOddNumbers ( int n ) { for ( int i = 1 ; i <= n ; i ++) { // if number is divisible by 2 then its even else odd if ( i % 2 != 0 ) { System . out . pri

Java Program to Print Even Numbers from 1 to N - The Coding Shala

Home >> Java Programs >> Java Program to Print Even Numbers from 1 to N  In this post, we will learn how to Display Even Numbers from 1 to n using Java Program. Java Program to Print Even Numbers from 1 to N Write a Java Program to display even numbers from 1 to given n. We can use Java loops here. Example 1: Input: 5 Output: 2              4 Approach: We can use a for loop from 1 to n and will check if the current number is divisible by 2, if it is divisible by 2 then it's an even number else not. We can also start from 2 and increase the current number in the loop by 2 and print all the numbers till n. Java Program:  /** * https://www.thecodingshala.com/ */ import java.util.Scanner ; public class Main { public static void printEvenNumbers ( int n ) { for ( int i = 1 ; i <= n ; i ++) { // if number is divisible by 2 then its even if ( i % 2 == 0 ) { System . out . println

Java Program to Find the Minimum Element in the Array - The Coding Shala

Home >> Java Programs >> Find the Minimum Element in the Array  In this post, we will learn how to write a Java Program to Find the Minimum Element in the given Array. Java Program to Find the Minimum Element in the Array Write a Java Program to find the minimum element in the given array. The elements can be positive or negative in the array. Example 1: Input: [1, 2, 4, 0, -3, -33, 55, 33] Output: -33 Java Program:   /** * https://www.thecodingshala.com/ */ public class Main { public static void printMinimum ( int [] arr ) { if ( arr . length < 1 ) { System . out . println ( "Given array is empty" ); } else { int max = arr [ 0 ]; for ( int i = 1 ; i < arr . length ; i ++) { if ( arr [ i ] < max ) { max = arr [ i ]; } } System . out . println ( "Minimum element in t