javaassignment
javaassignment
Name: SIDDHARTHA
Registration NO. : 23BCE11143
Roll no. : 94
Explanation:
Handling Errors:
If the user attempts to divide or find the remainder with zero, the program
displays an error message and stops execution.
If the user enters an invalid operator, the program notifies the user and exit.
Displaying the Result:
User Input:
The program asks the user to enter a number.
Even or Odd Check:
The ternary operator (num % 2 == 0) ? "Even" : "Odd"
determines whether the number is even or odd.
Positive, Negative, or Zero Check:
The ternary operator (num > 0) ? "Positive" : (num <
0) ? "Negative" : "Zero" is used to classify the number.
Output Display:
The results are printed.
Code:
Q.3 Write a Java program to convert a decimal number
to binary, octal, and hexadecimal using bitwise
operators.
Explanation:
Binary Conversion:
Use bitwise AND (& 1) to extract the last bit.
Use right shift (>> 1) to move to the next bit.
Octal Conversion:
Hexadecimal Conversion:
CODE:
Q.4 Write a Java program to simulate a simple login
system that checks for valid username and password
using if-else statements.
Explanation:
Predefined Credentials:
The program stores a valid username ("admin") and password ("password123").
User Input:
The program prompts the user to enter a username and password.
Credential Validation:
It checks if the entered username and password match the stored credentials.
If they match, it prints "Login successful!".
Otherwise, it prints "Invalid username or password"
CODE
Q.5 Write a Java program to simulate a simple menu-
driven system that uses switch statements to
perform different actions based on user input.
Explanation:
Menu Display:
The program shows a menu with options for addition, subtraction,
multiplication, division, and exit.
User Input Handling:
The user selects an option from the menu.
If they choose 5, the program exits.
Performing Operations:
The program asks for two numbers.
Based on the user’s choice, it performs the corresponding arithmetic
operation.
If division is chosen, it checks for division by zero.
Looping Until Exit:
The program runs inside a while loop until the user selects the exit
option.
CODE:
OUTPUT:
Q.6 Write a Java program to print the first 10
Fibonacci numbers using a for loop.
Explanation:
Initialize First Two Numbers:
The first two numbers in the Fibonacci sequence are 0 and 1.
OUTPUT:
OUTPUT:
Q.8 Write a Java program to print the first 100 prime
numbers using a while loop.
Explanation:
Initialize Counters:
count keeps track of how many prime numbers have been printed.
num starts from 2 (the first prime number).
CODE:
OUTPUT:
Q.9 Write a Java program to find the sum of all
numbers from 1 to 10 that are not divisible by 2
using a break statement to exit a loop.
Explanation:
Initialize sum to 0
This variable stores the total sum.
For Loop Iteration (1 to 10):
CODE:
OUTPUT:
Q.10 Write a Java program to simulate a simple
calculator that uses a continue statement to skip an
iteration of a loop when an invalid input is entered.
Explanation:
Looping for Continuous Calculation:
The program runs inside a while (true) loop, allowing multiple calculations.
It asks for two numbers and an operator.
Handling Invalid Inputs with continue:
If the user enters a non-numeric value, it prints an error message, skips the iteration,
and asks for input again.
If division by zero is attempted, the program displays an error and skips that
calculation.
Performing Arithmetic Operations:
CODE:
CODE: