📢 Join My WhatsApp Channel JavaWithSumit

Get Daily ICSE Java Notes, Programs & Exam Tips 🚀

👉 Join Now

ICSE Java Programs Part-I | Basic Input Output & Conditional Programs with Solutions

Input/ Output Based Programs



Question 1: WAP in Java to accept different type of data as input from Scanner class and print.

Solution: 


import java.util.Scanner; class DifferentDataTypes { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter an Integer: "); int i = sc.nextInt(); System.out.print("Enter a Float: "); float f = sc.nextFloat(); System.out.print("Enter a Double: "); double d = sc.nextDouble(); System.out.print("Enter a Long: "); long l = sc.nextLong(); System.out.print("Enter a Byte: "); byte b = sc.nextByte(); System.out.print("Enter a Short: "); short s = sc.nextShort(); System.out.print("Enter a Character: "); char ch = sc.next().charAt(0); sc.nextLine(); // Clear buffer System.out.print("Enter a String: "); String str = sc.nextLine(); System.out.print("Enter a Boolean (true/false): "); boolean bool = sc.nextBoolean(); System.out.println("\n----- OUTPUT -----"); System.out.println("Integer : " + i); System.out.println("Float : " + f); System.out.println("Double : " + d); System.out.println("Long : " + l); System.out.println("Byte : " + b); System.out.println("Short : " + s); System.out.println("Char : " + ch); System.out.println("String : " + str); System.out.println("Boolean : " + bool); sc.close(); } }

Output:
----- OUTPUT ----- Integer : 100 Float : 12.5 Double : 12345.6789 Long : 9876543210 Byte : 120 Short : 32000 Char : A String : Java Programming Boolean : true

Question 2: WAP to Input length and breadth of a rectangle and calculate area and perimeter.

Solution: 
import java.util.Scanner;
class Rectangle
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Length: ");
        double length = sc.nextDouble();
        System.out.print("Enter Breadth: ");
        double breadth = sc.nextDouble();
        double area = length * breadth;
        double perimeter = 2 * (length + breadth);
        System.out.println("Area = " + area);
        System.out.println("Perimeter = " + perimeter);
    }
}
Input:
Enter Length: 10 Enter Breadth: 50

Output:
Area = 50.0 Perimeter = 30.0

Question 3: WAP to Input radius of a circle and calculate area and circumference.

Solution: 
import java.util.Scanner;

class Circle
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter Radius: ");
        double r = sc.nextDouble();

        double area = Math.PI * r * r;
        double circumference = 2 * Math.PI * r;

        System.out.println("Area = " + area);
        System.out.println("Circumference = " + circumference);
    }
}
Input:
Enter Radius: 7

Output:
Area = 153.93804002589985 Circumference = 43.982297150257104

Question 4: WAP to Input principal, rate, and time and calculate simple interest.

Solution: 
import java.util.Scanner;

class SimpleInterest
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter Principal Amount: ");
        double p = sc.nextDouble();

        System.out.print("Enter Rate of Interest: ");
        double r = sc.nextDouble();

        System.out.print("Enter Time (in years): ");
        double t = sc.nextDouble();

        double si = (p * r * t) / 100;

        System.out.println("Simple Interest = " + si);
    }
}
Input:
Enter Principal Amount: 10000 Enter Rate of Interest: 5 Enter Time (in years): 2

Output:
Simple Interest = 1000.0

Question 5: WAP to Input a temperature in Celsius and convert it to Fahrenheit.

Solution: 
import java.util.Scanner;

class CelsiusToFahrenheit
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter Temperature in Celsius: ");
        double c = sc.nextDouble();

        double f = (9.0 / 5.0) * c + 32;

        System.out.println("Temperature in Fahrenheit = " + f);
    }
}
Input:
Enter Temperature in Celsius: 25

Output:
Temperature in Fahrenheit = 77.0

Question 6: WAP to Input a number and display its square and cube.

Solution: 
import java.util.Scanner;

public class NumberPowers {
    public static void main(String[] args) {
        // Create a Scanner object to read user input
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter a number: ");
        double number = input.nextDouble();
        
        // Calculate square and cube
        double square = number * number;
        double cube = number * number * number;
        
        // Display the results
        System.out.println("Square of " + number + " is: " + square);
        System.out.println("Cube of " + number + " is: " + cube);
        
        // Close the scanner resource
        input.close();
    }
}

Question 7: WAP to Input marks of five subjects and calculate total and percentage.

Solution: 
import java.util.Scanner;

class Marks
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter marks of Subject 1: ");
        int m1 = sc.nextInt();

        System.out.print("Enter marks of Subject 2: ");
        int m2 = sc.nextInt();

        System.out.print("Enter marks of Subject 3: ");
        int m3 = sc.nextInt();

        System.out.print("Enter marks of Subject 4: ");
        int m4 = sc.nextInt();

        System.out.print("Enter marks of Subject 5: ");
        int m5 = sc.nextInt();

        int total = m1 + m2 + m3 + m4 + m5;
        double percentage = total / 5.0;

        System.out.println("Total Marks = " + total);
        System.out.println("Percentage = " + percentage + "%");
    }
}
Input:
Enter marks of Subject 1: 85 Enter marks of Subject 2: 78 Enter marks of Subject 3: 92 Enter marks of Subject 4: 88 Enter marks of Subject 5: 77

Output:
Total Marks = 420 Percentage = 84.0%

Question 8: WAP to Input distance in kilometers and convert it to meters.

Solution: 
import java.util.Scanner;

class KilometerToMeter
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter Distance in Kilometers: ");
        double km = sc.nextDouble();

        double meter = km * 1000;

        System.out.println("Distance in Meters = " + meter);
    }
}
Input:
Enter Distance in Kilometers: 5

Output:
Distance in Meters = 5000.0

Decision Making Programs


Question 9: WAP to Check whether a number is positive or negative.

Solution: 

import java.util.Scanner;

class PositiveNegative
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a Number: ");
        int num = sc.nextInt();

        if(num > 0)
            System.out.println("Positive Number");
        else if(num < 0)
            System.out.println("Negative Number");
        else
            System.out.println("Zero");
    }
}

Sample Input:

Enter a Number: 25

Sample Output:

Positive Number

Question 10: WAP to Check whether a number is even or odd.

Solution: 

public class EvenOddTernary {
    public static void main(String[] args) {
        int number = 15;
        
        // Ternary operator structure: (condition) ? true_result : false_result
        String result = (number % 2 == 0) ? "Even" : "Odd";
        
        System.out.println(number + " is " + result);
    }
}

Input:

 Number: 15

Output:

Number is Odd

Question 11: WAP to Find the greater of two numbers.

Solution: 

import java.util.Scanner;

public class GreaterNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Taking input for the first number
        System.out.print("Enter the first number: ");
        int num1 = scanner.nextInt();
        
        // Taking input for the second number
        System.out.print("Enter the second number: ");
        int num2 = scanner.nextInt();
        
        // Comparing the two numbers
        if (num1 > num2) {
            System.out.println(num1 + " is greater than " + num2);
        } else if (num2 > num1) {
            System.out.println(num2 + " is greater than " + num1);
        } else {
            System.out.println("Both numbers are equal.");
        }
        
        scanner.close();
    }
}

Input:

 Enter the first number: 25
Enter the second number: 42

Output:

42 is greater than 25

Question 12: WAP to Find the largest among three numbers.

Solution: 

import java.util.Scanner;

public class LargestOfThree {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter first number: ");
        int num1 = scanner.nextInt();
        
        System.out.print("Enter second number: ");
        int num2 = scanner.nextInt();
        
        System.out.print("Enter third number: ");
        int num3 = scanner.nextInt();
        
        // Logical AND (&&) ensures both conditions must be true
        if (num1 ≥ num2 && num1 ≥ num3) {
            System.out.println(num1 + " is the largest number.");
        } else if (num2 ≥ num1 && num2 ≥ num3) {
            System.out.println(num2 + " is the largest number.");
        } else {
            System.out.println(num3 + " is the largest number.");
        }
        
        scanner.close();
    }
}

Input:

 Enter first number: 55
Enter second number: 89
Enter third number: 12

Output:

89 is the largest number.

Question 13: WAP to Check whether a year is a leap year.

Solution: 

import java.util.Scanner;

public class LeapYear {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a year: ");
        int year = sc.nextInt();

        if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }

        sc.close();
    }
}

Input:

 Enter a year: 2024

Output:

2024 is a leap year.

Question 14: WAP to Check whether a character is a vowel or consonant.

Solution: 

import java.util.Scanner;

public class VowelConsonant {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a character: ");
        char ch = sc.next().charAt(0);

        ch = Character.toLowerCase(ch);

        if (ch ≥ 'a' && ch ≤ 'z') {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                System.out.println("The character is a vowel.");
            } else {
                System.out.println("The character is a consonant.");
            }
        } else {
            System.out.println("Please enter an alphabet character.");
        }

        sc.close();
    }
}

Input:

 Enter a character: b

Output:

The character is a consonant.

Question 15: WAP to Check whether a person is eligible to vote or not.

Solution: 

import java.util.Scanner;

public class VotingEligibility {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        if (age ≥ 18) {
            System.out.println("You are eligible to vote.");
        } else {
            System.out.println("You are not eligible to vote.");
        }

        sc.close();
    }
}

Input:

 Enter your age: 16

Output:

You are not eligible to vote.

Question 16: WAP to Check whether a number is divisible by 5 and 11.

Solution: 

import java.util.Scanner;

public class Divisible {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        if (num % 5 == 0 && num % 11 == 0) {
            System.out.println(num + " is divisible by both 5 and 11.");
        } else {
            System.out.println(num + " is not divisible by 5 or 11 or both");
        }

        sc.close();
    }
}

Input:

 Enter a number: 25

Output:

25 is not divisible by  5 or 11 or both.

Read also:

Comments

Popular posts from this blog

Introduction to Object Oriented Programming in Java

Important Java Programs for Beginners | Grade, Fibonacci, Armstrong, Pattern & More

Object Modelling – Entities and Their Behaviour in Java