0% found this document useful (0 votes)
20 views

Java Programming Solutions

Hi

Uploaded by

Shubhanshu
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)
20 views

Java Programming Solutions

Hi

Uploaded by

Shubhanshu
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 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 = Integer.parseInt(args[0]);

int num2 = Integer.parseInt(args[1]);

System.out.println("Sum: " + (num1 + num2));

System.out.println("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 <= Math.sqrt(i); j++) {

if (i % j == 0) {

isPrime = false;

break;

if (isPrime) System.out.print(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;

System.out.print(a + " " + b);

for (int i = 2; i < n; i++) {

int next = a + b;

System.out.print(" " + next);

a = b;

b = next;

4. WAP to find out the greatest of three numbers.

import java.util.Scanner;

public class GreatestOfThree {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter three numbers: ");

int num1 = scanner.nextInt();

int num2 = scanner.nextInt();

int num3 = scanner.nextInt();

int max = Math.max(num1, Math.max(num2, num3));

System.out.println("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++) {

System.out.print((i * 2) + " ");

6. WAP to Print: * * * * * * * * * *

public class AsteriskPattern {

public static void main(String[] args) {


for (int i = 0; i < 10; i++) {

System.out.print("* ");

7. WAP to check whether a given year is leap year or not.

import java.util.Scanner;

public class LeapYearChecker {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a year: ");

int year = scanner.nextInt();

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

System.out.println(year + " is a leap year.");

} else {

System.out.println(year + " is not a leap year.");

8. WAP to find out simple interest.


import java.util.Scanner;

public class SimpleInterest {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter principal, rate, and time: ");

double principal = scanner.nextDouble();

double rate = scanner.nextDouble();

double time = scanner.nextDouble();

double interest = (principal * rate * time) / 100;

System.out.println("Simple Interest: " + interest);

9. WAP to show working of inner class using instance of outer class.

public class OuterClass {

class InnerClass {

void display() {

System.out.println("Inside Inner Class.");

public static void main(String[] args) {

OuterClass outer = new OuterClass();

OuterClass.InnerClass inner = outer.new InnerClass();


inner.display();

10. WAP to show the overloading of function.

public class FunctionOverloading {

void show(int a) {

System.out.println("Integer: " + a);

void show(String a) {

System.out.println("String: " + a);

public static void main(String[] args) {

FunctionOverloading obj = new FunctionOverloading();

obj.show(5);

obj.show("Hello");

You might also like