In: Computer Science
JAVA
Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars to Saudi Riyals. Prompt the user to enter 0 to convert from U.S. dollars to Saudi Riyals and 1 to convert from Saudi Riyal and U.S. dollars. Prompt the user to enter the amount in U.S. dollars or Saudi Riyal to convert it to Saudi Riyal or U.S. dollars, respectively.
Here are the sample runs:
Enter the exchange rate from
dollars to SR: 3.75
Enter 0 to convert dollars to SR and 1 vice versa: 0
Enter the dollar amount: 100
$100.0 is 375.0 SR
SOLUTION-
I have solve the problem in Java code with comments and screenshot
for easy understanding :)
CODE-
//java code
import java.util.Scanner;
//main class
public class Main
{
//main method
public static void main(String args[])
{
double resultDoller=0;
double resultSR=0;
//Create scanner
Scanner scan = new Scanner(System.in);
System.out.print("Enter the exchange rate from
dollars to SR: ");
double rate=scan.nextDouble(); //read dollar to
convert
//ASK USER FOR A OPTION IF '0' THEN USD TO SR
OTHERWISE SR TO USD
System.out.print("Enter 0 to convert from
Dollars to SR and 1 to viceversa: ");
int ch=scan.nextInt();
//for teh user choice
switch(ch)
{
case 0:
System.out.print("Enter the dollar amount: ");
double dollar = scan.nextDouble(); //Input the amount of usd from
user
resultDoller = rate*dollar; //convert dollar to SR
System.out.print("$"+dollar+ " is " +resultDoller+" SR"); //display
the result
break;
case 1:
System.out.println("Enter the SR amount: ");
double SR = scan.nextDouble(); //Input the amount of SR from
user
resultSR = SR / rate; //convert SR to dollar
System.out.printf("%.lf SR is $%.2f",SR,resultSR); //display the
result
break;
default:
System.out.printf("Incorrect input");
}
}
}
SCREENSHOT-
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------