ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

What is MDAS? Sample Java Source Code for MDAS

Updated on October 31, 2013

Get A Website Plus a Free Domain Name in Just 1 Hour!

Bring the new technology in your hands! Share your skills, improve and impress. Get Your Own Website and a Free Domain Name Here!

What is MDAS?

MDAS stands for Multiplication, Division, Addition and Subtraction. MDAS is a standard ordering of operations or a standard precedence of operation in Mathematics and modern computing application on what to execute first if the numbers are not enclosed in parenthesis.

For example, if we will solve,

6 + 4 * 3

The answer without MDAS rule should be

6 + 4 = 10 * 3 = 30

but with MDAS rule, the answer would be different. Because it gives priority to multiplication first, followed by division, then the addition and last to execute is the subtraction. With MDAS the answer to

6 + 4 * 3

is this:

4 * 3 = 12 + 6 = 18

See the huge difference between the answer with and without the MDAS rule? Similarly, MDAS is also introduced in computer programming. Since, MDAS would be helpful for those who are learning Java Programming, here is the sample code for MDAS in Java.

What does MDAS Program Offers?

1. Learning more on Looping

2. Learning more on the use of array.

3. Learning how to execute basic arithmetic operations in Java programming.

Java Source Code for MDAS Program


package mdas_using_for_loop;


import java.util.Scanner;

public class Main {

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        int maxSize=5;

        double sum = 0.0;
        double product;
        double divide;
        double subtract;
       
        System.out.print("Enter 5 numbers to calculate: ");

        double arr[] = new double [maxSize];


        for(int calculateNum=0; calculateNum < maxSize; calculateNum++)
        {
            arr[calculateNum] = input.nextInt();
        }
        System.out.println();
        System.out.println("--MDAS RESULT ON 5 NUMBERS--");
        System.out.println();
        for(int calculateNum=0; calculateNum < maxSize; calculateNum++)
        {
            if(arr[calculateNum] == maxSize - 1)
            {
                product = arr[0] * arr[1];
                System.out.println("(M)Product: " + product);

                divide = product/ arr[2];
                System.out.println("(D)Qoutient: " + divide);

                sum = (int) (divide + arr[3]);
                System.out.println("(A)Sum: " + sum);

                subtract = (sum - arr[4]);
                System.out.println("(S)Difference:" + subtract);

            }
        }

        
    }

}

Sample Output:

If this hub is helpful to you, please rate and share it. Happy Programming!

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.