• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Java

Quick Sort in Java Example

By Dinesh Thakur

Algorithm for Quick Sort:

Here ‘a’ is an array of elements and ‘first’ and ‘last’ represent the first index and last index of the array ‘a’ Perform the following steps only if(first<last).

step 1: Set i=first

step 2: Set j=last+l

step 3: Set k=first

step 4: Repeat through step-9 while(i<j)

step 5: Repeat through step-6 while(a[i]<a[j])

step 6: Increment the value of ‘i’ as i=i+1

step 7: Repeat through step-8 while(a[J]>a[k])

step 8: Decrement the value of ‘J’ as j=j+1

step 9: If(i<j)then exchange the value of a[i]and a[j]

step 10: Exchange the value of a[k] and a[j]

step 11: Call QuickSort(a,first,j-1)

step 12: Call QuickSort(a,j+ 1,last)

step 13: Exit

Here is the Java Example for Quick Sort:

import java.util.Scanner; 
class QuickSort
{
            static int a[];
            static int n;
          public static void main(String args[])
      {
            Scanner read=new Scanner(System.in);
            System.out.print("Enter Number of Elements You Want to Insert : ");
            n=read.nextInt();
            a=new int[n];
            for(int i=0;i<n;i++)
               {
                 System.out.print("\nEnter no."+(i+1)+" :");
                 a[i]=read.nextInt();
               }
                 QuickSort ii=new QuickSort();
                 ii.QuickSort(a,0,n-1);
                 System.out.print("\nAll Elements are :");
                 for(int i=0;i<n;i++)
                    {
                         System.out.print(a[i]+" ");
                    }
       }
          void QuickSort(int a[],int first,int last)
               {
                   int i,j,k,temp;
                   if(first<last)
                     {
                          i=first;
                          j=last+1;
                          k=first;
                           do
                             {
                               do
                                 {
                                    i++;
                                  }while(a[i]<a[k]);
                                      do
                                       {
                                          j--;
                                        }while(a[j]>a[k]);
                                         if(i <j)
                                           {
                                             temp=a[i];
                                             a[i]=a[j];
                                             a[j]=temp;
                                            }
                             }while(i<j);
                              temp=a[k];
                              a[k]=a[j];
                              a[j] =temp;
                              QuickSort(a,first,j-1);
                              QuickSort(a,j+1,last);
                      }
               }
}

Quick Sort in Java

What is bubble sort in Java with example?

By Dinesh Thakur

In this tutorial, We can create a bubble sort program in java to sort array elements. Bubble Sort Algorithm is called the simplest sorting algorithm. This Bubble Sort in Java tutorial will, therefore, allow you to understand the concept in depth. [Read more…] about What is bubble sort in Java with example?

Removing duplicate elements from Array in Java Example

By Dinesh Thakur

Algorithm for Removing duplicate elements from Array:

step 1: read y

step 2: create two integer array a[],b[] of size y

step 3: initialize c=0

step 4: repeat through step-6 while c less than a.length

step 5: read a

step 6: c=c+1

step 7: initialize c=0

step 8: repeat through step-11 while c less than a.length

step 9: print a

step 10: print one space

step 11: c=c+1

step 12: initialize t=0

step 13: repeat through step-l8while t less than a.length

step 14: initialize x=t+1

step 15: repeat through step-17 while x less than a.length

step 16: if a[t] equals to a[x] then set a[x]=0

step 17: x=x+1

step 18: t=t+ 1

step 19: set y =0

step 20: initialize c=0

step 21: repeat through step-23 while c less than a.length

step 22: If a not equals to 0 then set b[y]=a and set y=y+1

step 23: c=c+1

step 24: Initialize x=0

step 25: repeat through step-28 while x less than y

step 26: print b[x]

step 27: print one space

step 28: x=x+1

step 29: Exit

Here is the Java Example for removing duplicate elements from Array:

import java.util.Scanner; 
public class RemovingDuplicateElements
{
            public static void main(String args[])
      {
            int c,t,x,y;
            Scanner s=new Scanner(System.in);
            System.out.println("Enter the size of array");
            y=s.nextInt();
            int a[]=new int[y];
            int b[]=new int[y];
            System.out.println("Enter The Elements of Array");
            for(c=0;c<a.length;c++)
               {
                 a=s.nextInt();
               }
                 System.out.println("\nSee the elements of array\n");
                 for(c=0;c<a.length;c++)
                    {
                      System.out.print(a+" ");
                    }
                      System.out.print("\n");
                 for(t=0;t<a.length;t++)
             {
                 for(x=t+1;x<a.length;x++)
                    {
                      if(a[t]==a[x])
                       {
                         a[x]=0;
                       }
                    }
                         y=0;
                 for(c=0;c<a.length;c++)
                    {
                        if(a!=0)
                          {
                            b[y]=a ;
                            y++;
                          }
                    }
System.out.println("\nAfter deleting duplicate elements\n");
               for(x=0;x<=y;x++)
                  {
                     System.out.print(b[x]+" ");
                  }
            }
       }
}

Sum and Average the elements of an array for Java Example

By Dinesh Thakur

Algorithm for Sum and Average the Elements:

step 1: set sum=0

step 2: read arr_size

step 3: create an integer array arr[] of size arr_size

step 4: initialize i=0

step 5: repeat through step-11 while i less than arr.length

step 6: read arr[i]

step 7: sum=sum +arr[i]

step 8: i=i+ 1

step 9: set avg=(float)(sum/ arr.length)

step 10: print avg

step 11: Exit

Here is the Java Example for Sum and Average the Elements:

 import java.util.Scanner; 
public class ArraySumandAvg
{
              public static void main(String args[])
        {
              int sum=0;
              Scanner s=new Scanner(System.in);
              System.out.print("Enter The Size of the Array : ");
              int size=s.nextInt();
              int arr[]=new int[size];
              System.out.println("Enter the elements of Array");
              for(int i=0;i<arr.length;i++)
                 {
                    arr[i]=s.nextInt();
                    sum+=arr[i];
                  }
                    float avg=(float)(sum/arr.length);
            System.out.println("Sum is "+sum+" Average is "+avg);
        }
}

Sum and Average the elements of an array

Largest Number in an Array Java Example

By Dinesh Thakur

Algorithm for Largest Number in an Array:

 

step 1: set big=0,small=0

step 2: read arr_size

step 3: create an integer array a[] of size arr_size

step 4: initialize x=0

step 5: repeat through step-7 while x less than a.length

step 6: read a[x]

step 7: x= x+ 1

step 8: Initialize x=0

step 9: repeat through step-12 while x less than a.length

step 10: print a[x]

step 11: print one space

step 12: x=x+1

step 13: initialize x=0

step 14: repeat through step-16 while x less than a.length

step 15: if a[x] greater than big then big=a[x]

step 16: x=x+1

step 17: print big

step 18: Exit

Here is the Java Example for Largest Number in an Array:

import java.util.Scanner; 
public class LargestNumberinArray
{
              public static void main(String args[])
      {
              int x,y,big=0,small=0;
              Scanner sl=new Scanner(System.in);
              System.out.print("Enter The Size of Array : ");
              y=sl.nextInt();
              int a[]=new int[y];
              System.out.println("Enter The Elements");
              for(x=0;x<a.length;x++)
              a[x]=sl.nextInt();
              System.out.println("\n Print The Elements of Array \n" );
              for(x=0;x<a.length;x++)
              System.out.print(a[x]+" ");
              for(x=0;x<a.length;x++)
                 {
                   if(a[x]>big)
                    {
                      big=a[x];
                    }
                 }
System.out.println("Largest Number in an Array is : "+big);
      }
}

Largest Number in an Array

Sorting an Array in Ascending Order in Java Example

By Dinesh Thakur

Algorithm for SortinganArray:

step 1: set temp=0

step 2: read arr_size

step 3: create an integer array a[] of size arr_size

step 4: initialize i=0

step 5: repeat through step-11 while i less than a.length

step 6: read a[i]

step 7: initialize j=0

step 8: repeat through step-l0 while j less than a.length

step 9: if a[i] less than a[j] then interchange value of a[i] and a[j]

step 10: j=j+1

step 11: initialize i=0

step 12: repeat through step-14 while i less than a.length

step 13: print a[i]

step 14: i=i+ 1

step 15:Exit

Here is the Java Example for Sorting an Array:

 import java.util.Scanner; 
public class SortinganArray
{
               public static void main(String args[])
         {
               int i,j,temp=0;
               Scanner s=new Scanner(System. in) ;
               System.out.print("Enter The Size of The Array : ");
               int size=s.nextInt();
               int a[]=new int[size];
               System.out.println("Enter The array Elements");
               for(i=0;i<a.length;i++)
                  {
                     a[i]=s.nextInt();
                       for(j=0;j<a.length;j++)
                          {
                              if(a[i]<a[j])
                                {
                                    temp=a[j];
                                    a[j]=a[i] ;
                                    a[i]=temp;
                                 }
                           }
                  }
System.out.println("\nSorted an Array in Ascending Order \n");
                for(i=0;i<a.length;i++)
                   {
                      System.out.print(a[i]+"\t");
                    }
          }
}

Sorting an Array in Ascending Order

Program to find how many 1’s are present in an inputted array

By Dinesh Thakur

Algorithm for Ones Present Inputted Array:

step 1: set arr_size,j, count as three static variables of integer type

step 2: read arr_size

step 3: create an integer array arr[] of size arr_size

step 4: initialize i=0 

 

step 5: repeat through step-7 while i less than arr_size

step 6: readarr[i]

step 7: i=i+ 1

step 8: initialize i=0

step 9: repeat through step-15 while i less than arr_size

step 10: j=arr[i]

step 11: repeat through step-14 while j greater than 0

step 12: k=j%10

step 13: if k equals to one then count=count+ 1

step 14:j=j/10

step 15: i=i+ 1

step 16: print count

step 17: Exit

Here is the Java Example for Ones Present Inputted Array:

import java.util.Scanner;
public class OnesPresentInputtedArray
{
              public static int size;
              public static int j;
              public static int count;
           public static void main(String args[])
      {
              Scanner s=new Scanner(System.in);
              System.out.print("Enter the Number of Elements :");
              size=s.nextInt();
              int arr[]=new int[size];
              System.out.println("Enter the Elements : ");
              for(int i=0;i<size;i++)
                 {
                     System.out.println("Enter the "+i+"th Index Value");
                     arr[i]=s.nextInt();
                  }
                     for(int i=0;i<size;i++)
                        {
                           j=arr[i];
                           while(j>0)
                              {
                                 int k=j%10;
                                   if(k==1)
                                     {
                                         count++;
                                      }
                                          j=j/10;
                               }
                         }
System.out.print("No. of 1's Present in the Inputed Array is:-> ");
System.out.println(count);
      }
}

how many 1's are present in an inputted array

Enter a number from the keyboard and find out the Even-Odd pairs within that number.

By Dinesh Thakur

Here is the Java Example for Even-Odd pairs within that number:

import java.util.Scanner; 
public class EvenOdd
{
                      public static void main(String a[])
              {
                      Scanner sl=new Scanner(System.in);
                      System.out.print("Enter Number: ");
                      int n = sl.nextInt();
                      int odd[]= new int[2];
                      int eve[] = new int[2];
                      int o =-1, e =-1;
                      StringBuffer sb = new StringBuffer();
                      for(int i=1;i<=n;i++)
                          {
                                   if(i%2==0)
                                     {
                                     e++;
                                     eve[e]=i;
                                       }
                                     if(e==1)
                                       {
                                          sb.append(eve[0] + " " + eve[1] +" ");
                                          e = -1;
                                        }
                                      if(i%2 !=0)
                                        {
                                             o++;
                                             odd[o]=i;
                                         }
                                       if(o==1)
                                        {
                                            sb.append(odd[0]+ " " + odd[1]+",");
                                            o = -1;
                                         }
                              }
                                  System.out.println(sb.delete(sb.length()-2, sb.length()));
                  }
}

Even-Odd pairs within that number

Convert Binary integer to Decimal Number Example | Java Examples

By Dinesh Thakur

Explanation of Math.pow():

Pow()method is a static method of math class present in java.lang package.

This method takes two double type argument and returns the value if the 1st argument which is raised to the power of the 2nd argument.

Syntax:

Public static double pow(double dl,double d2)

Algorithm for Convert Binary integer to Decimal Number:

step 1: set p=0, r=0

step 2: read num

step 3: create an integer array a[] of size 20

step 4: repeat through step-7 while num greater than 0

step 5: a[p]=num%l0

step 6: num= num/ 10

step 7: p=p+ 1

step 8: set q=p-l

step 9: set r=0

step 10: repeat through step-12 while q greater than or equals to 0

step 11: r=r+(a[q]*Math.pow(2, q))

step 12: q=q-1

step 13: print r

step 14: Exit

 

Here is the Java Example for Convert Binary integer to Decimal Number:

import java.util.Scanner; 
public class ConvertBintoDec
{
                 public static void main(String args[])
         {
                 int num,p=0,q,r=0;
                 Scanner sl=new Scanner(System.in);
                 System.out.print("Enter Binary Number : ");
                 num=sl.nextInt();
                 int a[]=new int[20];
                    while(num>0)
                           {
                              a[p]=num%10;
                              num=num/10;
                              p++;
                           }
                                 q=p-1;
                                 r=0;
                                  while(q>=0)
                                         {
                                             r=(int)(r+(a[q]*Math.pow(2,q)));
                                             q--;
                                          }
                                 System.out.print("Decimal Number is :-> "+r);
              }
}

Convert Binary integer to Decimal Number Example

Convert Decimal Integer to Binary Number Example | Java Examples

By Dinesh Thakur

Algorithm for Convert Decimal integer to Binary Number:
step 1: set i=0
step 2: read num
step 3: create two integer arrays a[],b[] of size 20
step 4: repeat through step- 7 while num greater than 0

step 5: a[i]=num%2
step 6: i=i+1
step 7: num=num/ 2
step 8: set k=i-1
step 9: initialize j=0
step 10: repeat through step-14 while j less than i
step 11: b[j]=a[k]
step 12: print b[i]
step 13: k=k-1
step 14: j=j+ 1
step 15: Exit
Here is the Java Example for Convert Decimal integer to Binary Number:

import java.util.Scanner; 
public class ConvertDectoOctal
{
                   public static void main(String args[])
           {
                       int r=0,q,o=0,num;
                       int a[]=new int[10];
                       int b[]=new int[10];
                       Scanner sl=new Scanner(System.in);
                       System.out.print("Enter a number : ");
                       num=sl.nextInt();
                       System.out.println("\nEntered Number is :->"+num);
                       while(num>0)
                {
                        if(num<8)
                        {
                              a[o]=num;
                              o++;
                              break;
                        }
                     else
                       {
                               a[o]=num%8;
                               o++;
                               num=num/8;
                       }
                }
                               for(q=o-1;q>=0;q--)
                               {
                                    b[r]=a[q] ;
                                    r++;
                                }
                                    System.out.print("Octal number is :->"); 
                                    for(q=0;q<r;q++)
                                    System.out.print(b[q]);
          }
}

Convert Decimal integer to Binary number

Convert Decimal Integer to Octal Number Example | Java Examples

By Dinesh Thakur

Algorithm for Convert decimal integer to octal Number:

step 1: set r=0, i=0

step 2: create two integer arrays a[],b[] of size 10

step 3: read num

step 4: repeat through step-11 while num greater than 0

step 5: if num less than 8 then execute following codes otherwise goto

step- 9

step 6: a[i]=num

step 7: i=i+ 1

step 8: goto step-4

step 9: a[i]=num%8

step 10: i=i+ 1

step 11: num= num/ 8

step 12: initialize q=i-1

step 13: repeat through step-16 while q greater than or equals to 0

step 14: b[r]=a[q]

step 15: r=r+ 1

step 16: q=q-1

step 17: initialize q=0

step 18: repeat through step-20 while q less than r

step 19: print b[q]

step 20: q=q+ 1

step 21: Exit

Here is the Java Example for Convert Decimal integer to Octal Number:

import java.util.Scanner;
public class ConvertDectoOctal
{
                   public static void main(String args[])
           {
                       int r=0,q,o=0,num;
                       int a[]=new int[10];
                       int b[]=new int[10];
                       Scanner sl=new Scanner(System.in);
                       System.out.print("Enter a number : ");
                       num=sl.nextInt();
                       System.out.println("\nEntered Number is :->"+num);
                       while(num>0)
                {
                        if(num<8)
                        {
                              a[o]=num;
                              o++;
                              break;
                        }
                     else
                       {
                               a[o]=num%8;
                               o++;
                               num=num/8;
                       }
                }
                               for(q=o-1;q>=0;q--)
                               {
                                    b[r]=a[q] ;
                                    r++;
                                }
                                    System.out.print("Octal number is :->"); 
                                    for(q=0;q<r;q++)
                                    System.out.print(b[q]);
          }
}

Convert decimal integer to octal number

Type Casting in Java Example

By Dinesh Thakur

Type casting is the process of “casting”(Casting mean ‘conversion’ of value to another type) of value to one type to another type of variable. [Read more…] about Type Casting in Java Example

Volume of a Sphere in Java Example

By Dinesh Thakur

In Volume of a Sphere in Java Example , Math.PI is used because it denotes the value of pi i.e. 3.1414. Math.pow(r,3.0) is used for computing y3 [Read more…] about Volume of a Sphere in Java Example

Area of Triangle in Java Example

By Dinesh Thakur

Here is the Java Example for Area of Triangle

class AreaofTriangle 
{
            public  static  void  main  (String  args [ ] )
            {
                        int b,h;
                        float  a;
                        b =17;
                        h=23;
                        a= (float) 1/2*b*h;
                        System.out.println("Area  of  Triangle  is  "+a);
            }
 
}

Area of Triangle in Java

(float) before 1/2 is used for typecasting. If we divide two integers, the result is also as integer. The benefit of using (float) is that it converts 1/2 into 1.0/2.0 and obviously we get the result as 0.5. If we don’t typecast 1/2, then the result of division of two integers is also an integer, hence the result of ½ comes out to be 0, so it is compulsory to typecast an expression which has a / (divide sign).

Shift Operators in Java Examples

By Dinesh Thakur

The left-shift, right-shift, and zero-fill-right-shift operators <<, >>, and >>> shift the individual bits of an integer by a specified integer amount.

Example:

 

x << 3;

y >> 1;

z >>> 2;

 

In the first example, the individual bits of the integer variable x are shifted to the left three places (so it is multiplied by 23 i.e. 8). In the second example, the bits of yare shifted 1 place to the right, so it is divided by 2. Finally, the third example shows z being shifted to the right two places, with zeros shifted into the two leftmost places (it is divided by 4 and its sign is converted to positive value if it was negative earlier) .

The number being shifted in this case is the decimal 7, which is represented in binary as 0111. The first right-shift operation shifts the bits two places to the right, resulting in the binary number 0001, or decimal 1. The next operation, a left-shift, shifts the bits one place to the left, resulting in the binary number 1110, or decimal 14. The last operation is a zero-fill-right-shift, which shifts the bits one place to the right, resulting in the binary number 0011, or decimal 3.

With positive numbers, there is no difference between the two operators right-shift operator and zero-fill-right-shift operator, they both shift zeros into the upper bits of a number. The difference arises when we start shifting negative numbers. The negative numbers have the high-order bit set to 1. The right-shift operator preserves the high-order bit and effectively shifts the lower 31 bits to the right. This behavior yields results for negative numbers similar to those for positive numbers. That is, -8 shifted right by one results in -4. The zero-fill-right-shift operator, on the other hand, shifts zeros into all the upper bits, including the high-order bit. When this shifting is applied to negative numbers, the high-order bit becomes 0 and the number becomes positive.

 

Here is the Java Example for left-shift, right-shift, and zero-fill-right-shift operators

class   ShiftOperators    
{  
            public  static  void  main  (String  args[ ] )
            {
                         int  x  =  7 ;
                         System.out.println ("x  =  "  +  x) ;
                         System.out.println ("x  >>  2  =  "  +   (x  >>  2) ) ;
                         System.out.println ("x  <<  1  =  "  +  (x  <<  1) ) ;
                        System.out.println ("x  >>>  1  =  "  +  (x  >>>  1) ) ;   
                }                 
}

Shift Operators in Java

Bitwise AND, OR, and XOR Operators in Java Example

By Dinesh Thakur

The bitwise AND, OR, and XOR operators (&, |,and /\) all act on the individual bits of an integer. These operators are useful when an integer is being used as a bit field.

Here is the Java Example for Bitwise AND, OR, and XOR Operators

class  BitwiseANDORXOROperators  
{
            public  static  void  main  (String  args[ ] )
            {
                        int  x  =  13,  y  =  10;
                        System.out.println("x   =  "  +  x);
                        System.out.println("y  =  "  +  y);
                        System.out.println("x  &  y  =  "  +  (x  &  y) ) ;
                        System.out.println("x  |  y  =  "  +  (x  |   y) ) ;
                        System.out.println("x  ^  Y  =  "  +  (x  ^  y) ) ;
            }
}

Bitwise AND, OR, and XOR Operators

Bitwise Complement Operator Example in Java

By Dinesh Thakur

The bitwise complement operator (~), which perform a bitwise negation of an integer value. Bitwise negation means that each bit in the number is toggled. In other words, all the binary 0s become 1s and all the binary 1s become 0s.

x = 8;

Y = ~x;

Integer numbers are stored in memory as a series of binary which can be either 0 or 1. A number is considered negative if the highest order bit in the number is set to 1. Because a bitwise complement converts all the bits in a number including the high order bit (sign bit). Since the number becomes negative, it is in 2’s compliment form. To know its decimal value, first we will subtract 1 from the number and then perform l’s compliment onto it (i.e. convert 1 to 0 and 0 into 1).

Here is the Java Example for Bitwise Complement Operator:

class BitwiseComplementOperator 
{         
            public  static  void  main  (String  args[ ] )
            {
                        int  x  =  8 ;
                        System.out.println("x  =  "  +  x);
                        int  y  =  ~x;
                        System.out.println ("y  =  "  +  y);
            }
}

Bitwise Complement Operator

Java Escape Sequences Examples

By Dinesh Thakur

Sometimes you have to work with characters that are not listed on the keyboard or with characters that have a special meaning, as symbol as a “newline.” They can not be displayed directly in the source the program code, and to use them we need special techniques that will look at now. [Read more…] about Java Escape Sequences Examples

What is Array in Java? – Definition, Declare, Create, Initialize [Example]

By Dinesh Thakur

Arrays Definition: Perhaps one of the most important of all concepts that you need to learn to be effective is the array. Until now, we have looked at variables that hold just one value – the ints hold one number and the strings hold one text string. Arrays are used when we want to hold two or more values, perhaps a list. Arrays can be thought as columns in a spreadsheet – a spreadsheet can have one, or it can have many columns.   [Read more…] about What is Array in Java? – Definition, Declare, Create, Initialize [Example]

Java Sum of Digits – Java Example

By Dinesh Thakur

Algorithm for Java Sum of Digits : [Read more…] about Java Sum of Digits – Java Example

How Many Digits Are Present in the Number – Java Example

By Dinesh Thakur

Algorithm for Digits Are Present in the Number:

step 1: Set c=0

step 2: Read num

step 3: Repeat through step-5 while num greater than 0

step 4: c=c+1

step 5: num=num/10

step 6: Print c

Step 7: Exit

Here is the Java Example for Digits Are Present in the Number:

import java.util.Scanner; 
public class DigitCount
{
                  int count(int num)
           {
                  if(num>=0 && num<=9)
                        return 1;
                  else
                    return 1+count(num /10);
            }
               public static void main(String args[])
                 {
                         int c,num;
                   Scanner sl=new Scanner(System.in);
                   System.out.print("Enter A Number : ");
                   num=sl.nextInt();
                   DigitCount d=new DigitCount();
                   c=d.count(num);
                   System.out.println("Number of Digit is "+c);
                 }
}

Digits Are Present in the Number

Check Number is Binary or Not Using Java Example

By Dinesh Thakur

Algorithm for Check Number is Binary or Not:

step 1: Set r=0,c=0

step 2: Read num

step 3: Set b=num

step 4: Repeat through step-7 while num greater than 0

step 5: If (num mod 10)equals to 0 OR equals to 1 then c=c+1

step 6: r=r+ 1

step 7: num=num/ 1 0

step 8: If c equals to r then Print it is a binary number

          Else print it is not a binary number

step 9:Exit

Here is the Java Example for Check Number is Binary or Not:

import java.util.Scanner; 
public class CheckNumberisBinary
{
            public static void main(String args[])
        {
                   int r=0,c=0,num,b;
              Scanner sl=new Scanner(System.in);
              System.out.println("Enter a number");
              num=sl.nextInt();
              b= num;
              while(num>0)
                   {
                      if((num %10==0)|| (num%10==1))
                         c++;
                         r++;
                         num=num/10;
                   }
                      if(c==r)
               System.out.println(b+" is a Binary Number.");
               else
               System.out.println(b+" is not a Binary Number");
           }
}

Check Number is Binary or Not

Highest Prime Number Within the Given Range Java Example.

By Dinesh Thakur

Algorithm for Highest Prime Number:

step 1: Read num

step 2: Set i== 1

step 3: Initialize j==2

step 4: Repeat through step-7 while j less than or equals to num

step 5: If j equals to num then Set i==num and goto 8

step 6: If (num mod)) equals to 0 then num==num-1

step 7: j==j+1

step 8: Print i

Step 9: Exit

Here is the Java Example for Highest Prime Number:

public class HighestNumberRange 
{
               public static void main(String args[])
         {
                   int i=prime(100);
              System.out.println("value of highest prime is :-"+i);
         }
              public static int prime(int num)
              {
                        int i=1;
                 for(int j=2;j<=num;j++)
                    {
                      if(j== num)
                       {
                          i= num;
                          break;
                       }
                         if(num% j==0)
                           {
                              num--;
                           }
                     }
                           return i;
              }
}

Highest Prime Number Within the Given Range

Triangle of any size in Java Example

By Dinesh Thakur

Algorithm for Triangle of any size:

step 1: set i=0,j=0,row=0,osc=0,var=0

step 2: read row

step 3: if row greater than 0 then execute following codes otherwise goto

          step-33

step 4: osc=row-l

step 5: set isc= 1

step 6: initialize i=osc

step 7: Repeat through step-9 while i greater than 0

step 8: Print one space

step 9: i=i-1

step 10: Print “*”

step 11: osc=osc-1

step 12: Initialize i=2

step 13: Repeat through slep-26 while i less than row

step 14: initialize k=osc

step 15: Repeat through step-17 while k greater than 0

step 16: Print one space

step 17: k=k-1

step 18: Print “*”

step 19: Initialize j=isc

step 20: Repeat through step-22 while j greater than 0

step 21: Print one space

step 22: j=j-1

step 23: Print “*”

step 24: isc=isc+ 2

step 25: osc=osc-l

step 26: i=i+1

step 27: If row not equal to one then execute following codes otherwise

          goto step-33

step 28: Initialize i= 1

step 29: Repeat through step-32 while i less than or equals to row

step 30: Print “*”

step 31: Print one space

step 32: i=i+ 1

step 33: Exit

Here is the Java Example for Triangle of any size:

import java.util.*; 
class RectangleofAnySize
{
          public static void main(String s[])
        {
           int i,j,row,isc,osc,var;
           i=j=row=osc=var=0;
           Scanner read=new Scanner(System.in);
           System.out.print("Enter Number of Rows : ");
           row=read.nextInt();
           System.out.println("\n");
           if(row>0)
             {
                System.out.print("\t");
                osc=row-1;
                isc=1;
                for(i=osc;i>0;i--)
                   {
                       System.out.print(" ");
                   }
                       System.out.println("*");
                       osc--;
                for(i=2;i<row;i++)
                   {
                      System.out.print("\t");
                      for(int k=osc;k>0;k--)
                         {
                           System.out.print(" ");
                         }
                           System.out.print("*");
                             for(j=isc;j>0;j--)
                                {
                                  System.out.print(" ");
                                }
                                  System.out.println("*");
                                  isc+=2;
                                  osc--;
                    }
                                   System.out.print("\t");
                                      if(row!=1)
                                       {
                                        for(i=1;i<=row*2-1;i++)
                                          {
                                            System.out.print("*");
                                          }
                                       }
              }
System.out.println("");
         }
}

Triangle of any size in Java Example

Right Angled Triangle in Java Example

By Dinesh Thakur

Algorithm for Right Angled Triangle:

 

step 1: Set i= 0,j 0, row=0,osc=0,var=0

step 2: Read row

step 3: Move to new line

step4: If (row>O) then execute following steps else goto step-23

step 5: Initialize isc=0

step 6: Print “*”

step 7: Initialize i=2

step 8: Repeat through step-17while (i<row)

step 9: Print “*”

step l0: Initialize j=isc

step 11: Repeat through step-13 while(j>0)

step 12: Print one space

step 13: j=j-1

step 14: Print “*”

step 15: isc=isc+ 1

step 16: osc=osc-1

 step 17: I=i+ 1

step 18: If(row!=1then execute following steps else goto step-23

step 19: Initialize l=1

step 20: Repeat through step-22 while (l<=row)

step 21: Print “*”

step 22:l=l+1

step 23: Exit

Here is the Java Example for Right Angled Triangle:

import java.util.*; 
class RightAngledTriangle
{
                  public static void main(String s[])
              {
                      int i,j,row,isc,osc,var;
                      i=j=row=osc=var=0;
                      Scanner read=new Scanner(System.in);
                      System.out.print("Enter number of Rows : ");
                      row=read.nextInt();
                      System.out.println("\n");
                      if(row>0)
                        {
                           System.out.print("\t");
                           isc=0;
                           System.out.println("*");
                            for(i=2;i<row;i++)
                               {
                                 System.out.print("\t");
                                 System.out.print("*");
                                    for(j=isc;j>0;j--)
                                         System.out.print(" ");
                                         System.out.println("*");
                                         isc++;
                                         osc--;
                                }
                                     if(row!=1)
                                      {
                                        System.out.print("\t");
                                        for(int l=1;l<=row;l++)
                                        System.out.print("*");
                                      }
                          }
                                System.out.println("");
                  }
}

Right Angled Triangle in Java Example

Floyd Triangle in Java Example

By Dinesh Thakur

Algorithm for Floyd Triangle:

step 1: Set a= 1

step 2: Read row

step 3: Initialize i=0

step 4: Repeat through step-12 until i less than row

step 5: Initialize j=0

step 6: Repeat through step-10 until j less than or equals to i

step 7: Print a

step 8: If a less than 10 then print “ “

Else print “ “

step 9: a=a+1

step 10: j= j+ 1

step 11: move to new line

step 12: i=i+ 1

step 13: Exit

Here is the Java Example for Floyd Triangle:

import java.util.Scanner; 
public class FloydTriangle
{
                public static void main(String ar[])
         {
                    int a=1,i,j,row;
               Scanner s1=new Scanner(System.in);
               System.out.println("Enter The Number of ROWS");
               row=s1.nextInt();
               System.out.println("\n");
               for(i=0;i<row;i++)
                  {
                    for(j=0;j<=i ;j++)
                       {
                         if(a<10)
                           System.out.print(a+" ");
                           else
                           System.out.print(a+" ");
                           a++;
                        }
                           System.out.print("\n");
                   }
           }
}

Floyd Triangle in Java Example

GCD of Two Numbers in Java Using While

By Dinesh Thakur

Algorithm for GCD of Two Numbers :

step 1:Read a, b

step 2: Repeat through step-5 while a not equals to 0

step 3: Set gcd=a

step 4: a=b%a

step 5:b=gcd

step 6: Print gcd

step 7: Exit

Here is the Java Example for GCD of Two Numbers:

import java.util.*; 
public class GCDofTwoNumbers
{
                public static void main(String args[])
            {
                      int a,b,gcd;
                      a=b=gcd=0;
                      Scanner s=new Scanner(System.in);
                      system.out.println("Enter two nos.");
                      a=s.nextInt() ;
                      b=s.nextInt();
                      while(a!=0)
                            {
                                gcd=a;
                                a=b%a;
                                b=gcd;
                            }
                     System.out.println("GCD "+gcd);
             }
}

GCD of Two Numbers in Java Using While

Swap Variables in a Different Way Without Using Third Variables

By Dinesh Thakur

 Algorithm for Swap Variables in a Different Way:

step 1: Read a, b

step 2: a=(a+b)-(b=a)

step 3: Print a,b

step 4: Exit

Here is the Java Example for Swap Variables in a Different Way:

import java.util.Scanner; 
public class SwapDifferentWay
{
            public static void main(String args[])
       {
            Scanner s=new Scanner(System.in);
            int a,b;
            System.out.println("Enter Two Numbers");
                 a=s.nextInt();
                 b=s.nextInt();
            System.out.println("Before Swap");
            System.out.println("value of a is "+a+" value of b is "+b);
                 a=(a+b)-(b=a);
           System.out.println("After swap") ;
           System.out.println("value of a is "+a+" value of b is "+b);
       }
}

Swap the Values of Two Variables in a Different Way Without Using Third Variables

Swap Two Variables Using XOR Operator Using Java Example

By Dinesh Thakur

Algorithm for Swap Two Variables Using XOR Operator:

step 1: Read a, b

step 2: a=a^b

step 3: b=b^a

step 4: a=a^b

step 5: Print a,b

step 6: Exit

Here is the Java Example for Swap Two Variables Using XOR Operator:

import java.util.Scanner; 
public class SwapUsingXOR
{
                 public static void main(String args[])
           {
                  Scanner s=new Scanner(System.in);
                  int a,b;
                  System.out.println("Enter Two Numbers");
                  a=s.nextInt() ;
                  b=s.nextInt();
                  System.out.println("Before swap");
                  System.out.println("value of a is "+a+" value of b is "+b);
                  a=a^b;
                  b=b^a;
                  a=a^b;
                  System.out.println("After swap") ;
                  System.out.println("value of a is "+a+" value of b is "+b);
             }
}

Swap Two Variables Using XOR Operator

Strong Number Program in Java Example

By Dinesh Thakur

If sum of factorial of each digit of the number is same as the actual number then that number is called as strong number.

Algorithm for Strong Number:

Here the method has one formal argument b and it returns an integer value to its caller.

step 1: Set fact= 1

step 2: Read b

step 3: Set c=b

step 4: Repeat through step-6 until (c > 0)

step 5: fact=fact*c

step 6: c=c-1

step 7: return fact

step 8: Exit

Here is the Java Example for Strong Number:

import java.util.Scanner; 
public class StrongNumber
{
               public static void main(String args[])
              {
                 StrongNumber ss=new StrongNumber();
                 int a,b,r,s=0;
                 Scanner sl=new Scanner(System.in);
                 System.out.println("Enter A Number");
                 b=sl. nextInt() ;
                 a=b;
                 while(b>0)
                       {
                           r=b%10;
                           s=s+ss.fact(r);
                           b=b/10;
                       }
                  if(a==s)
                     System.out.println(a+" is a strong number");
                  else
                     System.out.println(a+" is not a strong number");
                }
                       int fact(int i)
                             {
                                 int f,j;
                                 f=1;
                                for(j=i ;j>0;j--)
                                     f=f*j;
                                     return f;
                              }
}

Strong Number Program in Java Example

« Previous Page
Next Page »

Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW