Core Java Programming: From Beginner to Project Development

Lesson 2.7: Practice Programs

Introduction

Congratulations! You have completed all the theoretical topics of Module 2. Now it’s time to apply your knowledge by writing Java programs.

In this lesson, you will practice using:

  • Variables

  • Data Types

  • Type Casting

  • Operators

  • Scanner Class

These programs will help you build confidence before moving on to decision-making statements (if, if-else, and switch) in the next module.

Important: Try to solve each problem on your own before checking the solution.


Learning Objectives

After completing this lesson, you will be able to:

  • Declare and initialize variables.

  • Accept user input using Scanner.

  • Perform arithmetic calculations.

  • Apply operators correctly.

  • Use different data types effectively.

  • Improve programming and logical thinking skills.


Program 1: Display Student Information

Problem Statement

Write a Java program to accept and display the following student details:

  • Name

  • Roll Number

  • Department

  • Semester

Sample Output

------ Student Details ------
Name       : Rahul Kumar
Roll Number: 101
Department : Computer Science
Semester   : 3

Program 2: Addition of Two Numbers

Problem Statement

Accept two integers from the user and display their sum.

Sample Output

Enter First Number : 10
Enter Second Number: 20

Sum = 30

Program 3: Simple Calculator

Problem Statement

Accept two numbers and display:

  • Addition

  • Subtraction

  • Multiplication

  • Division

  • Modulus

Sample Output

Addition       : 30
Subtraction    : 10
Multiplication : 200
Division       : 2
Remainder      : 0

Program 4: Area of a Rectangle

Formula

Area = Length × Width

Sample Output

Enter Length : 12
Enter Width  : 5

Area = 60

Program 5: Area of a Circle

Formula

Area = π × r × r

Use:

double pi = 3.14159;

Sample Output

Enter Radius : 7

Area = 153.94

Program 6: Calculate Simple Interest

Formula

SI = (P × R × T) / 100

Where:

  • P = Principal Amount

  • R = Rate of Interest

  • T = Time


Program 7: Temperature Converter

Convert Celsius to Fahrenheit.

Formula

F = (C × 9 / 5) + 32

Program 8: Swap Two Numbers (Using Third Variable)

Example

Before Swap

A = 10
B = 20

After Swap

A = 20
B = 10

Program 9: Swap Two Numbers (Without Third Variable)

Use arithmetic operators only.

Example:

A = A + B
B = A - B
A = A - B

Program 10: ASCII Value of a Character

Accept a character and display its ASCII (Unicode) value.

Sample Output

Enter Character : A

ASCII Value = 65

Program 11: Average of Five Numbers

Accept five marks and calculate:

  • Total Marks

  • Average

Sample Output

Total = 420

Average = 84.0

Program 12: Square and Cube

Accept a number and display:

  • Square

  • Cube

Example

Number = 5

Square = 25

Cube = 125

Program 13: Convert Days

Accept total days and convert into:

  • Years

  • Months

  • Days

(Hint: Assume 365 days = 1 year and 30 days = 1 month.)


Program 14: Currency Converter

Convert Indian Rupees into US Dollars.

(Hint: Assume 1 USD = ₹85.)

Sample Output

Enter Amount in INR : 850

USD = 10

Program 15: Employee Salary Details

Accept:

  • Employee Name

  • Basic Salary

Calculate:

  • HRA = 20%

  • DA = 10%

  • Gross Salary


Mini Challenge

Create a Student Result Program that accepts:

  • Name

  • Roll Number

  • Marks in 5 Subjects

Display:

  • Total Marks

  • Average

(Percentage and grade will be added after learning decision-making statements.)


Coding Tips

Before writing any program:

  1. Read the problem carefully.

  2. Identify the required inputs.

  3. Decide which data types to use.

  4. Write the algorithm.

  5. Write the Java code.

  6. Test the program with different inputs.


Module 2 Coding Challenge

Without referring to previous programs, create a Student Registration Program that accepts:

  • Name

  • Roll Number

  • Age

  • Department

  • Semester

  • Mobile Number

Display all the details in a professional format.


Practice Questions

  1. Which class is used to take user input in Java?

  2. Which method reads an integer value?

  3. What is the difference between next() and nextLine()?

  4. What is type casting?

  5. Which operator calculates the remainder?

  6. What is the purpose of variables?

  7. Name the eight primitive data types.

  8. What is the difference between primitive and non-primitive data types?


Assignment

Complete all 15 programs given in this lesson.

After completing them:

  • Save all programs in a folder named Module 2 Practice.

  • Name each file appropriately (e.g., Addition.java, Calculator.java, AreaCircle.java).

  • Test every program with different inputs.

  • Ensure your code is properly formatted and commented where necessary.


Lesson Summary

Excellent work!

You have now practiced the core concepts of Module 2, including variables, data types, operators, type casting, and user input.

By completing these practice programs, you have developed the skills required to create interactive Java applications.

In the next lesson, you will complete a Module 2 Assignment to test your understanding before moving on to Decision Making Statements (if, if-else, nested if, and switch).

Next Lesson: Lesson 2.8 – Module Assignment

Scroll to Top