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...