Saturday, August 29, 2015

Sum and Average of Five Numbers Using Two Dimensional Array in Java

Two dimensional array is also known as matrix in Java in this article I would like to share with you a sample program that I wrote that demonstrate how to declare and use two dimensional array in Java programming language I called this program Sum and Average of Five Numbers Using Two Dimensional Array in Java.

What does the program will do is to ask the user to enter five numbers and then the program will compute the sum and average of the five numbers given by the user.

If you have some questions about programming, about my work please send mu an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com. People here in the Philippines can contact me at  my mobile number 09173084360.

Thank you very much and Happy Programming.


Sample Program Output


Program Listing

/*
average_sum_two_dimensional.java
Programmer By: Mr. Jake R. Pomperada, MAED-IT
Date : July 23, 2015
Tools: NetBeans 8.0.2, Java SE 8.0

Problem No. 1:
  
Write a program that will ask the user to give five numbers 
and then our program will find the total sum and average of
five numbers using two dimensional array.
*/


import java.util.Scanner;

class average_sum_two_dimensional {
    
public static void main(String[] args) {
      
     Scanner input = new Scanner(System.in);
     char ch;
   
  do { 
      int [][] values; 
      values  = new int[5][1];  
      int total_sum=0, average=0;
      System.out.print("Sum and Average of Five Numbers");
      System.out.println();
      for (int a=0; a<5; a++)
        for (int b=0; b<1; b++) {  
      {
        
         System.out.print("Enter value in item no." + (a+1) + " : ");
          
         values[a][b]= input.nextInt();
         total_sum += values[a][b];
         average = total_sum/5;
      }
    } 
     System.out.println(); 
     System.out.print("The total sum is " + total_sum + ".");
     System.out.println();
     System.out.print("The average of five numbers is " + average + ".");
     System.out.println();
     System.out.print("\nDo you want to continue (Type y or n) : ");
     ch = input.next().charAt(0);                        
     } while (ch == 'Y'|| ch == 'y');  
     System.out.println();
     System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
     System.out.println("\n\n");
  }
}


No comments:

Post a Comment