KEMBAR78
Practical Home Assignment 1 | PDF | Fahrenheit | Integer (Computer Science)
0% found this document useful (0 votes)
12 views12 pages

Practical Home Assignment 1

The document contains programming exercises for a Core Java course, focusing on various fundamental concepts such as variable declaration, data type conversion, arithmetic operations, logical operators, control statements, loops, and arrays. Each exercise includes a task description, sample code, and expected output. The exercises are designed to help students practice and reinforce their understanding of Java programming.

Uploaded by

Sneha Chaudhary
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)
12 views12 pages

Practical Home Assignment 1

The document contains programming exercises for a Core Java course, focusing on various fundamental concepts such as variable declaration, data type conversion, arithmetic operations, logical operators, control statements, loops, and arrays. Each exercise includes a task description, sample code, and expected output. The exercises are designed to help students practice and reinforce their understanding of Java programming.

Uploaded by

Sneha Chaudhary
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
You are on page 1/ 12

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


System.out.println("Student Name: " + studentName);
System.out.println("Roll Number: " + studentRollNumber);
System.out.println("Marks in Subject 1: " + marksSubject1);
System.out.println("Marks in Subject 2: " + marksSubject2);
System.out.println("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


System.out.println("Temperature in Celsius: " + celsius + "°C");
System.out.println("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 java.util.Scanner; // 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(System.in);

// Prompt the user to enter the first integer


System.out.print("Enter the first integer: ");
int num1 = input.nextInt(); // Read the first integer

// Prompt the user to enter the second integer


System.out.print("Enter the second integer: ");
int num2 = input.nextInt(); // 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 = Double.NaN; // 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


System.out.println("\nResults:");
System.out.println("Addition: " + num1 + " + " + num2 + " = " + sum);
System.out.println("Subtraction: " + num1 + " - " + num2 + " = " + difference);
System.out.println("Multiplication: " + num1 + " * " + num2 + " = " + product);

if (num2 != 0) {
System.out.println("Division: " + num1 + " / " + num2 + " = " + quotient);
System.out.println("Modulus: " + num1 + " % " + num2 + " = " + modulus);
} else {
System.out.println("Division: Cannot divide by zero.");
System.out.println("Modulus: Cannot perform modulus with zero.");
}
// Close the scanner to release system resources
input.close();
}
}

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 java.util.Scanner;

public class ques4 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input age
System.out.print("Enter your age: ");
int age = sc.nextInt();

// Input citizenship status


System.out.print("Are you a citizen? (true/false): ");
boolean isCitizen = sc.nextBoolean();

// Input NRI status


System.out.print("Are you an NRI (Non-Resident Indian)? (true/false): ");
boolean isNRI = sc.nextBoolean();

// Check eligibility using && and ||


if ((age >= 18 && isCitizen) || (age >= 18 && isNRI)) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are NOT eligible to vote.");
}

sc.close();
}
}

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 java.util.Scanner;

public class ques5 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input two numbers


System.out.print("Enter first number: ");
double num1 = sc.nextDouble();

System.out.print("Enter second number: ");


double num2 = sc.nextDouble();

// Input operator
System.out.print("Enter an operator (+, -, *, /): ");
char operator = sc.next().charAt(0);

// Perform operation using if-else


double result;

if (operator == '+') {
result = num1 + num2;
System.out.println("Result = " + result);
} else if (operator == '-') {
result = num1 - num2;
System.out.println("Result = " + result);
} else if (operator == '*') {
result = num1 * num2;
System.out.println("Result = " + result);
} else if (operator == '/') {
if (num2 != 0) { // check for divide by zero
result = num1 / num2;
System.out.println("Result = " + result);
} else {
System.out.println("Error: Division by zero is not allowed!");
}
} else {
System.out.println("Invalid operator! Please use +, -, *, or /");
}

sc.close();
}
}

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 java.util.Scanner;

public class ques6 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input a number
System.out.print("Enter a number: ");
int num = sc.nextInt();

// Print multiplication table using for loop


System.out.println("Multiplication Table of " + num + ":");
for (int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}

sc.close();
}
}

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++;
}

System.out.println("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 java.util.Scanner;

public class ques8 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Declare an array of size 5


int[] marks = new int[5];

// Input marks of 5 students


System.out.println("Enter marks of 5 students:");
for (int i = 0; i < 5; i++) {
System.out.print("Student " + (i + 1) + ": ");
marks[i] = sc.nextInt();
}

// Print marks of each student


System.out.println("\nMarks of students:");
for (int i = 0; i < 5; i++) {
System.out.println("Student " + (i + 1) + ": " + marks[i]);
}

sc.close();
}
}

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 java.util.Scanner;

public class ques9 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Array of size 10
int[] numbers = new int[10];

// Input 10 integers
System.out.println("Enter 10 integers:");
for (int i = 0; i < 10; i++) {
System.out.print("Number " + (i + 1) + ": ");
numbers[i] = sc.nextInt();
}

// 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


System.out.println("\nThe maximum value is: " + max);

sc.close();
}
}

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 java.util.Scanner;

public class ques10 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Array for 5 subjects


int[] marks = new int[5];
int sum = 0;

// Input marks
System.out.println("Enter marks of 5 subjects:");
for (int i = 0; i < 5; i++) {
System.out.print("Subject " + (i + 1) + ": ");
marks[i] = sc.nextInt();
sum += marks[i];
}
// Calculate average
double average = sum / 5.0;

// Print average
System.out.println("\nAverage Marks = " + average);

// Assign grade using if-else


if (average >= 90) {
System.out.println("Grade: A");
} else if (average >= 75) {
System.out.println("Grade: B");
} else if (average >= 50) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: Fail");
}

sc.close();
}
}

OUTPUT:

You might also like