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

조회 수: 2 (최근 30일)
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);

채택된 답변

Stephen23
Stephen23 2024년 2월 23일
편집: Stephen23 2024년 3월 21일
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
Spaceman
Spaceman 2024년 2월 25일
편집: Spaceman 2024년 3월 21일
Eureka! I overcomplicate everything when it comes to MATLAB for some reason. If you put this in an answer I can accept. :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by