0% found this document useful (0 votes)
18 views11 pages

Java Programming Practice Exercises

The document contains ten Java programming exercises, each demonstrating different concepts such as user input, basic arithmetic operations, control structures, object-oriented programming, and loops. Examples include a welcome message program, a simple calculator, a prime number checker, and a string reversal program. Each exercise includes code snippets and explanations of the functionality.

Uploaded by

sazibshaha3
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)
18 views11 pages

Java Programming Practice Exercises

The document contains ten Java programming exercises, each demonstrating different concepts such as user input, basic arithmetic operations, control structures, object-oriented programming, and loops. Examples include a welcome message program, a simple calculator, a prime number checker, and a string reversal program. Each exercise includes code snippets and explanations of the functionality.

Uploaded by

sazibshaha3
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

Practice

1. Input and Output Practice

Program: Take user input for name and age, then print a welcome message.

import [Link];

public class WelcomeUser {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

[Link]("Enter your name: ");


String name = [Link]();

[Link]("Enter your age: ");


int age = [Link]();

[Link]("Welcome, " + name + "! You are " + age + " years old.");
}
}
2. Calculator (Basic Arithmetic Operations)

Program: Perform addition, subtraction, multiplication, and division on two numbers.

import [Link];

public class SimpleCalculator {


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

[Link]("Enter first number: ");


double num1 = [Link]();

[Link]("Enter second number: ");


double num2 = [Link]();

[Link]("Addition: " + (num1 + num2));


[Link]("Subtraction: " + (num1 - num2));
[Link]("Multiplication: " + (num1 * num2));

if (num2 != 0) {
[Link]("Division: " + (num1 / num2));
} else {
[Link]("Cannot divide by zero.");
}
}
}
3. Check Even or Odd

Program: Check whether a number is even or odd using if-else.

import [Link];

public class EvenOddChecker {


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

[Link]("Enter a number: ");


int number = [Link]();

if (number % 2 == 0) {
[Link](number + " is Even.");
} else {
[Link](number + " is Odd.");
}
}
}
4. Multiplication Table

Program: Print the multiplication table of a given number using a loop.

import [Link];

public class MultiplicationTable {


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

[Link]("Enter a number: ");


int number = [Link]();

[Link]("Multiplication Table of " + number);


for (int i = 1; i <= 10; i++) {
[Link](number + " x " + i + " = " + (number * i));
}
}
}
5. Simple Object-Oriented Program

Program: Define a Student class and create an object with name and roll.

class Student {
String name;
int roll;

void setDetails(String n, int r) {


name = n;
roll = r;
}

void displayDetails() {
[Link]("Name: " + name);
[Link]("Roll: " + roll);
}
}

public class StudentTest {


public static void main(String[] args) {
Student s1 = new Student();
[Link]("Riya", 101);
[Link]();
}
}
6. Guess the Number Game

Concepts: random numbers, loops, user interaction, conditionals

import [Link];
import [Link];

public class GuessNumber {


public static void main(String[] args) {
Random rand = new Random();
int secret = [Link](100) + 1; // 1 to 100
Scanner sc = new Scanner([Link]);
int attempts = 0;
[Link]("Guess the number between 1 and 100. You have 7 attempts.");

while (attempts < 7) {


[Link]("Enter your guess: ");
int guess = [Link]();
attempts++;

if (guess == secret) {
[Link]("Correct! You guessed it in " + attempts + " attempts.");
return;
} else if (guess < secret) {
[Link]("Too low.");
} else {
[Link]("Too high.");
}
}
[Link]("Game over. The number was: " + secret);
}
}

7. Prime Number Checker

Program: Check whether a number is prime.

import [Link];

public class PrimeCheck {


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

[Link]("Enter a number: ");


int num = [Link]();

boolean isPrime = true;


if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
[Link](num + " is Prime.");
} else {
[Link](num + " is Not Prime.");
}
}
}

8. Prime Number Checker & List in a Range

Concepts: methods, loops, boolean logic

import [Link];

public class PrimeRange {


public static boolean isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) return false;
}
return true;
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter lower bound: ");
int low = [Link]();
[Link]("Enter upper bound: ");
int high = [Link]();

[Link]("Primes between " + low + " and " + high + ":");


for (int i = low; i <= high; i++) {
if (isPrime(i)) {
[Link](i + " ");
}
}
[Link]();
}
}
9. Factorial of a Number (Using Loop)

Program: Calculate factorial of a given number.

import [Link];

public class Factorial {


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

[Link]("Enter a number: ");


int num = [Link]();

long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}

[Link]("Factorial of " + num + " is " + fact);


}
}
10. Reverse a String

Program: Take a string input and print it in reverse.

import [Link];

public class ReverseString {


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

[Link]("Enter a string: ");


String str = [Link]();

String reversed = "";


for (int i = [Link]() - 1; i >= 0; i--) {
reversed += [Link](i);
}

[Link]("Reversed string: " + reversed);


}
}

You might also like