Problems: Simple Programs

Reading Time: 7 minutes


Printing Information

Write a program in Java that takes input from user his/her details and then prints it back on screen.

Enter your name:   Abhimanyu
Enter your gender: M
Enter your age:    16
Enter your height: 213.36

Your name is:   Abhimanyu
Your gender is: M
Your age is:    16
Your height is: 213.36

Welcome Human!
import java.util.Scanner;

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

        System.out.print("Enter your name:   ");
        String name = in.next();

        System.out.print("Enter your gender: ");
        char gender = in.next().charAt(0);

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

        System.out.print("Enter your height: ");
        float height = in.nextFloat();

        System.out.println("Your name is:   " + name);
        System.out.println("Your gender is: " + gender);
        System.out.println("Your age is:    " + age);
        System.out.println("Your height is: " + height);

        System.out.println("Welcome Human!");
        in.close();
    }
}

Geometrical Problems

Review programs in Java to solve simple geometrical problems.

Finding Perimeter and Area

Write a program in Java that takes length L and width W of a rectangle from user and calculates the perimeter and area of the rectangle.

\[ \begin{aligned} \text{Perimeter of a rectangle} &= 2 \times (\text{length} + \text{width}) \\ \text{Area of a rectangle} &= \text{length} \times \text{width} \\ \end{aligned} \]

Enter length of rectangle: 5
Enter width of rectangle:  7

Perimeter of rectangle is: 24
Area of rectangle is:      35
import java.util.Scanner;

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

        System.out.print("Enter length of rectangle: ");
        int l = in.nextInt();

        System.out.print("Enter width of rectangle:  ");
        int w = in.nextInt();

        int p = 2 * (l + w);
        int a = l * w;

        System.out.println("Perimeter of rectangle is: " + p);
        System.out.println("Area of rectangle is:      " + a);

        in.close();
    }
}

Finding Circumference and Area

Write a program in Java that takes radius R of a circle from user and calculates the circumference and area of the circle.

\[ \begin{aligned} \text{Circumference of a circle} &= 2 \times \pi \times \text{radius} \\ \text{Area of a circle} &= \pi \times \text{radius}^2 \\ \pi &= 3.14 \end{aligned} \]

Enter radius of circle: 5

Circumference of circle is: 31.42
Area of circle is:          78.54
import java.util.Scanner;

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

        System.out.print("Enter radius of circle: ");
        float r = in.nextFloat();

        float c = 2 * Math.PI * r;
        float a = Math.PI * r * r;

        System.out.println("Circumference of circle is: " + c);
        System.out.println("Area of circle is:          " + a);

        in.close();
    }
}

Finding Surface Area and Volume

Write a program in Java that takes one side S of a cube from user and calculates the volume and surface area of the cube.

\[ \begin{aligned} \text{Volume of a cube} &= \text{side} \times \text{side} \times \text{side} \\ \text{Surface area of a cube} &= 6 \times \text{side} \times \text{side} \\ \end{aligned} \]

Enter side of cube: 4

Surface area of cube is: 96
Volume of cube is:       64
import java.util.Scanner;

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

        System.out.print("Enter side of cube: ");
        int s = in.nextInt();

        int a = 6 * s * s;
        int v = s * s * s;

        System.out.println("Surface area of cube is: " + a);
        System.out.println("Volume of cube is:       " + v);

        in.close();
    }
}

Percentage Problems

Review programs in Java to solve simple real-life percentage related problems.

Finding Percentage of Marks

Class teachers have a tiring task of calculating the percentage of marks obtained in science subjects for each students. To help them out, your class decides to ease their task.

Write a program in Java that takes marks obtained by a student in Biology B, Physics P, and Chemistry C, along with the total marks T of all the three subject and calculate the percentage of marks obtained in science subjects by a student.

\[ \text{Marks obtained} = \frac{\text{Marks obtained in (Biology + Physics + Chemistry)}}{\text{Total marks of science subjects}} \times 100 \]

Enter marks obtained in Biology:   80
Enter marks obtained in Chemistry: 78
Enter marks obtained in Physics:   85
Enter total marks of all subjects: 300

Marks obtained in Science subjects: 81
import java.util.Scanner;

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

        System.out.print("Enter marks obtained in Biology:   ");
        int b = in.nextInt();

        System.out.print("Enter marks obtained in Chemistry: ");
        int c = in.nextInt();
        
        System.out.print("Enter marks obtained in Physics:   ");
        int p = in.nextInt();

        System.out.print("Enter total marks of all subjects: ");
        int t = in.nextInt();

        int percent = ((b + c + p) / t) * 100;

        System.out.print("Marks obtained in Science subjects: " + percent);

        in.close();
    }
}

Finding Simple Interest

One of your rich relative is in a business of giving out loans. Being in a loan business, he often has to calculate simple interest. So, you decided to ease his task of finding interest payable by clients.

Write a program in Java that takes from user a principal amount P, annual rate of interest R, and time of loan T and calculates the simple interest and total amount payable at the end of loan period.

\[ \begin{aligned} \text{Interest} &= \text{Principal} \times \text{Time} \times \frac{Rate}{100} \\ \text{Amount} &= \text{Interest} + \text{Principal} \\ \end{aligned} \]

Enter principal amount: 25000
Enter interest rate:    2.5
Enter time elapsed:     3

Interest payable:     1875
Total amount payable: 26875
import java.util.Scanner;

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

        System.out.print("Enter principal amount: ");
        p = in.nextFloat();
        System.out.print("Enter interest rate:    ");
        r = in.nextFloat();
        System.out.print("Enter time elapsed:     ");
        t = in.nextFloat();

        float i = (p * r * t) / 100;
        float a = p + i;

        System.out.println("Interest payable:     " + i);
        System.out.println("Total amount payable: " + a);

        in.close();
    }
}

Finding Gain Percentage

Sneha owns a stationery shops and wants to make a certain gain on each item she sells. Help her out by writing a program for her.

Write a program in Java for her that takes the cost price CP of an item and the gain percentage GP she wants and then calculate the selling price of the item.

\[ \text{Selling price} = \text{Cost price} \times \left( 1 + \frac{\text{Gain \%}}{100} \right) \]

Enter cost price of item: 50
Enter gain percentage:    30

Selling price will be: 65
import java.util.Scanner;

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

        System.out.print("Enter cost price of item: ");
        float cp = in.nextFloat();

        System.out.print("Enter gain percentage:    ");
        float gp = in.nextFloat();

        float sp = cp * (1 + (gp / 100));

        System.out.println("Selling price will be: " + sp);

        in.close();
    }
}

SI Unit Problems

SI units is the future. Yet, people still tend to use the old-school British sytem of units instead of the new standards of units. To help people out and make remarkable change on the society, you decided to write a bunch of programs to convert older units to newer SI units.

Converting Gram to Kilogram

Write a program in Java that takes a weight G in grams from user and prints the corresponding weight in Kilogram.

Enter weight (in G): 2500

Weight (in KG) is: 2.5
import java.util.Scanner;

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

        System.out.print("Enter weight (in G): ");
        float g = in.nextFloat();

        float kg = g / 1000;

        System.out.print("Weight (in KG) is: " + kg);

        in.close();
    }
}

Converting Fahreinheit to Celsius

Write a program in Java that takes a temperature F in Fahreinheit from user and prints the corresponding temperature in Celsius.

Enter temperature (in F): 98.2

Temperature (in C) is: 36.8
import java.util.Scanner;

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

        System.out.print("Enter temperature (in F): ");
        float f = in.nextFloat();

        float c = 5 * (f - 32) / 9;

        print("Temperature (in C) is: " + c);

        in.close();
    }
}

Converting H:M:S to Seconds

Write a program in Java that takes hours H, minutes M, and seconds S from user and prints the corresponding time in seconds.

Enter hour:    42
Enter minutes: 14
Enter seconds: 5

Total seconds: 152045
import java.util.Scanner;

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

        System.out.print("Enter hour:    ");
        int h = in.nextInt();
        System.out.print("Enter minutes: ");
        int m = in.nextInt();
        System.out.print("Enter seconds: ");
        float s = in.nextFloat();

        s = s + (m * 60) + (h * 60 * 60);

        System.out.println("Total seconds: " + s);

        in.close();
    }
}