CSIT751 (Core Java)
Module I – Introduction to Java
Programming Exercises for Practice (Practical Home Assignment 1)
1. Identifier Naming Practice
Task:
Write a program that declares variables for a student’s name, roll number, and marks in three
subjects. Assign values and print them. Ensure all variable names follow Java identifier rules.
CODING:
public class ques1 {
public static void main(String[] args) {
// Declare variables following Java identifier rules
String studentName = "Sneha Chaudhary";
int studentRollNumber = 101;
int marksSubject1 = 90;
int marksSubject2 = 88;
int marksSubject3 = 96;
// Print the assigned values
[Link]("Student Name: " + studentName);
[Link]("Roll Number: " + studentRollNumber);
[Link]("Marks in Subject 1: " + marksSubject1);
[Link]("Marks in Subject 2: " + marksSubject2);
[Link]("Marks in Subject 3: " + marksSubject3);
}
}
OUTPUT:
2. Data Type Conversion
Task:
Write a Java program that:
Stores an integer value for temperature in Celsius.
Converts it to Fahrenheit using the formula F = (C × 9/5) + 32.
Displays both Celsius and Fahrenheit values.
CODING:
public class ques2 {
public static void main(String[] args) {
// Store an integer value for temperature in Celsius
int celsius = 25; // Example Celsius temperature
// Convert Celsius to Fahrenheit using the formula F = (C × 9/5) + 32
// Use floating-point division for accurate calculation
double fahrenheit = (celsius * 9.0 / 5.0) + 32;
// Display both Celsius and Fahrenheit values
[Link]("Temperature in Celsius: " + celsius + "°C");
[Link]("Temperature in Fahrenheit: " + fahrenheit + "°F");
}
}
OUTPUT:
3. Arithmetic Operations
Task:
Accept two integers from the user and perform addition, subtraction, multiplication, division, and
modulus. Display results for each operation.
CODING:
import [Link]; // Import the Scanner class to read user input
public class ques3 {
public static void main(String[] args) {
// Create a Scanner object to read input from the console
Scanner input = new Scanner([Link]);
// Prompt the user to enter the first integer
[Link]("Enter the first integer: ");
int num1 = [Link](); // Read the first integer
// Prompt the user to enter the second integer
[Link]("Enter the second integer: ");
int num2 = [Link](); // Read the second integer
// Perform arithmetic operations
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
// Division requires special handling for potential division by zero and floating-point result
double quotient;
if (num2 != 0) {
quotient = (double) num1 / num2; // Cast to double for accurate division
} else {
quotient = [Link]; // Not a Number if division by zero occurs
}
int modulus;
if (num2 != 0) {
modulus = num1 % num2;
} else {
modulus = 0; // Or handle as an error if desired, for this example set to 0
}
// Display the results
[Link]("\nResults:");
[Link]("Addition: " + num1 + " + " + num2 + " = " + sum);
[Link]("Subtraction: " + num1 + " - " + num2 + " = " + difference);
[Link]("Multiplication: " + num1 + " * " + num2 + " = " + product);
if (num2 != 0) {
[Link]("Division: " + num1 + " / " + num2 + " = " + quotient);
[Link]("Modulus: " + num1 + " % " + num2 + " = " + modulus);
} else {
[Link]("Division: Cannot divide by zero.");
[Link]("Modulus: Cannot perform modulus with zero.");
}
// Close the scanner to release system resources
[Link]();
}
}
OUTPUT:
4. Logical Operators – Eligibility Check
Task:
Write a program to check if a person is eligible to vote.
Input: age and citizenship status (true or false).
Use logical operators (&&, ||) to decide eligibility.
CODING:
import [Link];
public class ques4 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Input age
[Link]("Enter your age: ");
int age = [Link]();
// Input citizenship status
[Link]("Are you a citizen? (true/false): ");
boolean isCitizen = [Link]();
// Input NRI status
[Link]("Are you an NRI (Non-Resident Indian)? (true/false): ");
boolean isNRI = [Link]();
// Check eligibility using && and ||
if ((age >= 18 && isCitizen) || (age >= 18 && isNRI)) {
[Link]("You are eligible to vote.");
} else {
[Link]("You are NOT eligible to vote.");
}
[Link]();
}
}
OUTPUT:
5. Control Statement – Simple Calculator
Task:
Write a program that:
Takes two numbers and an operator symbol (+, -, *, /) from the user.
Uses if-else statements to perform the appropriate operation.
CODING:
import [Link];
public class ques5 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Input two numbers
[Link]("Enter first number: ");
double num1 = [Link]();
[Link]("Enter second number: ");
double num2 = [Link]();
// Input operator
[Link]("Enter an operator (+, -, *, /): ");
char operator = [Link]().charAt(0);
// Perform operation using if-else
double result;
if (operator == '+') {
result = num1 + num2;
[Link]("Result = " + result);
} else if (operator == '-') {
result = num1 - num2;
[Link]("Result = " + result);
} else if (operator == '*') {
result = num1 * num2;
[Link]("Result = " + result);
} else if (operator == '/') {
if (num2 != 0) { // check for divide by zero
result = num1 / num2;
[Link]("Result = " + result);
} else {
[Link]("Error: Division by zero is not allowed!");
}
} else {
[Link]("Invalid operator! Please use +, -, *, or /");
}
[Link]();
}
}
OUTPUT:
6. Loop – Multiplication Table
Task:
Ask the user for a number and print its multiplication table from 1 to 10 using a for loop.
CODING:
import [Link];
public class ques6 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Input a number
[Link]("Enter a number: ");
int num = [Link]();
// Print multiplication table using for loop
[Link]("Multiplication Table of " + num + ":");
for (int i = 1; i <= 10; i++) {
[Link](num + " x " + i + " = " + (num * i));
}
[Link]();
}
}
OUTPUT:
7. Loop with Conditional – Sum of Even Numbers
Task:
Write a program that uses a while loop to find the sum of all even numbers between 1 and 50.
CODING:
public class ques7 {
public static void main(String[] args) {
int i = 1;
int sum = 0;
// while loop from 1 to 50
while (i <= 50) {
if (i % 2 == 0) { // check if number is even
sum += i;
}
i++;
}
[Link]("Sum of even numbers between 1 and 50 is: " + sum);
}
}
OUTPUT:
8. Arrays – Store and Print Marks
Task:
Write a program that:
Reads marks of 5 students into an integer array.
Prints each student’s marks.
CODING:
import [Link];
public class ques8 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Declare an array of size 5
int[] marks = new int[5];
// Input marks of 5 students
[Link]("Enter marks of 5 students:");
for (int i = 0; i < 5; i++) {
[Link]("Student " + (i + 1) + ": ");
marks[i] = [Link]();
}
// Print marks of each student
[Link]("\nMarks of students:");
for (int i = 0; i < 5; i++) {
[Link]("Student " + (i + 1) + ": " + marks[i]);
}
[Link]();
}
}
OUTPUT:
9. Arrays and Loops – Find Maximum
Task:
Write a program to read 10 integers into an array and find the maximum value using a loop.
CODING:
import [Link];
public class ques9 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Array of size 10
int[] numbers = new int[10];
// Input 10 integers
[Link]("Enter 10 integers:");
for (int i = 0; i < 10; i++) {
[Link]("Number " + (i + 1) + ": ");
numbers[i] = [Link]();
}
// Assume first number is maximum
int max = numbers[0];
// Find maximum using loop
for (int i = 1; i < 10; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
// Print maximum value
[Link]("\nThe maximum value is: " + max);
[Link]();
}
}
OUTPUT:
10. Mini Project – Average and Grade
Task:
Write a program that:
Reads marks of 5 subjects into an array.
Calculates the average marks.
Uses if-else to assign grades:
o >= 90: A
o >= 75: B
o >= 50: C
o Else: Fail
CODING:
import [Link];
public class ques10 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Array for 5 subjects
int[] marks = new int[5];
int sum = 0;
// Input marks
[Link]("Enter marks of 5 subjects:");
for (int i = 0; i < 5; i++) {
[Link]("Subject " + (i + 1) + ": ");
marks[i] = [Link]();
sum += marks[i];
}
// Calculate average
double average = sum / 5.0;
// Print average
[Link]("\nAverage Marks = " + average);
// Assign grade using if-else
if (average >= 90) {
[Link]("Grade: A");
} else if (average >= 75) {
[Link]("Grade: B");
} else if (average >= 50) {
[Link]("Grade: C");
} else {
[Link]("Grade: Fail");
}
[Link]();
}
}
OUTPUT: