0% found this document useful (0 votes)
5 views6 pages

Java Programs With Loops Patterns Slabs

The document contains a series of Java programming exercises designed for Class 9 and 10 students, covering basic concepts such as printing text, arithmetic operations, loops, and conditional statements. Each exercise includes a description, code implementation, expected output, and variable data types. The programs demonstrate fundamental programming skills and logic in Java.

Uploaded by

aadig2626
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Java Programs With Loops Patterns Slabs

The document contains a series of Java programming exercises designed for Class 9 and 10 students, covering basic concepts such as printing text, arithmetic operations, loops, and conditional statements. Each exercise includes a description, code implementation, expected output, and variable data types. The programs demonstrate fundamental programming skills and logic in Java.

Uploaded by

aadig2626
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Programs Project (Class 9 & 10)

Generated on: 2025-06-14

1. Print Hello World


Q1. Write a Java program to print 'Hello, World!' on the screen.

Description: This program prints 'Hello, World!' to the screen.


public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Output:

Hello, World!

VDT:

Variable | Data Type | Description

---------|------------|-------------

args | String[] | Command-line arguments

2. Add Two Numbers


Q2. Write a Java program to add two numbers and display the result.

Description: This program adds two numbers and prints the result.
public class AddNumbers {
public static void main(String[] args) {
int a = 8, b = 12;
int sum = a + b;
System.out.println("Sum: " + sum);
}
}

Output:

Sum: 20

VDT:

Variable | Data Type | Description

a | int | First number


b | int | Second number

sum | int | Sum of a and b

3. Input and Display Name


Q3. Write a Java program to input your name and print it.

Description: This program takes user input and displays it.


import java.util.Scanner;
public class InputName {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
}
}

Output:

Enter your name: Ayush

Hello, Ayush

VDT:

Variable | Data Type | Description

sc | Scanner | Scanner object

name | String | Stores user's name

4. For Loop (1 to 5)
Q4. Write a Java program using a for loop to print numbers from 1 to 5.

Description: Demonstrates use of a for loop.


public class ForLoop {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}

Output:

2
3

VDT:

Variable | Data Type | Description

i | int | Loop counter

5. While Loop (1 to 5)
Q5. Write a Java program using a while loop to print numbers from 1 to 5.

Description: Demonstrates use of a while loop.


public class WhileLoop {
public static void main(String[] args) {
int i = 1;
while(i <= 5) {
System.out.println(i);
i++;
}
}
}

Output:

VDT:

Variable | Data Type | Description

i | int | Loop counter

6. Do-While Loop (1 to 5)
Q6. Write a Java program using a do-while loop to print numbers from 1 to 5.

Description: Demonstrates use of a do-while loop.


public class DoWhileLoop {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while(i <= 5);
}
}

Output:

VDT:

Variable | Data Type | Description

i | int | Loop counter

7. Star Pattern
Q7. Write a Java program to print a right-angle triangle star pattern.

Description: This program prints a triangle pattern using nested loops.


public class StarPattern {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

Output:

**

***

****
*****

VDT:

Variable | Data Type | Description

i | int | Outer loop counter

j | int | Inner loop counter

8. Multiplication Table
Q8. Write a Java program to display the multiplication table of 5.

Description: Uses a loop to display a multiplication table.


public class MultiplicationTable {
public static void main(String[] args) {
int num = 5;
for(int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}
}
}

Output:

5x1=5

5 x 2 = 10

...

5 x 10 = 50

VDT:

Variable | Data Type | Description

num | int | Number to multiply

i | int | Loop counter

9. Square of a Number
Q9. Write a Java program to find the square of a number.

Description: Uses basic mathematical operation to find the square.


public class Square {
public static void main(String[] args) {
int num = 6;
int square = num * num;
System.out.println("Square: " + square);
}
}

Output:

Square: 36

VDT:

Variable | Data Type | Description

num | int | Input number

square | int | Square of the number

10. Slab-Based Salary Calculation


Q10. Write a Java program to calculate salary based on slabs: if salary > 10000, 10% bonus, else

5%.

Description: Demonstrates conditional logic using if-else.


public class SalaryBonus {
public static void main(String[] args) {
double salary = 12000;
double bonus;
if(salary > 10000) {
bonus = salary * 0.10;
} else {
bonus = salary * 0.05;
}
System.out.println("Bonus: " + bonus);
}
}

Output:

Bonus: 1200.0

VDT:

Variable | Data Type | Description

salary | double | Base salary

bonus | double | Calculated bonus

You might also like