Python/C/C++/JAVA

Basic Practice Programs with Code and Concept

By D.S

Area and Volumes of different Shapes

In this program, you will be prompted to enter your choice of shape (Square, Circle, Cube) and provide the necessary dimensions. The program will then calculate and display the area or volume of the selected shape.

(a.) C Code

#include <stdio.h>
		int main() {
        int choice;
        float side, radius, area, volume;

        printf("Enter your choice: 
1. Square 
2. Circle 
3. Cube
");
        scanf("%d", &choice);

        switch(choice) {
            case 1:
                printf("Enter the side of the square: ");
                scanf("%f", &side);
                area = side * side;
                printf("Area of the square: %.2f
", area);
                break;
            case 2:
                printf("Enter the radius of the circle: ");
                scanf("%f", &radius);
                area = 3.14 * radius * radius;
                printf("Area of the circle: %.2f
", area);
                break;
            case 3:
                printf("Enter the side of the cube: ");
                scanf("%f", &side);
                volume = side * side * side;
                printf("Volume of the cube: %.2f
", volume);
                break;
            default:
                printf("Invalid choice
");
        }

        return 0;
    }
Output:-
Enter your choice:
1. Square
2. Circle
3. Cube
1
Enter the side of the square: 4
Area of the square: 16.00

(b.) C++ Code

#include <iostream>
    #include <cmath>
    using namespace std;

    int main() {
        int choice;
        float side, radius, area, volume;

        cout << "Enter your choice: 
1. Square 
2. Circle 
3. Cube
";
        cin >> choice;

        switch(choice) {
            case 1:
                cout << "Enter the side of the square: ";
                cin >> side;
                area = side * side;
                cout << "Area of the square: " << area << endl;
                break;
            case 2:
                cout << "Enter the radius of the circle: ";
                cin >> radius;
                area = M_PI * radius * radius;
                cout << "Area of the circle: " << area << endl;
                break;
            case 3:
                cout << "Enter the side of the cube: ";
                cin >> side;
                volume = side * side * side;
                cout << "Volume of the cube: " << volume << endl;
                break;
            default:
                cout << "Invalid choice" << endl;
        }

        return 0;
    }
Output:-
Enter your choice:
1. Square
2. Circle
3. Cube
1
Enter the side of the square: 4
Area of the square: 16.00

(c.) Python Code

import math

    def main():
        print("Enter your choice: 
1. Square 
2. Circle 
3. Cube")
        choice = int(input())

        if choice == 1:
            side = float(input("Enter the side of the square: "))
            area = side * side
            print("Area of the square: {:.2f}".format(area))
        elif choice == 2:
            radius = float(input("Enter the radius of the circle: "))
            area = math.pi * radius * radius
            print("Area of the circle: {:.2f}".format(area))
        elif choice == 3:
            side = float(input("Enter the side of the cube: "))
            volume = side * side * side
            print("Volume of the cube: {:.2f}".format(volume))
        else:
            print("Invalid choice")

    if __name__ == "__main__":
        main()
Output:-
Enter your choice:
1. Square
2. Circle
3. Cube
1
Enter the side of the square: 4
Area of the square: 16.00

(d.) Java Code

import java.util.Scanner;

    public class AreaAndVolume {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter your choice: 
1. Square 
2. Circle 
3. Cube");
            int choice = scanner.nextInt();

            switch(choice) {
                case 1:
                    System.out.print("Enter the side of the square: ");
                    float side = scanner.nextFloat();
                    float area = side * side;
                    System.out.println("Area of the square: " + area);
                    break;
                case 2:
                    System.out.print("Enter the radius of the circle: ");
                    float radius = scanner.nextFloat();
                    area = (float) (Math.PI * radius * radius);
                    System.out.println("Area of the circle: " + area);
                    break;
                case 3:
                    System.out.print("Enter the side of the cube: ");
                    side = scanner.nextFloat();
                    float volume = side * side * side;
                    System.out.println("Volume of the cube: " + volume);
                    break;
                default:code
				
                    System.out.println("Invalid choice");
            }
        }
    }
Output:-
Enter your choice:
1. Square
2. Circle
3. Cube
1
Enter the side of the square: 4
Area of the square: 16.00