Last active
September 20, 2021 11:32
-
-
Save OddExtension5/84af3b63125f52e06398ccf1be2df60f to your computer and use it in GitHub Desktop.
JAVA CLASSES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```java | |
/* | |
Program 01 | |
Write a program to take your name as input from user through keyboard and display a greeting | |
*/ | |
import java.util.Scanner; | |
public class Greeting { | |
public static void main(String[] args) { | |
Scanner s = new Scanner(System.in); | |
System.out.print("Enter your name: "); | |
String name = s.nextLine(); | |
System.out.println("Hello " + name + " !!!"); | |
} | |
} | |
======================================================================================================================= | |
/* | |
Program 02 | |
Write a program that inputs, calculates and displays the average of three numbers | |
*/ | |
import java.util.Scanner; | |
public class Clac { | |
public static void main(String args[]) { | |
float n1, n2, n3, sum, avg; | |
Scanner kb = new Scanner(System.in); | |
System.out.println("Enter 3 numbers :"); | |
n1 = kb.nextFloat(); | |
n2 = kb.nextFloat(); | |
n3 = kb.nextFloat(); | |
sum = n1 + n2 + n3; | |
avg = sum / 3; | |
System.out.println("You entered : " + n1 + " , " + n2 + " , " + n3 ); | |
System.out.println("Their average is = " + avg); | |
} | |
} | |
========================================================================================================================= | |
/* | |
Program 03 | |
Write a program to read a time in minutes and split it into days, hours and minutes. | |
*/ | |
import java.util.Scanner; | |
public class Time { | |
public static void main(String args[]) { | |
final int hoursInDay = 24; | |
final int minsInHour = 60; | |
int totMins; | |
int totHrs; | |
int days = 0, hours = 0, mins = 0; | |
Scanner Keyboard = new Scanner(System.in); | |
System.out.print("Enter total minutes: "); | |
totMins = Keyboard.nextInt(); | |
totHrs = totMins / minsInHour; | |
mins = totMins % minsInHour; | |
days = totHrs / hoursInDay; | |
hours = totHrs % hoursInDay; | |
System.out.println(totMins + " minutes is " + days + " days, " + hours + " hours and " + mins + " minutes"); | |
} | |
} | |
============================================================================================================================= | |
/* | |
Program 04 | |
Write a program that inputs principal amount, rate of interest and time and calculates compound interest. | |
*/ | |
import java.util.Scanner; | |
public class CalcInterest { | |
public static void main(String args[]) { | |
double principal, time, rate, amount, cinterest; | |
Scanner kb = new Scanner(System.in); | |
System.out.print("Enter the principal amount: "); | |
principal = kb.nextDouble(); | |
System.out.print("Enter time (in years): "); | |
time = kb.nextDouble(); | |
System.out.print("Enter interest rate (%): "); | |
rate = kb.nextDouble(); | |
amount = principal * Math.pow((100 + rate)/100, time); | |
cinterest = amount - principal; | |
System.out.println("Principal Amount = " + principal); | |
System.out.println("Time invested for " + time + " years"); | |
System.out.println("At " + rate + "% invested"); | |
System.out.println("Compound Interest generated = " + cinterest); | |
System.out.println("Final Amount is: " + amount); | |
} | |
} | |
================================================================================================================== | |
/* | |
Program 05 | |
Write a program to calculate the distance between 2 points (x1, y1) and (x2, y2) | |
Hint: Use Distance Formula of Coordinate Geometry | |
*/ | |
import java.util.Scanner; | |
public class Distance { | |
public static void main(String args[]) { | |
int x1, x2, y1, y2; | |
double dist; | |
Scanner kb = new Scanner(System.in); | |
System.out.print("Enter the x1 value: "); | |
x1 = kb.nextInt(); | |
System.out.print("Enter the x2 value: "); | |
x2 = kb.nextInt(); | |
System.out.print("Enter the y1 value: "); | |
y1 = kb.nextInt(); | |
System.out.print("Enter the y2 value: "); | |
y2 = kb.nextInt(); | |
dist = Math.sqrt( Math.pow((x2 - x1), 2) + Math.pow((y2-y1), 2)); | |
System.out.println("Distance between the given coordinates is: " + dist); | |
} | |
} | |
``` | |
================================================================================================================== | |
/* | |
Program 06 | |
Write a program to print sum of three given numbers 1221, 2332, 3443. | |
*/ | |
/* | |
Program 07 | |
Write a program to find the perimeter of a triangle with sides measuring 10cm, 14cm, and 15cm | |
*/ | |
/* | |
Program 08 | |
Write a program to find the side of the square whose perimeter is 20m | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment