Posts

Showing posts with the label java programming

Bubble Sort Program in Java

Image
                      BUBBLE SORT PROGRAM USING JAVA                                           In Bubble sort we compare two consecutive elements of an array, if first one element is greater than next one then swap them. The time complexity in worst and average case is O(n^2) while in best case is O(n) and the space complexity is O(1) . package bubbleSort; public class BubbleSort { public static void bubbleSort(int[] arr) { for(int i=0; i<arr.length; i++) { for(int j=i+1; j<arr.length; j++) { if(arr[i] > arr[j]) { // swap the numbers int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } }          // main method  public static void main(String[] args) {         // declare and assign an unsorted array  i...

Calculator by java programming language

 import java.util.Scanner; public class Calculator{     public static void main(String args[]){         Scanner input = new Scanner(System.in);         char opr;         float a,b;         System.out.println("input the value\t");         a = input.nextFloat();         opr = input.next().charAt(0);         b = input.nextFloat();         if(opr=='+')         {             System.out.print(a+b);         }         else if(opr=='-')         {           ...