Count the number of co-prime pairs in an array

Program: Count the number of co-prime pairs in an array. (co-prime numbers means Any two numbers whose GCD is 1) Input: The first line contains an integer T, total number of elements. Then follow T elements. Output: Count the number of co-prime pairs in an array. Sample Input and Output: Input 1: 3 1 2 3 Output 1: 3 Here, Co-prime pairs are (1, 2), (2, 3), (1,...

Type Conversion and Casting

Type Conversion and Casting: If you have a experience in a programming language, then you know this thing that it is common to assign a value of one type of variable to another type variable. Java performs type conversion automatically when the two types are compatible. For example, it is always possible to assign a int to a long. But not all types are compatible, therefor all types...

Data Types In Java

In a Java there are eight different primitive types of data: byte, short, int, long, char, float, double, and boolean. This types are also referred to as a simple data types and these types are grouped into four different types: Integers: In this group byte, short, int and long are included, which are for whole-valued signed numbers.Floating-point : In this group includes float and double, which is represents...

HOW TO GET DISTINCT ELEMENTS FROM AN ARRAY BY AVOIDING DUPLICATE ELEMENTS?

Here we explain how to find distinct elements from an array by avoiding duplicate elements. In the below program we use array of integers int num[], in this array we found distinct elements. For finding the distinct elements we use the following two ways. Program: Find distinct elements from an array public class MyDisticntElements { public static void printDistinctElements(int[] arr) { for(int i=0;i<arr.length;i++) { boolean isDistinct = false;...

PROGRAM TO ADD TWO NUMBERS WITHOUT USING PLUS (‘+’) SIGN

In our daily life we add some numbers and for this addition we use "+" sign. I have a one question, Can it be possible without using plus sign we add numbers? the answer is Yes! we can add two numbers without using the plus ("+") sign. In the below program we use two different methods for addition of two numbers. First method add() in which we add...

Java String Methods with Examples

Java string methods with examples: There are different string methods in java we will use this methods for perform various operations on the string in java.Following are some Java String Methods with Examples. Program: Get character from a string In this program we use charAt() method to get or access the single character from a specified position. class StringExample { public static void main(String[] args) { String str...

Basic operations on String in Java- Strings Concatenation, Compare

Following programs are for performing the strings basic operations in java like string concatenation, string compare. To perform the string concatenation, string compare in java uses the inbuilt methods in java. String Length: Program: Find the length of string Here we first create the string named str and use the length() method to get the size of the String. class StringExample { public static void main(String[] args) {...

Reverse a String

How to reverse the string? We will use the following different solutions for reversing the string. First we get a input from user using command line. In the first solution of this problem we buffer the input string using the StringBuffer(String string) method. Then reverse this buffer and after reversing convert the buffer into string. Program: public class StringReverse { public static void main(String[] args) { String str...

Linear Search

Linear Search is a used for finding the key element in the multiple of elements in a given array. Now a days, Linear search is not that much used because Linear search algorithm is slow as compare to binary search or hashing. Algorithm: First step is traverse the arrayNext is match the search element with the array elements.If the search element is found then return the index position...

Selection Sort

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The smallest element is selected from the unsorted array and swapped with theleftmost element, and...

Back to Top