0% found this document useful (0 votes)
65 views8 pages

Import Public Class Public Static Void Null Double Int Try New in Out Out Out

This Java program allows users to convert between kilometers and miles. It prompts the user to choose between converting kilometers to miles or miles to kilometers, then prompts the user to enter a distance. Based on the options selected, it performs the conversion calculation and displays the result.

Uploaded by

fikrue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views8 pages

Import Public Class Public Static Void Null Double Int Try New in Out Out Out

This Java program allows users to convert between kilometers and miles. It prompts the user to choose between converting kilometers to miles or miles to kilometers, then prompts the user to enter a distance. Based on the options selected, it performs the conversion calculation and displays the result.

Uploaded by

fikrue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

import java.util.

Scanner;

public class DistanceConverter {


public static void main(String args[]) {
Scanner input = null;
double km,mi,distance;
int option;
try {
input = new Scanner(System.in);
System.out.println("Choose an
Option:");
System.out.println("Convert Kilometers
to Miles(1)");
System.out.println("Convert Miles to
Kilometers(2)");
option =
Integer.parseInt(input.nextLine());
if (option != 1 && option != 2)
{
System.out.println("Plz choose
option 1 or 2");
System.exit(0);
}
if(option == 1) {
System.out.println("Enter Distance
in Kilometers");
distance =
Double.parseDouble(input.nextLine());
mi = 0.62 * distance;
System.out.println("Distance in
Miles is:" + mi );

}
else if(option == 2) {
System.out.println("Enter Distance
in Miles");
distance =
Double.parseDouble(input.nextLine());
km = 1.60 * distance;
System.out.println("Distance in
Kilometers is:" + km );
}
}
catch(Exception e)
{
System.out.print("Error.Program
Terminated");
}
finally
{
if (input != null) {
input.close();
}
}
}
}

In this tutorial, we will develop a java program to convert


Fahrenheit to Celsius temperature. The mathematical formula to
converting fahrenheit temperature to celsius temperature is
below:

Celsius = 5 * (Fahrenheit – 32) /9


IPO chart
Input Process Output
Celsius = 5.0 *
Temperature in Temperature in
(fahrenheit – 32.0) /
Fahrenheit Celsius
9.0;
Java Program
The following program takes the input of Fahrenheit temperature
from the user. It calculates the Celsius equivalent of the Fahrenheit
temperature. Displays the Celsius temperature in the output.

public class FahrenheitToCelsius {

public static void main(String args[]) throws


IOException {
BufferedReader input = null;
double fahrenheit,celsius;
try {
input = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Please enter
temperature in Fahrenheit : ");
fahrenheit =
Double.parseDouble(input.readLine());

celsius = 5.0 * (fahrenheit - 32.0) /


9.0;
System.out.println("The temperature in
Fahrenheit provided:"
+ fahrenheit + " F");
System.out.println("The temperature in
Celsius is:" + celsius + " C");
}
catch(IOException ioe)
{
System.out.print("Unexpected IO
error.");
}
catch(NumberFormatException nfe)
{
System.out.print("Please check for
valid input.");
}
catch(Exception e)
{
System.out.print("Unexpected error.");
}
finally
{
if (input != null) {
input.close();
}
} } }
import java.util.Scanner;

public class MathUnitConversions6 {

public static void main(String[] args) {

double kilometers;

System.out.println("Please enter kilometers:");

Scanner in = new Scanner(System.in);


kilometers = in.nextDouble();

double miles = kilometers / 1.6;

System.out.println(miles + " Miles");

}
}

Java program for conversion of Kilometers


(KMS) to Miles
import java.util.Scanner;

public class DeveloperHelps {

public static void main(String[] args) {

double kilometers;
System.out.println("Please enter the kilometers");

Scanner in = new Scanner(System.in);

kilometers = in.nextDouble();

double miles = kilometers / 1.609;

System.out.println(miles + " miles");

Java program for conversion of Miles to


Kilometers (KMS)
import java.util.Scanner;

public class DeveloperHelps {

public static void main(String[] args) {

double miles;

Scanner in = new Scanner(System.in);

System.out.println("Please enter the miles:");

miles = in.nextDouble();

double kilometers = miles * 1.609;

System.out.println(kilometers + " Kilometers");

}
}

Java Program to determine whether a given string


is palindrome
In this program, we need to check whether a given string is palindrome or not.

A string is said to be palindrome if it is the same from both the ends. For e.g. above
string is a palindrome because if we try to read it from backward, it is same as forward.
One of the approach to check this is iterate through the string till middle of string and
compare a character from back and forth.

ALGORITHM
o STEP 1: START

o STEP 2: DEFINE String string = "Kayak"

o STEP 3: SET flag = true

o STEP 4: CONVERT string into lowercase.

o STEP 5: SET i =0. REPEAT STEP 6 to STEP 7 UNTIL i<="" li="">

o STEP 6: IF (string.charAt(i) != string.charAt(string.length()-i-1))


              then
              SET flag = false
              break
o STEP 7: SET i = i + 1

o STEP 8: IF flag


              then PRINT "Yes"
              else
              PRINT "No"
o STEP 9: END

Program:
1. public class PalindromeString    
2. {    
3.     public static void main(String[] args) {    
4.         String string = "Kayak";    
5.         boolean flag = true;    
6.             
7.         //Converts the given string into lowercase    
8.         string = string.toLowerCase();    
9.             
10.         //Iterate the string forward and backward, compare one character at a tim
e     
11.         //till middle of the string is reached    
12.         for(int i = 0; i < string.length()/2; i++){    
13.             if(string.charAt(i) != string.charAt(string.length()-i-1)){    
14.                 flag = false;    
15.                 break;    
16.             }    
17.         }    
18.         if(flag)    
19.             System.out.println("Given string is palindrome");    
20.         else    
21.             System.out.println("Given string is not a palindrome");    
22.     }    
23. } 

ava Program to find Reverse of the string.


In this program, we need to find the reverse of the string. This can be done by iterating
the string backward and storing each character from the original string into a new
string.

1. Original string: Dream big  
2. Reverse of the string: big maerD  

ALGORITHM
o STEP 1: START
o STEP 2: DEFINE String string = "Dream big"

o STEP 3: DEFINE reversedStr = " "

o STEP 4: SET i =string.length()-1. REPEAT STEP 5 to STEP 6 UNTIL i>=0

o STEP 5: reversedStr = reversedStr + string.charAt(i)

o STEP 6: i = i - 1

o STEP 7: PRINT string.

o STEP 8: PRINT reversedStr.

o STEP 9: END

Program:
1.   public class Reverse   
2. {    
3.     public static void main(String[] args) {    
4.         String string = "Dream big";    
5.         //Stores the reverse of given string    
6.         String reversedStr = "";    
7.             
8.         //Iterate through the string from last and add each character to variable re
versedStr    
9.         for(int i = string.length()-1; i >= 0; i--){    
10.             reversedStr = reversedStr + string.charAt(i);    
11.         }    
12.             
13.         System.out.println("Original string: " + string);    
14.         //Displays the reverse of given string    
15.         System.out.println("Reverse of given string: " + reversedStr);    
16.     }    
17. }    

Output:

You might also like