The post Default value Of int In Java appeared first on Java Vogue.

]]>**Note : The Default value of int in java is 0.**

We check** int default value java** in this example. We create a unassigned variable(defaultInt) , then print it’s value .

```
public class DefaultIntValue {
int defaultInt;
public static void main(String[] args) {
DefaultIntValue defaultIntValue = new DefaultIntValue();
defaultIntValue.printIntDefaultValues();
}
public void printIntDefaultValues() {
// print default value of int
System.out.println("Default value of boolean: " + defaultInt);
}
}
```

Output :

`Default value of boolean: 0`

This examples clears that default value of int in java is 0 .

Let’s be sure about it . We will create few more unassigned int variables and print value of these unassigned int variables .

```
public class DefaultIntValue2 {
int defaultInt1;
int defaultInt2;
public static void main(String[] args) {
DefaultIntValue2 defaultIntValue2 = new DefaultIntValue2();
defaultIntValue2.printIntDefaultValues();
}
public void printIntDefaultValues() {
//print default value of int
System.out.println("Default value of int: " + defaultInt1);
System.out.println("Default value of int: " + defaultInt2);
}
}
```

Output :

```
Default value of int: 0
Default value of int: 0
```

Now it is verify that** int default value java is 0** .

In this tutorial we learned java int default value is zero(0). You can see more java program for practice .

The post Default value Of int In Java appeared first on Java Vogue.

]]>The post Java boolean default value appeared first on Java Vogue.

]]>**Note : The Default value of boolean is false and wrapper class Boolean is null.**

Now we will write a program to check default boolean value in java.

```
public class DefaultBooleanValue {
boolean defaultBoolean;
Boolean defaultBooleanWrapper;
public static void main(String[] args) {
DefaultBooleanValue defaultBooleanValue = new DefaultBooleanValue();
defaultBooleanValue.printBooleanDefaultValues();
}
public void printBooleanDefaultValues() {
//print value of boolean
System.out.println("Default value of boolean: " + defaultBoolean);
//print value of Boolean
System.out.println("Default value of Boolean(Wrapper): "
+ defaultBooleanWrapper);
}
}
```

Output :

```
Default value of boolean: false
Default value of Boolean(Wrapper): null
```

The output of this example makes clear that **The Default value of boolean is false and wrapper class Boolean is null** .

Lets be sure about it. We will now create few more boolean and Boolean variables and we will print java boolean default value and see the output.

```
public class DefaultBooleanValue2 {
boolean defaultBoolean1;
Boolean defaultBooleanWrapper1;
boolean defaultBoolean2;
Boolean defaultBooleanWrapper2;
public static void main(String[] args) {
DefaultBooleanValue2 jbd = new DefaultBooleanValue2();
jbd.printBooleanDefaultValues();
}
public void printBooleanDefaultValues() {
//print value of boolean
System.out.println("Default value of boolean: " + defaultBoolean1);
System.out.println("Default value of boolean: " + defaultBoolean2);
//print value of Boolean
System.out.println("Default value of Boolean(Wrapper): "
+ defaultBooleanWrapper1);
System.out.println("Default value of Boolean(Wrapper): "
+ defaultBooleanWrapper2);
}
}
```

Output :

```
Default value of boolean: false
Default value of boolean: false
Default value of Boolean(Wrapper): null
Default value of Boolean(Wrapper): null
```

Now it is sure that The Default value of boolean is false and wrapper class Boolean is null .

In this tutorial we learned about default value of boolean in java . We have written two program to check default boolean value java . You can learn more interesting java program for practice .

The post Java boolean default value appeared first on Java Vogue.

]]>The post How to Square a number In java appeared first on Java Vogue.

]]>In mathematics or algebra, you can calculate the “square” of a number, by multiplying the same number with itself.

Example : the square of 2 is (2*2=4)4, and the square of 3 is (3*3=9)9.

Now we can square in java using these below methods .

- Square a number by multiplying it by itself
- Square a number using Math.pow function

We can find square in java multiply number by itself . It is very simple method for squaring in java for any number .

```
public class SquareNumber {
public static void main(String[] args) {
int num = 2;
int square = num * num ;
System.out.println("square of num :"+ num +" is :"+ square );
}
}
```

Output :

`square of num :2 is :4`

We can use Java math.pow() method for squaring in java . In this example of square Math.pow(num, POW) multiply num with itself POW times .

```
public class SquareNumber2 {
public static final Integer POW = 2;
//return square a number
public static Double getSquare(Double num) {
//square a number
return Math.pow(num, POW);
}
public static void main(String[] args) {
double num = 2;
System.out.println("square of num :"+ num +" is :"+ getSquare(num) );
num = 3;
System.out.println("square of num :"+ num +" is :"+ getSquare(num) );
}
}
```

Output :

```
square of num :2.0 is :4.0
square of num :3.0 is :9.0
```

In this tutorial we learned how to square a number in java using multiple methods. squaring in java is frequently ask question in academic exam . So you should try these example of square . You can find more java program for practice .

The post How to Square a number In java appeared first on Java Vogue.

]]>The post Mean Of First n Natural Numbers appeared first on Java Vogue.

]]>Sum = n*(n+1)/2

Now we calculate average of n natural numbers as

average of n natural numbers = sum of n natural number / n

average of n natural numbers=n*(n+1)/2/n

average of n natural numbers = n+1/2

Hence , **mean of first n natural numbers** is = n+1/2

Now we write a java program to find mean of first n natural numbers . In this program we take input from user then calculate mean of given n natural numbers as .

```
import java.util.Scanner;
public class MeansFirstNaturalNumber {
public static void main(String[] args) {
double n ;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first nutural number value:");
n = sc.nextFloat();
//calculate average of n natural number
double average = (n+1)/2;
System.out.println("Means of first natural number "+ n+" is "+ average);
sc.close();
}
}
```

Output:

```
Enter the first nutural number value:
50
Means of first natural number 50.0 is 25.5
```

In this tutorial we have learned how to calculate mean of first n natural numbers and then we have written java program to find mean of first n natural numbers . You can learn more java program for coding .

The post Mean Of First n Natural Numbers appeared first on Java Vogue.

]]>The post Swapping Of Two Numbers In Java appeared first on Java Vogue.

]]>First we will write **java program to swap two numbers **using third variable . In this java program we use a third variable to swap two numbers . First we take input from user for variable i & j then with the help of third variable t we will swap these two numbers i & j.

```
import java.util.Scanner;
public class SwapTwoNumberUsingVariable {
public static void main(String[] args) {
int i, j, t;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of i :");
i = sc.nextInt();
System.out.println("Enter the value of j :");
j = sc.nextInt();
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
t = i;
i = j;
j = t;
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
sc.close();
}
}
```

Output :

```
Enter the value of i :
10
Enter the value of j :
20
Before swapping numbers i=10, j=20
Before swapping numbers i=20, j=10
```

We will **write a program to swap two numbers in java **using Addition and Subtraction Operator . In this java program we will **swap of two numbers without using third variable in java . **We swap two numbers using Addition and Subtraction Operator by below logic

i= i -j;

j = i+j;

i = j-i;

Let’s write code this

```
import java.util.Scanner;
public class SwapTwoNumberUsingAdd {
public static void main(String[] args) {
int i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of i :");
i = sc.nextInt();
System.out.println("Enter the value of j :");
j = sc.nextInt();
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
i= i -j;
j = i+j;
i = j-i;
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
sc.close();
}
}
```

Output:

```
Enter the value of i :
10
Enter the value of j :
20
Before swapping numbers i=10, j=20
Before swapping numbers i=20, j=10
```

This example also for **swap of two numbers without using third variable in java . **In this example we take input from user and then program to swap two numbers using Multiplication and Division Operator as below .

```
import java.util.Scanner;
public class SwapTwoNumberUsingmul2 {
public static void main(String[] args) {
int i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of i :");
i = sc.nextInt();
System.out.println("Enter the value of j :");
j = sc.nextInt();
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
//swapping two number in one line
i = i*j /(j=i);
System.out.println("Before swapping numbers i=" + i + ", j=" + j);
sc.close();
}
}
```

Output :

```
Enter the value of i :
10
Enter the value of j :
20
Before swapping numbers i=10, j=20
Before swapping numbers i=20, j=10
```

Now write a program to swap two numbers using XOR . In this example we will take input from user ans then swap numbers using XOR. In this we swap of two numbers without using third variable in java . Let’s see example swapping of two numbers in java using xor .

```
import java.util.Scanner;
public class SwapTwoNumberUsingXOR {
public static void main(String[] args) {
int i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of i :");
i = sc.nextInt();
System.out.println("Enter the value of j :");
j = sc.nextInt();
System.out.println("Before swap i=" + i + ", j=" + j);
i = i ^ j;
j = i ^ j;
i = i ^ j;
System.out.println("After Swap i=" + i + ", j=" + j);
sc.close();
}
}
```

Output :

```
Enter the value of i :
10
Enter the value of j :
20
Before swap i=10, j=20
After Swap i=20, j=10
```

We have seen how to swap two numbers in java using different method. In this tutorial we learned swapping of two numbers using third variable and also swap of two numbers without using third variable in java . you can learn more java program for practice .

The post Swapping Of Two Numbers In Java appeared first on Java Vogue.

]]>The post How To Find The Average Of Numbers In Java appeared first on Java Vogue.

]]>The average is the outcome from the sum of the given numbers divided by the count of the numbers being averaged.

` average = sum of all number / count of number`

Example : 1,2,3,5,9

count of number : 5

sum of all number = 20

then average = 20/5=4

Now we will write java program to find the average of numbers

- Find the average of numbers entered by user
- Program to calculate average of numbers using array

We calculate **average of n natural numbers** in this java program . We take input from user count of number then read number using scanner and store in array . First we calculate sum of numbers and to find average we divided sum of number by count of number .

```
import java.util.Scanner;
public class AverageExample1 {
public static void main(String[] args) {
System.out.println("Enter count of numbers");
Scanner scanner = new Scanner(System.in);
/*
* take input for count from user
*/
int count = scanner.nextInt();
double[] arr = new double[count];
double sum = 0;
/*
* take element value from user
*/
for (int i = 0; i < arr.length; i++) {
System.out.print("Enter Value of Element " + (i + 1) + ":");
arr[i] = scanner.nextDouble();
}
scanner.close();
/*
* calculate sum of element
*/
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
/*
* calculate average of element
*/
double average = sum / arr.length;
/*
* print the average of numbers
*/
System.out.println("The average is:" + average);
}
}
```

Output:

```
Enter count of numbers
5
Enter Value of Element 1:10
Enter Value of Element 2:20
Enter Value of Element 3:30
Enter Value of Element 4:45
Enter Value of Element 5:60
The average is:33.0
```

In this program we will find the average of numbers of given array . we calculate sum of element of array then to get average , we divided sum by length of array .

```
public class AverageExample2 {
public static void main(String[] args) {
double[] arr = {10,20,30.5,40,25};
double sum = 0;
/*
*calculate sum of element of array
*/
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
//calculate average of array
double average = sum / arr.length;
//print average if array
System.out.println("The average is:" + average);
}
}
```

Output :

`The average is:25.1`

In this tutorial we have learned how to find the average of numbers using array . We calculated average of number enter by user also . You can learn more java program for practice .

The post How To Find The Average Of Numbers In Java appeared first on Java Vogue.

]]>The post Print First 10 Prime Numbers in Java appeared first on Java Vogue.

]]>Example 1 : **is 21 a prime number?**

21 is divided by 1, 3,7 ,21 . Because 21 is divided by 3 and 7 also , so 21 is not a prime number .

Example 2 : **is 37 a prime number ?**

37 is divided by 1 and 37 only , So 37 is a prime number .

Now we will print first ten prime number using java program . In this program we take variable num and count . We iterate a while loop till print **first ten prime numbers . **variable num increment by 1 in each iterate and count increment by 1 when find any number is prime number .

Program description: **Program to print the first 10 prime numbers**

```
public class FirstTenPrimeNumber {
public static void main(String[] args) {
int num =1 ,count =0;
while ( count < 10) {
if (isPrimeNumber(num)) {
System.out.println( num +" is prime number");
count++;
}
num++;
}
}
private static boolean isPrimeNumber(int num) {
if (num <= 1) {
return false;
}
boolean flag = true;
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0) {
flag = false;
break;
}
}
return flag;
}
}
```

Output :

```
2 is prime number
3 is prime number
5 is prime number
7 is prime number
11 is prime number
13 is prime number
17 is prime number
19 is prime number
23 is prime number
29 is prime number
```

In this tutorial we learned what is prime numbers , prime number examples and how to find first ten prime numbers . You can learn more java program for interview preparation .

The post Print First 10 Prime Numbers in Java appeared first on Java Vogue.

]]>The post How to Determine if three lengths form a Triangle appeared first on Java Vogue.

]]>Let’s A , B ,C are three sides of triangle then it form a triangle if

- A+B > C
- B+C > A
- C+A > B

We write java program to check if a triangle is valid or not when sides are given .Now we will determine if three lengths form a triangle using java program .

```
import java.util.Scanner;
public class TriangleValid {
public static void main(String[] args) {
int side1 , side2, side3;
//create scanner object to get input from user
Scanner sc=new Scanner(System.in);
System.out.println("Enter side1 ");
//take input from user
side1 = sc.nextInt();
System.out.println("Enter side2 ");
//take input from user
side2 = sc.nextInt();
System.out.println("Enter side3 ");
//take input from user
side3 = sc.nextInt();
if( (side1 + side2 > side3) && (side2 + side3 > side1) && (side1 + side3 > side2) ){
System.out.println("This is valid triangle");
}else {
System.out.println("This is not valid triangle");
}
}
}
```

Output:

```
Enter side1
10
Enter side2
5
Enter side3
7
This is valid triangle
```

In this tutorial we learned how to determine if three lengths form a triangle using java .You can learn more java program for practice .

The post How to Determine if three lengths form a Triangle appeared first on Java Vogue.

]]>The post Duck Number In Java appeared first on Java Vogue.

]]>A **Duck number** is a number which has zeroes present in it, but there should be no zero present in the beginning of the number. In other words , A **duck number** is a positive non-zero number which has zero present in it. The zero is present at any position of that number except at the starting of the number .

**Example of duck number :**

- 2210 is a Duck number because it contains zero , but not at the beginning .
- 0253 is not a Duck number because it contains zero at the beginning of number.
- 030201 is not a Duck number because it contains zero in beginning of the number.
- 00531 is also not a Duck number because it contains leading zeros.

- Take number variable (num) .
- check last digit of given number(num) is zero then return true .
- Remove the last digit from number(num).
- repeat step 2 & 3 until number(num) become zero.

We will implement above algorithm to find duck number in java** . **

```
package com.demo.program;
import java.util.Scanner;
public class DuckNumber {
//check given number is duck or not
public static boolean isDuckNumber(int num) {
//iterate loop until num become zero
while(num != 0) {
if(num%10 == 0)
return true; //return true if the number is Duck
// divide the number by 10 to remove the last digit
num = num / 10;
}
return false; //return false if the number is not Duck
}
public static void main(String args[])
{
int num;
//create scanner object to get input from user
Scanner sc=new Scanner(System.in);
System.out.println("Enter number");
//take input from user
num = sc.nextInt();
if (isDuckNumber(num))
System.out.println("given number "+ num + " is a Duck number");
else
System.out.println("given number "+ num + " is not a Duck number");
//close scanner object
sc.close();
}
}
```

Output:

```
Enter number
2304
given number 2304 is a Duck number
```

We will find duck number in java in given range . We take input of min range and max range from user and print all duck number in range .

```
package com.demo.program;
import java.util.Scanner;
public class DuckNumberInRange {
//check given number is duck or not
public static boolean isDuckNumber(int num) {
//iterate loop until num become zero
while(num != 0) {
if(num%10 == 0)
return true; //return true if the number is Duck
// divide the number by 10 to remove the last digit
num = num / 10;
}
return false; //return false if the number is not Duck
}
public static void main(String args[])
{
int minRange , maxRange;
//create scanner object to get input from user
Scanner sc=new Scanner(System.in);
System.out.println("Enter Min range");
//take user input
minRange = sc.nextInt();
System.out.println("Enter Max range");
//take user input
maxRange = sc.nextInt();
for (int i = minRange; i <= maxRange; i++) {
if (isDuckNumber(i))
System.out.println( i + " is a duck number");
}
//close scanner object
sc.close();
}
}
```

Output:

```
Enter Min range
0
Enter Max range
50
10 is a duck number
20 is a duck number
30 is a duck number
40 is a duck number
50 is a duck number
```

We learned in this tutorial what is duck number in java , how to find duck number in java and print all duck number in range . You can see more java coding program for practice .

The post Duck Number In Java appeared first on Java Vogue.

]]>The post Kaprekar Number Program in Java appeared first on Java Vogue.

]]>A number whose square divided into two parts (none of the parts have only 0) and the sum of the parts is equal to the original number(given number) then it is called **kaprekar number**. The numbers are named after D. R. Kaprekar.

**Example of kaprekar numbers**:- 45

The square of 45 = 2025

20 + 25 = 45, so 45 is a kaprekar number.

10, 100, 1000 are not a kaprekar number

10^2 = 100 => 10 + 0= 10

100^2 = 10000 = 100 + 00 = 100

But the condition is none of the part having only 0, so these are not the **kaprekar number**.

First we will write algorithm to calculate kaprekar number . Let’s discuss algorithms step by step

- Take input from user an store in variable (number).
- Calculate square of number and store in variable square .
- Count digit of square and store in countDigits .
- Now iterate i =countDigits-1 to i > 0
- Calculate first part by square divide by 10^i
- Calculate second part is remainder value , when square divide by 10^i .
- If any part is 0 then go next iteration.
- Calculate sum of both part
- If sum is equal to given number then given number is
**kaprekar number**and return true .

We implement this algorithm and write program to find kaprekar number in Java.

```
import java.util.Scanner;
public class KaprekarNumber {
//check given number is kaprekar or not
public static boolean isKaprekar(int number) {
int square = 0;
int temp = 0;
int countDigits = 0;
int firstPart = 0;
int secondPart = 0;
int sum = 0;
// calculate square root of the number
square = number * number;
// count number of digits in the square of given number
temp =square;
while(temp!=0) {
countDigits++;
temp /= 10;
}
// divide square into two parts and
// check it's sum is equal to the number or not
for(int i=countDigits-1; i>0; i--) {
// find first part of square
firstPart = square / (int)Math.pow(10, i);
// find second part of square
secondPart = square % (int)Math.pow(10, i);
// check have any part only 0
if(firstPart == 0 || secondPart == 0)
continue;
// find sum value
sum = firstPart + secondPart;
// compare sum and number
if( sum == number )
return true;
}
return false;
}
public static void main(String[] args) {
// declare variables
int number = 0;
// read the input from user
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number : ");
number = scan.nextInt();
// check the given number is kaprekar number or not
if(isKaprekar(number))
System.out.println("Given "+ number+" is a"
+ " kaprekar number");
else
System.out.println("Given "+number+" is not a"
+ " kaprekar number");
// close Scanner class object
scan.close();
}
}
```

Output :

```
Enter a number : 45
Given 45 is a kaprekar number
```

Now we write program to find all kaprekar number in java in between a given range . We take input of min and max range from user and then calculate kaprekar number in given range .

```
import java.util.Scanner;
public class KaprekarNumberInRange {
//check given number is kaprekar or not
public static boolean isKaprekar(int number) {
int square = 0;
int temp = 0;
int countDigits = 0;
int firstPart = 0;
int secondPart = 0;
int sum = 0;
// calculate square root of the number
square = number * number;
// count number of digits in the square of given number
temp =square;
while(temp!=0) {
countDigits++;
temp /= 10;
}
// divide square into two parts and
// check it's sum is equal to the number or not
for(int i=countDigits-1; i>0; i--) {
// find first part of square
firstPart = square / (int)Math.pow(10, i);
// find second part of square
secondPart = square % (int)Math.pow(10, i);
// check have any part only 0
if(firstPart == 0 || secondPart == 0)
continue;
// find sum value
sum = firstPart + secondPart;
// compare sum and number
if( sum == number )
return true;
}
return false;
}
public static void main(String[] args) {
// declare variables for min range and max range
int minRange = 0, maxRange = 0;
// create Scanner class object
Scanner scan = new Scanner(System.in);
// read inputs
System.out.print("Enter min value of range : ");
minRange = scan.nextInt();
System.out.print("Enter max value of range : ");
maxRange = scan.nextInt();
// check number is kaprekar number or not
System.out.println("The kaprekar numbers from "+
minRange+" to "+ maxRange+" are : ");
for(int i=minRange; i<=maxRange; i++) {
if(isKaprekar(i))
System.out.print(i+" ");
}
// close Scanner class object
scan.close();
}
}
```

Output :

```
Enter min value of range : 1
Enter max value of range : 100
The kaprekar numbers from 1 to 100 are:
9 45 55 99
```

Here , we print all Kaprekar numbers between 1 to 100

In this tutorial we learned what is kaprekar number in java and written java program to find kaprekar number. We also written java program to print all kaprekar number in a range .

The post Kaprekar Number Program in Java appeared first on Java Vogue.

]]>