Posts

Showing posts from May, 2021

Java Program to Check Prime Number - The Coding Shala

Home >> Java Programs >> Check Prime Number  In this post, we will learn how to Check if a Given Number is a Prime Number or not in Java. Java Program to Check Prime Number A Prime Number is a number that is divisible by 1 and the number itself. For example, the number 5 is a Prime Number because it is divisible by 1 and 5 only.  Note: Number 0 and 1 are not prime numbers. Example 1: Input: 7 Output: Prime Number Example 2: Input: 9 Output: Not a Prime Number Java Program:  // Java program to Check Prime Number import java.util.Scanner ; public class Main { static boolean isPrime ( int num ) { if ( num < 2 ) return false ; // basic for loop ==> for(int i = 2; i < num / 2; i++ ) for ( int i = 2 ; i * i <= num ; i ++) { if ( num % i == 0 ) return false ; } return true ; } public static void main ( String [] args ) { Scanner sc = new Scanner ( System . in ); ...

Java Program to Check if the Given Character is Vowel or Consonant - The Coding Shala

Home >> Java Programs >> Check Character is Vowel or Consonant  In this post, we will learn how to Check Whether the Character is Vowel or Consonant in Java. Java Program to Check if the Given Character is Vowel or Consonant As we know, Vowels are ('a', 'e', 'i', 'o', 'u'), and all the other characters are consonant. Write a Java Program to check if the given character is vowel or consonant. Example 1: Input: e Output: Vowel Java Program:  // Java program to Check Vowel or Consonant import java.util.Scanner ; public class Main { public static void main ( String [] args ) { Scanner sc = new Scanner ( System . in ); // take input char System . out . println ( "Enter the Character between a to z" ); // next will take input as string so charAt(0) used char ch = sc . next (). charAt ( 0 ); if ( ch == 'a' || ch == 'e' || ch == 'i' |...

Java Program to Calculate Simple Interest - The Coding Shala

Home >> Java Programs >> Calculate Simple Interest  In this post, we will learn how to Calculate Simple Interests in Java. Java Program to Calculate Simple Interest You have given the principal amount, rate of interest, and time. Write a Java Program to calculate the interest amount. We will use the formula of simple Interest. Simple Interest = (P * R * T) / 100 Here, P = Principle Amount           R = Rate of Interest           T = Time Java Program:  // Java program to Calculate Simple Interest public class Main { public static void main ( String [] args ) { float P = 2000 ; float R = 6 ; float T = 3 ; // calculate interest using formula float interest = ( P * R * T ) / 100 ; // print System . out . println ( "Principal amount is: " + P ); System . out . println ( "Interest Rate is: " + R ); System . out . println ( "Time is: " ...

Java Program to Check Armstrong Number - The Coding Shala

Home >> Java Programs >> Armstrong Number  In this post, we will learn how to check if a given number is Armstrong Number in Java. Java Program to Check Armstrong Number A positive number is called Armstrong Number if the following equation is true: ab...z = a^n + b^n + .... + z^n Here, n is the number of digits in the number. For example, let's take 3 digits Armstrong Number.   Example 1: Input: 153 Output: Armstrong Number Explanation:          153 = 1*1*1 + 5*5*5 + 3*3*3               = 1 + 125 + 27              = 153 Java Program:  // Java program to Check Armstrong Number public class Main { public static void main ( String [] args ) { // input number to check Armstrong int number = 370 ; int sum = 0 , temp = number ; while ( temp != 0 ) { int digit = temp % 10 ; temp = temp / 10 ; ...

Java Program to Reverse a String using Recursion - The Coding Shala

Home >> Java Programs >> Reverse a String using Recursion  In this post, we will learn how to Reverse a String in Java using Recursion. Java Program to Reverse a String using Recursion Given a string S as input. You have to reverse the given string using Recursion. Example 1: Input: Akshay Output: yahskA Java Program:  // Java program to Reverse a String using Recursion public class Main { public static String reverseIt ( String str ) { if ( str . length () == 0 ) { return "" ; } return reverseIt ( str . substring ( 1 )) + str . charAt ( 0 ); } public static void main ( String [] args ) { String str = "Akshay Saini" ; String reverseStr = reverseIt ( str ); System . out . println ( "Reverse String is: " + reverseStr ); } } Other Posts You May Like How to Reverse a String in Java Java Program to Find Duplicate characters count in a String Java Program to C...

Java Program to Calculate the Power of a Number using Recursion - The Coding Shala

Home >> Java Programs >> Power of a Number using Recursion  In this post, we will learn how to find the Power of a Number using Recursion in Java. Java Program to Calculate the Power of a Number using Recursion You have given a number N ( base) and power p (exponent). Write a Java Program to Calculate the power of the number N by using recursion. Example 1: Input: N = 3           P = 4 Output: 81 Java Program:  // Java program to calculate power of a number using Recursion public class Main { public static int findPower ( int base , int exponent ) { if ( exponent == 0 ) { return 1 ; } return base * findPower ( base , exponent - 1 ); } public static void main ( String [] args ) { // inputs can be taken using scanner int base = 3 ; int exponent = 4 ; // using recursion int power = findPower ( base , exponent ); System . out . println ( "powe...

Java Program to Calculate the Power of a Number - The Coding Shala

Home >> Java Programs >> Power of a Number  In this post, we will learn how to Calculate the Power of a Number using the loop and Math.pow() method in Java. Java Program to Calculate the Power of a Number You have given a number N ( base) and power p (exponent). Write a Java Program to Calculate the power of the number N. Example 1: Input: N = 3           P = 4 Output: 81 Approach 1 Using loop. Java Program:  // Java program to calculate power of a number public class Main { public static void main ( String [] args ) { // inputs can be taken using scanner int base = 3 ; int exponent = 4 ; int power = 1 ; // using loop for ( int i = 1 ; i <= exponent ; i ++) { power = power * base ; } System . out . println ( "power(" + base + "," + exponent + ") is: " + power ); } } Approach 2 We can also use Java's inbuilt Math.pow() method to calculate the p...

Java Program to Calculate Distance between Two Points - The Coding Shala

Home >> Java Programs >> Calculate Distance between Two Points  In this post, we will learn how to find Distance between two points using Java Program. Java Program to Calculate Distance between Two Points You have given two points with coordinates (x1, y1) and (x2, y2). Write a Java program to find the distance between these two points. We can find the distance between two points using the distance formula. Distance = sqrt((x2-x1) ^ 2 + (y2-y1) ^ 2) Example 1: Input: (x1,y1) = (3,4)            (x2, y2) = (7, 7) Output: 5 Java Program:  // Java program to find distance between two points import java.util.Scanner ; public class Main { public static void main ( String [] args ) { Scanner sc = new Scanner ( System . in ); int x1 , y1 , x2 , y2 ; // take inputs System . out . println ( "Enter x1: " ); x1 = sc . nextInt (); System . out . println ( "Enter y1: " ); y1 = sc ....

Java Program to find the Sum of N numbers - The Coding Shala

Home >> Java Programs >> Find Sum of N numbers  In this post, we will learn how to find the Sum of N numbers in Java. Java Program to Find the Sum of N numbers Write a Java Program to Find the Sum of N numbers, take all the numbers as input using Scanner, and print the sum of n numbers. Example 1: Input: elements: 1,2,3,4,5 Output: 15 Java Program:  // Java program to find Sum of N numbers import java.util.Scanner ; public class Main { public static void main ( String [] args ) { Scanner sc = new Scanner ( System . in ); System . out . println ( "How many numbers are there?" ); int num = sc . nextInt (); double sum = 0 ; for ( int i = 0 ; i < num ; i ++) { System . out . println ( "Enter " + ( i + 1 ) + " element: " ); int n = sc . nextInt (); sum = sum + n ; } System . out . print ( "Sum of " + num + " numbers is: " +...

Java Program to find the Average of N numbers - The Coding Shala

Home >> Java Programs >> Average of N numbers  In this post, we will learn how to find the Average of N numbers in Java. Java Program to find the Average of N numbers Write a Java program to find the Average of N numbers, take all the n numbers as input. Example 1: Input: number of elements is = 5        elements = 1, 2, 3, 4, 5 Output: 3 Explanation:    avg = (1 + 2 + 3 + 4 + 5) / 5 = 3 Java Program:  // Java program to find Average of N numbers import java.util.Scanner ; public class Main { public static void main ( String [] args ) { Scanner sc = new Scanner ( System . in ); System . out . println ( "How many numbers are there?" ); int num = sc . nextInt (); double sum = 0 ; for ( int i = 0 ; i < num ; i ++) { System . out . println ( "Enter " + ( i + 1 ) + " element: " ); int n = sc . nextInt (); sum = sum + n ; } // find avg d...

How to take input numbers into an Array in Java - The Coding Shala

Home >> Java Programs >> How to take input numbers into an Array  In this post, we will learn how to take input numbers into an Array using the scanner in Java. Java Program to take input numbers into an Array Write a Java Program that takes numbers as input and inserts those elements into an Array. Example 1: Input: number of elements = 5          elements are = 1 2 3 4 5 Output: arr = [1, 2,3,4,5] Java Program:  // Java program to take input numbers into array import java.util.Scanner ; public class Main { public static void main ( String [] args ) { Scanner sc = new Scanner ( System . in ); System . out . println ( "Enter the size of array" ); int num = sc . nextInt (); // declare array int [] arr = new int [ num ]; System . out . println ( "Enter " + num + " numbers" ); for ( int i = 0 ; i < num ; i ++) { arr [ i ] = sc . nextInt (); } ...