How to calculate and display electricity used in a month using fprintf?

7 visualizaciones (últimos 30 días)
Given: A power company charges 6.6 cents per KWH of electricity.
Find: Write a script that prompts the user for the number of KWH's they used in a month. The script should then calculate the total elecericty charge for the month and display it in dollars, with 2 decimals.
...
I got it figured out :
n = input('How many kWh this month: ');
fprintf('Your charge for 200 kWh will be $%.2f\n', 0.066*n);

Respuesta aceptada

Stephen23
Stephen23 el 23 de Feb. de 2024
Editada: Stephen23 el 21 de Mzo. de 2024
Note that the assignment does not require that you also print the number of kWH, only the charge is required:
n = str2double(input('How many kWH this month: ','s'));
fprintf('Your charge will be $%.2f\n', 0.066*n)
  1 comentario
Spaceman
Spaceman el 25 de Feb. de 2024
Editada: Spaceman el 21 de Mzo. de 2024
Eureka! I overcomplicate everything when it comes to MATLAB for some reason. If you put this in an answer I can accept. :)

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 23 de Feb. de 2024
KWH = input('How many KWH this month: ');
BILL = 0.066 * KWH;
fprintf('Your charge for %f KWH will be $%.2f\n', KWH, BILL)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by