C 语言计算电费程序

编写一个 C 语言电费计算程序,并附带示例。为此,我们将使用 Else If 语句。

C 语言电费计算程序示例

此程序帮助用户输入用户消耗的电量单位。然后,它将计算总额。这种计算电费的方法在电费部门对不同单位收取不同费率时非常有用。在本示例中,我们使用 Else If 语句。

#include <stdio.h>
 
int main()
{
	int Units;
	float Amount, Sur_Charge, Total_Amount;
  	
	printf("\n Please Enter the Units that you Consumed  :  ");
  	scanf("%d", &Units);
  
  	if (Units < 50)
  	{
             Amount = Units * 2.60;
  		Sur_Charge = 25;  	
  	} 
  	else if (Units <= 100)
  	{
  		// First Fifty Units charge is 130 (50 * 2.60)
  		// Next, we are removing those 50 units from total units
  		Amount = 130 + ((Units - 50 ) * 3.25);
  		Sur_Charge = 35; 	
  	}
  	else if (Units <= 200)
  	{
  		// First Fifty Units charge is 130, and 50 - 100 is 162.50 (50 * 3.25)
  		// Next, we are removing those 100 units from total units
  		Amount = 130 + 162.50 + ((Units - 100 ) * 5.26);
  		Sur_Charge = 45; 	
  	}
  	else
  	{
  		// First Fifty Units 130, 50 - 100 is 162.50, and 100 - 200 is 526 (100 * 5.65)
  		// Next, we are removing those 200 units from total units
	   	Amount = 130 + 162.50 + 526 + ((Units - 200 ) * 7.75); 
	   	Sur_Charge = 55; 
	}
	
	Total_Amount = Amount + Sur_Charge;
	printf("\n Electricity Bill  =  %.2f", Total_Amount); 
	
  	return 0;
}
Program to Calculate Electricity Bill 1

我再试试另一个值。

 Please Enter the Units that you Consumed  :  265

 Electricity Bill  =  1377.25

查找电费示例 2

程序 用于查找电费,如果电费部门实行统一费率,则很有用。例如:如果您消耗的单位在 300 到 500 之间,则每个单位的费用将固定为 7.75 卢比,依此类推。

C 编程中的 Else If 语句 将检查第一个条件。如果为 TRUE,它将执行该块中的语句。如果条件为 FALSE,它将检查下一个(Else If 条件),依此类推。

#include <stdio.h>
 
int main()
{
	int Units;
	float Amount, Sur_Charge, Total_Amount;
  	
	printf("\n Please Enter the Units that you Consumed  :  ");
  	scanf("%d", &Units);
  
       // If it is True then Statements inside this block will be executed
  	if (Units > 500)
  	{
  		Amount = Units * 9.65;
  		Sur_Charge = 85;  	
  	} // Otherwise (fails), Compiler will move to Next Else If block 
  	else if (Units >= 300)
  	{
  		Amount = Units * 7.75;
  		Sur_Charge = 75; 	
  	} 
  	else if (Units >= 200)
  	{
  		Amount = Units * 5.26;
  		Sur_Charge = 55; 	
  	}  
  	else if (Units >= 100)
  	{
  		Amount = Units * 3.76;
  		Sur_Charge = 35; 	
  	} // Otherwise (fails), Compiler will move to Else block   	
  	else
  	{
	   	Amount = Units * 2.25; 
	   	Sur_Charge = 25; 
	}
	
	Total_Amount = Amount + Sur_Charge;
	printf("\n Electricity Bill  =  %.2f", Total_Amount); 
	
  	return 0;
}
Calculate Electricity Bill Example 2

评论已关闭。