1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
mars1129 [50]
3 years ago
10

Write a program to convert a fraction to a decimal. Have your program ask for the numerator first, then the denominator. Make su

re to check if the denominator is zero. If it is, print out "Error - cannot divide by zero."
Computers and Technology
1 answer:
Anton [14]3 years ago
3 0
<h2>Answer:</h2>

import java.util.Scanner;

public class FractionToDecimal {

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       

       System.out.println("Please enter the numerator");

       double numerator = input.nextDouble();

       System.out.println("Please enter the denominator");

       double denominator = input.nextDouble();

       

       if(denominator == 0){

           System.err.println("ERROR - Cannot divide by zero.");

       }

       else {

           System.out.println("The result is " + (numerator/denominator));

       }

       

   }

   

}

<h2>Explanation:</h2>

// import the Scanner class to allow for user input

import java.util.Scanner;

// 1. Declare a class

public class FractionToDecimal {

   // 2. Write the main method

   public static void main(String[] args) {

       

       // 3. Create an object of the Scanner class called <em>input</em>

       Scanner input = new Scanner(System.in);

       

       // 4. Prompt the user to supply the numerator

       System.out.println("Please enter the numerator");

       

       // 5. Declare and initialize a variable <em>numerator </em>to hold the numerator

       // supplied by the user.

       // The numerator is of type double since the program does not

       // specify any. With a double both integer and floating point numbers

       // will be supported.

       double numerator = input.nextDouble();

       

       // 6. Prompt the user to enter the denominator

       System.out.println("Please enter the denominator");

       // 7. Declare and initialize a variable <em>denominator </em>to hold the

       // denominator supplied by the user.

       // The denominator is also of type double since the program does not

       // specify any. With a double, both integer and floating point numbers

       // will be supported.

       double denominator = input.nextDouble();

       // 8. Check if the denominator is or is not zero.

       // if it is zero, display an error message        

       if(denominator == 0){

           System.err.println("ERROR - Cannot divide by zero.");

       }

       // if it is not zero, then print out the result of the division

       else {

           System.out.println("The result is " + (numerator/denominator));

       }

       

   }                       // end of main method

   

}                            // end of class declaration

Please Note:

The code above has been written in Java.

The explanation segment contains comments to explain every line of the code.

But a few things are still worth noting;

i. Dividing the numerator by the denominator will convert a fraction to decimal.

ii. The object of the Scanner class <em>input </em> is used to get the inputs (numerator and denominator) from the user. Since the inputs are going to be integers or doubles, the nextDouble() method of the the Scanner class is used.

iii. If the denominator is zero, an error message will be displayed. I have used the System.err.println() method for that rather than the regular System.out.println() method. The difference is that the former will print out the error text in red color.

iv. Notice also that the else statement in the code will perform the division and also print out the result of the division along side with some text.

<em>Hope this helps!</em>

You might be interested in
Write algorithm to find (a+b)^2=(a+b)*(a+b)​
Masja [62]

Answer:

Basically

(a+b)^2 = a^2 + b^2 + 2ab

Explanation:

steps

       1: start

       2: read a value

        3: read b value

        4: c=(a+b)*(a+b);

        5: print  c value

         6:end

3 0
2 years ago
What is required to become a good critical thinker? Use a process when dealing with a big issue. Practice the right skills over
Damm [24]

Answer:

Evaluate issues based on logic and reason

Explanation:

Critical Thinking is the ability to think scientifically & logically, analysing based on both formers - rather than rule of thumb.

Critical thinker engages clearly in conceptual, rational thinking. So, an important desirable characteristic of a critical thinker is 'evaluating issues based on logic and reason'.

6 0
3 years ago
You are given two processors P1 and P2 that execute the same instruction set but have different architectures. The instructions
mafiozo [28]

The answer & explanation for this question is given in the attachment below.

3 0
3 years ago
The U.S. is a major player in the global media domain. However, other nations play a role in creating a dominant ideology, as ev
poizon [28]

Answer:

The answer is "Authorization for mobile wiretaps on American citizens."

Explanation:

  • The term media includes advertising, documentaries, painting, and graphic, which allows you to use a blog's domain name, and it can also provide you to create a fake profile.
  • In certain countries, a prevailing philosophy is performed, as demonstrated by the prevalence of telecommunications wiretaps approved in Sweden by US residents.
6 0
2 years ago
An intruder with malicious intent breaks into an office and steals a hard drive
kakasveta [241]

Answer: data security

Explanation:

The company's authentication protocols and data encryption measures denotes data security.

Data security simply means protecting unauthorized people from having access to a particular data. This is done to prevent unauthorized access of important information or to prevent fraud. Some of the data security measures include data encryption, tokenization, hashing, etc.

4 0
3 years ago
Other questions:
  • A binary search can be performed only on ____.
    6·1 answer
  • When a hardware or software interrupt occurs, the CPU calls________
    14·1 answer
  • Consider the following code: x = 17 y = 5 print (x % y) What is output?
    10·1 answer
  • What is one method that can be used to open the Microsoft Word application?
    8·1 answer
  • Are the buying and selling of stocks centralized activities? Why or why not?
    10·2 answers
  • 1. Some of the music in the 1960s was used to protest social and political issues. Is music still used as a form of protest? Why
    13·1 answer
  • A data center needs to ensure that data is not lost at the system level in the event of a blackout. Servers must stay operable f
    6·1 answer
  • Describe the major elements and issues with system prototyping​
    7·1 answer
  • JAVA<br>plzz help...............​
    7·1 answer
  • In which of the following situations would it be most appropriate to choose lossy compression over lossless compression?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!