Header Ads Widget

Ticker

6/recent/ticker-posts

Basic Program 1. Study of class path and java runtime environment 2. Write a program to ● Implement command line calculator ● Write To prints Fibonacci series.

Study of class path and Java Runtime Environment:

Classpath in Java is a parameter used to specify the location of classes and packages that the Java Virtual Machine needs to execute a Java program. It is an environment variable that contains the directories or JAR files that contain Java class files. When a Java program is compiled, it generates bytecode that is saved in class files. These class files are then loaded by the Java Virtual Machine when the program is executed.

Java Runtime Environment (JRE) is a software package that provides the Java Virtual Machine, Java class libraries, and other components that are needed to run Java applications. JRE is required to run Java applications or applets on a machine. It includes the Java Virtual Machine, which is responsible for interpreting and executing Java bytecode, as well as the Java class libraries, which provide the core functionality of the Java programming language.

Write a program to implement command-line calculator:

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double num1, num2, result;
        char operator;

        System.out.print("Enter first number: ");
        num1 = input.nextDouble();

        System.out.print("Enter second number: ");
        num2 = input.nextDouble();

        System.out.print("Enter operator (+, -, *, /): ");
        operator = input.next().charAt(0);

        switch (operator) {
            case '+':
                result = num1 + num2;
                System.out.printf("%.2f + %.2f = %.2f", num1, num2, result);
                break;

            case '-':
                result = num1 - num2;
                System.out.printf("%.2f - %.2f = %.2f", num1, num2, result);
                break;

            case '*':
                result = num1 * num2;
                System.out.printf("%.2f * %.2f = %.2f", num1, num2, result);
                break;

            case '/':
                result = num1 / num2;
                System.out.printf("%.2f / %.2f = %.2f", num1, num2, result);
                break;

            default:
                System.out.println("Invalid operator.");
        }
    }
}

Write a program to print Fibonacci series:

import java.util.Scanner;

public class Fibonacci {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n, i, t1 = 0, t2 = 1;

        System.out.print("Enter number of terms: ");
        n = input.nextInt();

        System.out.print("Fibonacci Series: ");
        for (i = 1; i <= n; ++i) {
            System.out.print(t1 + " ");

            int sum = t1 + t2;
            t1 = t2;
            t2 = sum;
        }
    }
}

Post a Comment

0 Comments