0% found this document useful (0 votes)
56 views3 pages

Basic Java Programming Exercises

This document provides 10 exercises to practice basic Java programming. The exercises include printing text, performing mathematical operations like addition and multiplication on input numbers, calculating expressions, and printing patterns. The test data and expected output are provided for each exercise.

Uploaded by

Elaine Joy Roxas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views3 pages

Basic Java Programming Exercises

This document provides 10 exercises to practice basic Java programming. The exercises include printing text, performing mathematical operations like addition and multiplication on input numbers, calculating expressions, and printing patterns. The test data and expected output are provided for each exercise.

Uploaded by

Elaine Joy Roxas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA BASIC EXERCISES (5 POINTS EACH)

1. Write a Java program to print 'Hello' on screen and then print your name on a
separate line.
Expected Output :
Hello
Alexandra Abramov

2. Write a Java program to print the sum of two numbers. 


Test Data:
74 + 36
Expected Output :
110

3. Write a Java program to divide two numbers and print on the screen.
Test Data :
50/3
Expected Output :
16

4. Write a Java program to print the result of the following operations.


Test Data:
a. -5 + 8 * 6
b. (55+9) % 9
c. 20 + -3*5 / 8
d. 5 + 15 / 3 * 2 - 8 % 3
Expected Output :
43
1
19
13

5.  Write a Java program that takes two numbers as input and display the product
of two numbers.

1
Test Data:
Input first number: 25
Input second number: 5
Expected Output :
25 x 5 = 125

6. Write a Java program to print the sum (addition), multiply, subtract, divide and
remainder of two numbers.
Test Data:
Input first number: 125
Input second number: 24
Expected Output :
125 + 24 = 149
125 - 24 = 101
125 x 24 = 3000
125 / 24 = 5
125 mod 24 = 5

7. Write a Java program that takes a number as input and prints its multiplication
table up to 10.
Test Data:
Input a number: 8
Expected Output :
8x1=8
8 x 2 = 16
8 x 3 = 24
...
8 x 10 = 80

2
8. Write a Java program to display the following pattern.

9. Write a Java program to compute the specified expressions and print the
output. 
Test Data:
((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5))
Expected Output
2.138888888888889

10. Write a Java program to compute a specified formula. 


Specified Formula :
4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))
Expected Output
2.9760461760461765

Common questions

Powered by AI

The expected output pattern for integer division in Java is the quotient of the division, ignoring any remainder. For example, dividing 50 by 3 in Java as integers delivers a result of 16, as Java truncates the decimal part. This output pattern highlights a fundamental behavior where only full divisor fits are considered, essential in scenarios disregarding floating-point precision .

Implementing a complex expression in Java requires understanding the order of operations and appropriate use of parentheses to explicitly define evaluation order. For '((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5))', calculate the operations within the innermost parentheses first. Ensure to handle precision by using double data type. Perform and store these computations in an expression, then print using 'System.out.println()', which results in approximately 2.138888888888889 .

Java follows standard mathematical rules for the order of operations, often remembered by the acronym PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction). In the expression '-5 + 8 * 6', multiplication is performed before addition. Thus, 8 * 6 equals 48, and then -5 is added, resulting in 43 .

Java treats integer division by truncating decimal results, whereas floating-point division retains precision. For example, '50 / 3' using integers results in 16, discarding the remainder. Mixed-type division, or casting integers to double, retains fractional parts. This distinction affects programs requiring high precision, like scientific computations, where integer division may lead to significant inaccuracies .

To compute and display the result of dividing two numbers in Java, first ensure you define your variables with appropriate data types. Next, perform the division operation within a print statement or store the result in a variable to print later. Consider casting to a double for precise division if working with integers. For example, dividing 50 by 3 can be done using: 'System.out.println(50 / 3);' which yields 16 when not casting to double .

To generate and display a multiplication table up to 10 for an input number in Java, use a loop structure such as a 'for' loop. The loop should iterate from 1 to 10, and in each iteration, compute the product of the loop variable and the input number. Display the results formatted as 'input_number x loop_variable = product' using 'System.out.println()'. For example, for an input of 8, the output will include lines like '8 x 1 = 8' through '8 x 10 = 80' .

To implement a program in Java that performs multiple arithmetic operations on two input numbers, a developer should first gather inputs using a Scanner class to read from the console. Then, perform each operation: addition with '+', subtraction with '-', multiplication with '*', division with '/', and modulus with '%'. Output each result using 'System.out.println()'. This allows for handling results such as '125 + 24 = 149' and '125 mod 24 = 5' for inputs 125 and 24, respectively .

A Java program can be structured to take two user inputs using the Scanner class for console inputs. Declare two variables to store these inputs, use 'scanner.nextInt()' or 'scanner.nextDouble()' as needed based on the data type for capturing user input. Compute the product using the '*' operator. Finally, display the result using the 'System.out.println()' method, as shown by '25 x 5 = 125' for inputs 25 and 5 .

To compute a specified formula in Java, like '4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))', understand the expression's components and order. Use floating-point operations for precision and evaluate inside-out. Implement the formula directly within a print statement or store its result in a double variable for precision and clarity. Executing this in Java results in approximately 2.9760461760461765 .

Using proper data types in Java ensures that arithmetic computations are performed accurately and expectedly. Integer types cannot handle fractions, whereas double types can. If precision is required, like in ((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5)), using 'double' ensures the 2.138888888888889 output. Incorrectly using 'int' would truncate decimals, giving wrong results. Thus, data type impacts precision and correctness, crucial in financial or scientific computation .

You might also like