Exercise 2 Arithmetic Operations: Object Oriented Programming - Practical Exercise 2
Exercise 2 Arithmetic Operations: Object Oriented Programming - Practical Exercise 2
Exercise 2
Exercise 2
Arithmetic Operations
Objective: This exercise enables you to understand the basic arithmetic operators in Java. Procedure: This program prints the result of arithmetic operations on integer data types on the console. Algorithm: The algorithm for the program is given below: Step 1: Open a new text document on your desktop. Step 2: Type in the relevant code as given below: 2.1: Write a main function with appropriate declarations: int sum, diff, prod, quotient; //call the functions as shown below. sum = add(a,b); diff = sub(a,b); prod = mul(a,b); quotient = div(a,b); 2.2: declare functions as given below: int add(int a, int b); { return a + b;} int sub(int a, int b); { return a-b;} // check for the sign // of the resultant by using if statements to get a // positive or negative result. int prod(int a, int b); { return a * b); } int div(inta,int b); { return a/b or b/a as appropriate} Step 3: Print the results on the console.
Page No.: 2