Home Interview interview program java Basic interview programs in java part 1

# Basic interview programs in java part 1

2  Nowadays in any interview, a programming test is a must, So here we are creating a series of basic interview programs in java, We learn step by step

## Program to Accept values of Two Numbers and print their Addition

``````package com.onurdesk.basic.interview.programs;

import java.util.Scanner;

public static void main(String args[]) {
int x, y, z;
System.out.print("Enter two integers to calculate their sum : ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = x + y;
System.out.println("Sum of entered integers = " + z);
System.out.println("Thank you for reading basic interview programs in java");

}
}``````

## Program to Accept value of Radius and Calculate Area of Circle.

``````package com.onurdesk.basic.interview.programs;

class CalculateCircleAreaOnurdeskExample {

public static void main(String[] args) {

/*
* Area of a circle is pi * r * r where r is a radius of a circle.
*/

// NOTE : use Math.PI constant to get value of pi

System.out.println("Area of a circle is " + area);
System.out.println("Thank you for reading basic interview programs in java");

}
}``````

## Program to Accept value of length & width and Calculate Area of Rectangle.

``````package com.onurdesk.basic.interview.programs;

import java.util.Scanner;

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

System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();

System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();

// Area = length*width;
double area = length * width;

System.out.println("Area of Rectangle is:" + area);
System.out.println("Thank you for reading basic interview programs in java");
}
}
``````

## Program to Accept value of the side of Square and Calculate Area of Square.

``````package com.onurdesk.basic.interview.programs;

import java.util.*;

class AreaOfSquare {
public static void main(String args[]) {
int side, area;

Scanner sc = new Scanner(System.in);

System.out.println("Enter value of the sides of square");
side = sc.nextInt();

area = side * side;

System.out.println("Area of Square : " + area);
System.out.println("Thank you for reading basic interview programs in java");
}
}
``````

## Write a Program to Calculate Areas in Java.

``````package com.onurdesk.basic.interview.programs;

import java.util.Scanner;

class AreaCalculator {

float l, b, h, r, ba, s, c;
float result = 0f;
float pi = 3.14f;
int var;

public static void main(String[] args) {
AreaCalculator ar = new AreaCalculator();
ar.options();

System.out.println("Thank you for reading basic interview programs in java");

}

public void options() {
Scanner a = new Scanner(System.in);
System.out.println(
"Enter the Object of which Area is to be calculated \n1:square \n2:rectangle \n3:Triangle\n4:circle\n5:Trapezoid\n6:Repeat\n7:Exit");

var = a.nextInt();
Area a1 = new Area();

if (var == 1) {
System.out.println("Enter the Side of Square");
s = a.nextFloat();
a1.square(s);
options();
}

else if (var == 2) {
System.out.println("Enter the Length of Rectangle");
l = a.nextFloat();
b = a.nextFloat();
a1.rectangle(l, b);
options();
}

else if (var == 3) {
System.out.println("Enter the Height of Triangle");
h = a.nextFloat();
System.out.println("Enter the Base of Triangle");
ba = a.nextFloat();
a1.triangle(h, ba);
options();
}

else if (var == 4) {
r = a.nextFloat();
a1.circle(r);
options();
}

else if (var == 5) {
System.out.println("Enter the A side of Trapezoid");
b = a.nextFloat();
System.out.println("Enter the B side of Trapezoid");
c = a.nextFloat();
System.out.println("Enter the Height of Trapezoid");
h = a.nextFloat();
a1.trapezoid(b, c, h);
options();
}
}
}

class Area {
public void square(float s) {
float result = 0f;
result = s * s;
System.out.println("The Area of Square is :" + result);
}

public void rectangle(float l, float b) {
float result = 0f;
result = l * b;
System.out.println("The Area of Rectangle is :" + result);
}

public void triangle(float h, float ba) {
float result = 0f;
result = 0.5f * h * ba;
System.out.println("The Area of Triangle is :" + result);
}

public void circle(float r) {
float result = 0f;
result = 3.14f * (r * r);
System.out.println("The Area of Circle is :" + result);
}

public void trapezoid(float b, float c, float h) {
float result = 0f;
result = (((b + c) / 2) * h);
System.out.println("The Area of Trapezoid is :" + result);
}
}``````

## Write a program Java to check if the number input by the users is prime or not

``````package com.onurdesk.basic.interview.programs;

import java.util.Scanner;

class PrimeCheck {
public static void main(String args[]) {
int temp;
boolean isPrime = true;
Scanner scan = new Scanner(System.in);
System.out.println("Enter any number:");
// number input by the users
int num = scan.nextInt();
scan.close();
for (int i = 2; i <= num / 2; i++) {
temp = num % i;
if (temp == 0) {
isPrime = false;
break;
}
}
// If isPrime is true then the number is prime else not
if (isPrime)
System.out.println("This "+num + " is a Prime Number");
else
System.out.println("This "+num + " is not a Prime Number");

System.out.println("Thanks for visiting https://onurdesk.com");
}
}``````

## Write a program to Find Simple Interest and Compound Interest in Java

``````package com.onurdesk.basic.interview.programs;

import java.lang.*;
import java.util.Scanner;

class CalculateInterest
{
public static void main(String[] args)
{
double p, n, r, si, ci;

Scanner s = new Scanner(System.in);

System.out.print("Enter the amount : ");
p = s.nextDouble();

System.out.print("Enter the No.of years : ");
n = s.nextDouble();

System.out.print("Enter the Rate of interest : ");
r = s.nextDouble();

si = (p * n * r) / 100;
ci = p * Math.pow(1.0 + r / 100.0, n) - p;

System.out.println("\nAmount : " + p);
System.out.println("No. of years : " + n);
System.out.println("Rate of interest : " + r);
System.out.println("Simple Interest : " + si);
System.out.println("Compound Interest : " + ci);

System.out.println("Thank you for reading basic interview programs in java");

}
}
``````

## Write a program to Calculate Binary Number Calculation in Java

``````package com.onurdesk.basic.interview.programs;

import java.util.*;

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

System.out.print("First Binary:  ");
String binOne = in.next();

System.out.print("Second Binary: ");
String binTwo = in.next();

int left = Integer.parseInt(binOne, 2);
int right = Integer.parseInt(binTwo, 2);

System.out.println("Sum of the binary numbers : " + Integer.toBinaryString(left + right));
System.out.println("Difference of the binary numbers : " + Integer.toBinaryString(left - right));
System.out.println("Product of the binary numbers : " + Integer.toBinaryString(left * right));
System.out.println("Quotient of the binary numbers : " + Integer.toBinaryString(left / right));

System.out.println("Thank you for reading basic interview programs in java");

}
}
``````

## Write a program to convert Celsius to Fahrenheit in java.

``````package com.onurdesk.basic.interview.programs;

import java.util.*;

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

System.out.println("Enter temperature in Celsius");
temperature = in.nextInt();

temperature = (temperature * 9 / 5.0) + 32;
System.out.println("Temperature in Fahrenheit = " + temperature);

System.out.println("Thank you for reading basic interview programs in java");
}
}``````

## Write a program to Create a Simple Calculator in Java.

``````package com.onurdesk.basic.interview.programs;

import java.util.Scanner;
import java.io.*;

class Calculator {
public static void main(String[] args) {
int choice;
int x = 0;
int y = 0;
int sum;
PrintStream out;
Scanner input;
Calculator calc = new Calculator();
try {
out = new PrintStream("calclog.txt");
do {
System.out.println("Calculator Program");
System.out.println("--------------------\n");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Mod");
System.out.println("6. Power");
System.out.println("99. End Program\n");
System.out.println("Enter Choice: ");
input = new Scanner(System.in);
choice = input.nextInt();
while ((choice < 1 || choice > 6) && choice != 99) {
System.out.println("Please enter 1, 2, 3, 4, 5, or 6: ");
choice = input.nextInt();
}
if (choice != 99) {
System.out.println("Please enter 2 numbers only: ");
x = input.nextInt();
y = input.nextInt();
}
switch (choice) {
case 1:
System.out.printf("The sum is %d\n\n", sum);
out.println(x + "+" + y + "=" + sum);
break;
case 2:
sum = calc.sub(x, y);
out.println(x + "-" + y + "=" + sum);
break;
case 3:
sum = calc.multi(x, y);
out.println(x + "*" + y + "=" + sum);
break;
case 4:
try {
sum = calc.div(x, y);
out.println(x + "/" + y + "=" + sum);
} catch (Exception e) {
System.out.println("\nError: Cannot Divide by zero\n\n");
}
break;
case 5:
sum = calc.mod(x, y);
System.out.printf("The mod is %d\n\n", sum);
out.println(x + "%" + y + "=" + sum);
break;
case 6:
sum = calc.pow(x, y);
out.println(x + "^" + y + "=" + sum);
break;
}
} while (choice != 99);
input.close();
System.out.println("Ending program...");
} catch (Exception e) {
System.out.println("ERROR: Some error occured");
e.printStackTrace();
}
}

public int add(int num1, int num2) {
int sum;
sum = num1 + num2;
return sum;
}

public int sub(int num1, int num2) {
int sum;
sum = num1 - num2;
return sum;
}

public int multi(int num1, int num2) {
int sum;
sum = num1 * num2;
return sum;
}

public int div(int num1, int num2) {
int sum;
sum = num1 / num2;
return sum;
}

public int mod(int num1, int num2) {
int sum;
sum = num1 % num2;
return sum;
}

public int pow(int base, int exp) {
int sum = 1;
if (exp == 0) {
sum = 1;
}
while (exp > 0) {
sum = sum * base;
exp--;
}
return sum;
}
}``````

## Write a program to Calculate Mean in java

``````package com.onurdesk.basic.interview.programs;

import java.util.Scanner;

class CalculateMean {

public static void main(String[] args) {

int sum = 0, inputNum;
int counter;
float mean;
Scanner NumScanner = new Scanner(System.in);

System.out.println("Enter the total number of terms whose mean you want to calculate");

counter = NumScanner.nextInt();

System.out.println("Please enter " + counter + " numbers:");

for (int x = 1; x <= counter; x++) {
inputNum = NumScanner.nextInt();
sum = sum + inputNum;
System.out.println();
}

mean = sum / counter;
System.out.println("The mean of the " + counter + " numbers you entered is " + mean);
System.out.println("Thank you for reading basic interview programs in java");
}
}
``````