Q1: Write a Java method to calculate the area of a circle.
Hint: The name of the method is: circlemethod. The program accepts only integer values
from the user.
Code:
public class CircleArea {
public static void circlemethod(int radius) {
double area = [Link] * radius * radius;
[Link]("Area: " + area);
}
public static void main(String[] args) {
[Link] sc = new [Link]([Link]);
[Link]("Enter radius (integer): ");
int r = [Link]();
circlemethod(r);
}
}
Output:
Enter radius (integer): 5
Area: 78.5
Q2: Write a program to generate 10 random elements in a double array.
Code:
public class RandomArray {
public static void main(String[] args) {
double[] arr = new double[10];
for (int i = 0; i < [Link]; i++) {
arr[i] = [Link]();
[Link](arr[i]);
}
}
}
Output:
Random 10 double values between 0.0 and 1.0 (output will vary).
Q3: Write a Java program to print the summation (addition), subtraction, multiplication,
division, and modulo of the two numbers 130 and 24.
Code:
public class Arithmetic {
public static void main(String[] args) {
int a = 130, b = 24;
[Link]("Sum: " + (a + b));
[Link]("Subtraction: " + (a - b));
[Link]("Multiplication: " + (a * b));
[Link]("Division: " + (a / b));
[Link]("Modulo: " + (a % b));
}
}
Output:
Sum: 154
Subtraction: 106
Multiplication: 3120
Division: 5
Modulo: 10
Q4: Write a Java program that asks the user to input a positive number n and prints the cubic
series up to the given number using a while statement:
Example Output: 1, 8, 27, 64, ...
Code:
public class CubicSeries {
public static void main(String[] args) {
[Link] sc = new [Link]([Link]);
[Link]("Enter a positive number: ");
int n = [Link]();
int i = 1;
while (i <= n) {
[Link]((i * i * i) + " ");
i++;
}
}
}
Output:
Enter a positive number: 4
1 8 27 64
Q5: Write a Java program that asks the user to input the temperature and prints one of the
following using if statement:
- Print "hot" if the weather is greater than 30.
- Print "cold" if the weather is less than 15.
- Print "warm" otherwise.
Code:
public class WeatherCheck {
public static void main(String[] args) {
[Link] sc = new [Link]([Link]);
[Link]("Enter the temperature: ");
int temp = [Link]();
if (temp > 30)
[Link]("hot");
else if (temp < 15)
[Link]("cold");
else
[Link]("warm");
}
}
Output:
Enter the temperature: 32
hot
Q6: Write a Java program that asks the user to input a number n and prints the sum and
average of the given number using a while statement.
Code:
public class SumAverage {
public static void main(String[] args) {
[Link] sc = new [Link]([Link]);
[Link]("Enter a number: ");
int n = [Link](), sum = 0, i = 1;
while (i <= n) {
sum += i;
i++;
}
double avg = (double) sum / n;
[Link]("Sum: " + sum);
[Link]("Average: " + avg);
}
}
Output:
Enter a number: 5
Sum: 15
Average: 3.0
Q7: Write a program to check if the entered number is odd or not.
Code:
public class OddCheck {
public static void main(String[] args) {
[Link] sc = new [Link]([Link]);
[Link]("Enter a number: ");
int n = [Link]();
if (n % 2 != 0)
[Link]("The number is odd.");
else
[Link]("The number is even.");
}
}
Output:
Enter a number: 3
The number is odd.
Q8: Write a program to print all the numbers from 10 to 20 using a for statement.
Code:
public class PrintRange {
public static void main(String[] args) {
for (int i = 10; i <= 20; i++) {
[Link](i);
}
}
}
Output:
10
11
12
13
14
15
16
17
18
19
20
Q9: Write a program in Java to print "Computer Science" five times using a do-while
statement.
Code:
public class PrintCS {
public static void main(String[] args) {
int i = 0;
do {
[Link]("Computer Science");
i++;
} while (i < 5);
}
}
Output:
Computer Science
(printed 5 times)
Q10: Write a program in Java to sum the first 10 natural numbers using a for statement.
Code:
public class SumNatural {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
[Link]("Sum: " + sum);
}
}
Output:
Sum: 55
Q11: Write a Java program that uses a for loop to print the multiples of 5.
Code:
public class MultiplesOf5 {
public static void main(String[] args) {
for (int i = 5; i <= 100; i += 5) {
[Link](i);
}
}
}
Output:
10
15
...
100 (all multiples of 5 up to 100)
Q12: Write a Java program to get a number from the user and print whether it is divisible by 5
or not.
Code:
public class DivisibleBy5 {
public static void main(String[] args) {
[Link] sc = new [Link]([Link]);
[Link]("Enter a number: ");
int n = [Link]();
if (n % 5 == 0)
[Link]("Divisible by 5");
else
[Link]("Not divisible by 5");
}
}
Output:
Enter a number: 15
Divisible by 5
Q13: Write a program to create two variables X and Y as positive integers and check if X is
divisible by Y or not.
Code:
public class DivisibleCheck {
public static void main(String[] args) {
[Link] sc = new [Link]([Link]);
[Link]("Enter X: ");
int x = [Link]();
[Link]("Enter Y: ");
int y = [Link]();
if (x % y == 0)
[Link]("X is divisible by Y");
else
[Link]("X is not divisible by Y");
}
}
Output:
Enter X: 10
Enter Y: 2
X is divisible by Y
Q14: Write a program to demonstrate input statement and print the following student details
two times:
- Student Name
- Student ID
Code:
public class StudentDetails {
public static void main(String[] args) {
[Link] sc = new [Link]([Link]);
for (int i = 0; i < 2; i++) {
[Link]("Enter Student Name: ");
String name = [Link]();
[Link]("Enter Student ID: ");
int id = [Link]();
[Link]("Student-Name: " + name);
[Link]("Student-ID: " + id);
}
}
}
Output:
Enter Student Name: Ali
Enter Student ID: 123
Student-Name: Ali
Student-ID: 123
(repeated twice)
Q15: Write a Java program that allows the user to:
A. Enter a number
B. Check if the given integer is even and divisible by 8
C. Display the multiplication table of that integer using a do-while statement.
Code:
public class DivAndTable {
public static void main(String[] args) {
[Link] sc = new [Link]([Link]);
[Link]("Enter a number: ");
int n = [Link]();
if (n % 2 == 0 && n % 8 == 0)
[Link]("Even and divisible by 8");
else
[Link]("Not divisible by 8 or not even");
int i = 1;
do {
[Link](n + " x " + i + " = " + (n * i));
i++;
} while (i <= 10);
}
}
Output:
Enter a number: 16
Even and divisible by 8
16 x 1 = 16
...
16 x 10 = 160
Q16: Write a program in Java to print a pattern like a right-angle triangle using stars * and a
for statement.
Example Pattern:
**
***
****
*****
Code:
public class StarPattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}
Output:
**
***
****
*****