Java Programming Solutions
1. WAP to enter two numbers using command line argument and calculate their Sum and
multiplication.
public class SumAndMultiply {
public static void main(String[] args) {
int num1 = [Link](args[0]);
int num2 = [Link](args[1]);
[Link]("Sum: " + (num1 + num2));
[Link]("Multiplication: " + (num1 * num2));
2. WAP to print prime numbers up to 50.
public class PrimeNumbers {
public static void main(String[] args) {
for (int i = 2; i <= 50; i++) {
boolean isPrime = true;
for (int j = 2; j <= [Link](i); j++) {
if (i % j == 0) {
isPrime = false;
break;
if (isPrime) [Link](i + " ");
}
3. WAP to print Fibonacci series.
public class FibonacciSeries {
public static void main(String[] args) {
int n = 10; // or any other number of terms
int a = 0, b = 1;
[Link](a + " " + b);
for (int i = 2; i < n; i++) {
int next = a + b;
[Link](" " + next);
a = b;
b = next;
4. WAP to find out the greatest of three numbers.
import [Link];
public class GreatestOfThree {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter three numbers: ");
int num1 = [Link]();
int num2 = [Link]();
int num3 = [Link]();
int max = [Link](num1, [Link](num2, num3));
[Link]("Greatest number: " + max);
5. WAP to print first twenty even numbers.
public class FirstTwentyEvenNumbers {
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
[Link]((i * 2) + " ");
6. WAP to Print: * * * * * * * * * *
public class AsteriskPattern {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
[Link]("* ");
7. WAP to check whether a given year is leap year or not.
import [Link];
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
[Link](year + " is a leap year.");
} else {
[Link](year + " is not a leap year.");
8. WAP to find out simple interest.
import [Link];
public class SimpleInterest {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter principal, rate, and time: ");
double principal = [Link]();
double rate = [Link]();
double time = [Link]();
double interest = (principal * rate * time) / 100;
[Link]("Simple Interest: " + interest);
9. WAP to show working of inner class using instance of outer class.
public class OuterClass {
class InnerClass {
void display() {
[Link]("Inside Inner Class.");
public static void main(String[] args) {
OuterClass outer = new OuterClass();
[Link] inner = [Link] InnerClass();
[Link]();
10. WAP to show the overloading of function.
public class FunctionOverloading {
void show(int a) {
[Link]("Integer: " + a);
void show(String a) {
[Link]("String: " + a);
public static void main(String[] args) {
FunctionOverloading obj = new FunctionOverloading();
[Link](5);
[Link]("Hello");