Java Programs for Basic Calculations and Logic
Java Programs for Basic Calculations and Logic
The Java program uses a switch statement within a menu-driven system to perform arithmetic operations as selected by the user. Options include addition, subtraction, multiplication, and division. It runs inside a while loop, allowing continuous execution until the user selects the exit option. This structure ensures that the program remains interactive, repeatedly asking the user for inputs and performing calculations until explicitly told to exit .
The Java program ensures continued operation in a calculator with invalid inputs by running within a while (true) loop. It employs the continue statement to skip iterations when non-numeric inputs are detected or when the division by zero is attempted, displaying errors without crashing. This prevents system interruptions and maintains stability by prompting for new input despite errors .
The ternary operator simplifies if-else statements by providing a concise syntax for conditional expressions. To check a number's parity, the expression (num % 2 == 0) ? "Even" : "Odd" evaluates to "Even" if the number is divisible by 2, and "Odd" otherwise. Similarly, the expression (num > 0) ? "Positive" : (num < 0) ? "Negative" : "Zero" categorizes the number as positive, negative, or zero in one line .
The Java program simulates a calculator by prompting the user to input two numbers and an arithmetic operation (+, -, *, /, %). It uses a switch statement to determine and perform the selected operation. If the user attempts division or modulo by zero, the program displays an error message and halts execution. If an invalid operator is entered, it notifies the user and exits the program. This approach ensures that only valid operations are attempted .
The Java program converts a decimal number to binary, octal, and hexadecimal using bitwise operations. For binary conversion, it extracts individual bits using the AND operator (& 1) and shifts bits using right shift (>> 1). Octal conversion uses (& 7) to extract groups of three bits, and the right shift (>> 3). Hexadecimal conversion employs (& 15) to capture four-bit groups, shifting right (>> 4) accordingly .
The Java program generates the first 10 Fibonacci numbers using a for loop. It initializes the first two numbers of the sequence as 0 and 1. In each iteration of the loop, the next number in the sequence is calculated as the sum of the previous two numbers (next = first + second). After each calculation, the program updates the values of the first and second variables to move forward in the sequence .
The Java program simulates a game of chance by filling an integer array with random numbers within the range of 1 to 100 using a for loop. A for-each loop then iterates over the array to print each generated random number, letting users view the output of their "chance" in this simple simulation .
The program calculates the sum of odd numbers from 1 to 10 using a for loop. It initializes a sum variable to 0 and iterates through the range. If a number is even, the loop skips it using the continue statement. Only odd numbers are added to the sum. The break statement is not explicitly used in this process; rather, the continue statement ensures that even numbers do not affect the sum .
The program prints the first 100 prime numbers using a while loop controlled by a count variable that tracks the number of primes printed. Starting with num initialized to 2, a nested method isPrime() checks each number's primality by confirming non-divisibility from 2 to √n. If a number is prime, it's printed, and the count is incremented. The loop continues until 100 primes are output .
The Java program defines a simple login system by storing known sets of credentials: a username "admin" and a password "password123". When the user inputs their details, the program checks them against the stored credentials using if-else statements. A match results in "Login successful!" being displayed, while mismatches result in "Invalid username or password". This simple verification is achieved through conditional checks .