Circuit Power Calculator in C#

Problem Statement

We need to create a C# program that calculates the power consumed by an electrical circuit. The formula to calculate power (P) in an electrical circuit is given by: P=V×I.
where:

  • ( P ) is the power in watts (W),
  • ( V ) is the voltage in volts (V), and
  • ( I ) is the current in amperes (A).

Our program will prompt the user to enter the voltage and current values, then compute and display the power.

Logic and Approach

  1. Understanding the Formula: The power consumed by an electrical circuit depends on the product of voltage and current.
  2. User Input: The program should accept input for voltage and current from the user.
  3. Calculation: Using the formula ( P = V \times I ), the program calculates the power.
  4. Output: The result is then displayed to the user.

C# Program: Circuit Power Calculator

C#
using System;

namespace CircuitPowerCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            // Introduction message
            Console.WriteLine("Welcome to the Circuit Power Calculator!");

            // Getting the voltage input from the user
            Console.Write("Enter the voltage (in volts): ");
            double voltage = Convert.ToDouble(Console.ReadLine());

            // Getting the current input from the user
            Console.Write("Enter the current (in amperes): ");
            double current = Convert.ToDouble(Console.ReadLine());

            // Calculating the power
            double power = CalculatePower(voltage, current);

            // Displaying the result
            Console.WriteLine($"The power consumed by the circuit is {power} watts.");
        }

        // Method to calculate power based on voltage and current
        static double CalculatePower(double voltage, double current)
        {
            return voltage * current;
        }
    }
}

Code Explanation

1. Namespace and Class Declaration

C#
namespace CircuitPowerCalculator
{
    class Program
    {
  • namespace CircuitPowerCalculator: Namespaces are used to organize code and prevent name conflicts. Here, CircuitPowerCalculator is the namespace that encapsulates our program.
  • class Program: This is the main class of our application. In C#, every application must have a Main method within a class.

2. Main Method

C#
static void Main(string[] args)
{
    // Introduction message
    Console.WriteLine("Welcome to the Circuit Power Calculator!");
  • static void Main(string[] args): The Main method is the entry point of our C# application. The static keyword means the method belongs to the class itself rather than an instance of the class. void indicates that this method doesn’t return any value.
  • Console.WriteLine("Welcome to the Circuit Power Calculator!");: This line prints a welcome message to the console.

3. User Input for Voltage

C#
Console.Write("Enter the voltage (in volts): ");
double voltage = Convert.ToDouble(Console.ReadLine());
  • Console.Write("Enter the voltage (in volts): ");: This line prompts the user to enter the voltage value.
  • double voltage = Convert.ToDouble(Console.ReadLine());: This line reads the user input as a string, converts it to a double type, and assigns it to the voltage variable.

4. User Input for Current

C#
Console.Write("Enter the current (in amperes): ");
double current = Convert.ToDouble(Console.ReadLine());
  • Similar to the voltage input, this code prompts the user for the current and converts the input to a double type.

5. Power Calculation

C#
double power = CalculatePower(voltage, current);
  • double power = CalculatePower(voltage, current);: This line calls the CalculatePower method, passing the voltage and current as arguments, and assigns the returned value (power) to the power variable.

6. CalculatePower Method

C#
static double CalculatePower(double voltage, double current)
{
    return voltage * current;
}
  • static double CalculatePower(double voltage, double current): This is a static method that calculates the power based on the given voltage and current. It takes two double arguments and returns a double.
  • return voltage * current;: This line calculates the power by multiplying the voltage and current and returns the result.

7. Display the Result

C#
Console.WriteLine($"The power consumed by the circuit is {power} watts.");
  • Console.WriteLine(...): This line prints the calculated power to the console.

Conclusion

This simple C# program demonstrates the process of calculating the power consumption of an electrical circuit using user-provided voltage and current values. The program introduces fundamental concepts such as reading user input, type conversion, method creation, and basic arithmetic operations.

By understanding and using this code, you can build a foundation in C# programming and expand to more complex applications.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *