Thursday, August 3, 2017

Odd and Even Numbers with Bubble Sort in Java

Here is the revision of my previous code that I wrote using Java as my programming language. The the program does it will accepts a series of numbers and then it will display the list of odd and even numbers. After which our program will ask the user if the user would like to sort the given series of numbers in ascending or descending order. The code uses bubble sort algorithm to sort the numbers.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

Thank you.




Sample Program Output


Program Listing


import java.util.Scanner;

class odd_even {

  public static void main(String[] args) {

 Scanner input = new Scanner(System.in);
 char ch;
 int [] values;
 values = new int[10];
 int x=0;
 System.out.print("ODD AND EVEN NUMBERS PROGRAMS VERSION 2.0");
 System.out.println("\n\n");
 System.out.print("Written By: Mr. Jake R. Pomperada ");
 System.out.println("\n\n");
  for (int a=0; a<10; a++)
  {
x=1;
x+=a;
  System.out.print("Enter value in item no. " + x + " : ");
  values[a] = input.nextInt();
}
 System.out.println();
 System.out.print("LIST OF EVEN NUMBERS");
 System.out.println();
 for (int a=0; a<10; a++)
  {
  if(values[a]%2 == 0) {
System.out.print(" " + values[a]+ " ");
}
 }
 System.out.println("\n\n");
 System.out.print("LIST OF ODD NUMBERS ");
 System.out.println();
 for (int a=0; a<10; a++)
 {
 if(values[a]%2 != 0) {
System.out.print(" " + values[a]+ " ");
}
 }
 System.out.println("\n\n");
 System.out.print("\n Sort Array Order? 1 - Ascending Order and 2 - Descending Order : ");
 ch = input.next().charAt(0);

 if (ch=='1') {
 //Sorting in ascending order using bubble sort
        bubbleSortInAscendingOrder(values);

        System.out.println("\n\n");
        System.out.print("===== ASCENDING ORDER ===== ");
        System.out.println("\n\n");
        for(int i = 0; i < values.length; i++)
        {
            System.out.print(values[i]+" ");
        }
   }

    if (ch=='2') {

     //Sorting in Decending order using bubble sort

            bubbleSortInDecendingOrder(values);

            System.out.println("\n\n");
            System.out.print("===== DECENDING ORDER ===== ");
            System.out.println("\n\n");
            for(int i = 0; i < values.length; i++)
            {
                System.out.print(values[i]+" ");
            }
   }
 System.out.println("\n\n");
 System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
 System.out.println("\n\n");
 }


   //This method sorts the input array in asecnding order

     public static void bubbleSortInAscendingOrder( int numbers[])
     {
         int temp;

         for(int i = 0; i < numbers.length; i++)
         {
             for(int j = 1; j < (numbers.length -i); j++)
             {
                 //if numbers[j-1] > numbers[j], swap the elements
                 if(numbers[j-1] > numbers[j])
                 {
                     temp = numbers[j-1];
                     numbers[j-1]=numbers[j];
                     numbers[j]=temp;
                 }
             }
         }
     }


     //This method sorts the input array in decending order

     public static void bubbleSortInDecendingOrder( int numbers[])
     {
         int temp;

         for(int i = 0; i < numbers.length; i++)
         {
             for(int j = 1; j < (numbers.length -i); j++)
             {
                 //if numbers[j-1] > numbers[j], swap the elements
                 if(numbers[j-1] < numbers[j])
                 {
                     temp = numbers[j-1];
                     numbers[j-1]=numbers[j];
                     numbers[j]=temp;
                 }
             }
         }
     }


} // End of Code

No comments:

Post a Comment