C# Program to Convert a Positive Number into Negative

This C# program is designed to convert a positive integer into its negative counterpart. The process is achieved by utilizing a simple mathematical operation – multiplying the positive number by -1. The program provides a clear user interface, guiding the user to input a positive number, processing the conversion, and then displaying both the original positive number and its corresponding negative value.

Problem statement

You are required to write a C# program that converts a positive integer into its negative counterpart. Your program should take a positive integer as input, perform the conversion, and then display the resulting negative number.

C# Program to Convert a Positive Number into Negative

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a positive number: ");
        int positiveNumber = Convert.ToInt32(Console.ReadLine());

        int negativeNumber = ConvertToNegative(positiveNumber);

        Console.WriteLine($"The negative of {positiveNumber} is {negativeNumber}");
    }

    static int ConvertToNegative(int number)
    {
        return -number;
    }
}

How it works

  1. Program Structure:
    • The program starts with the declaration of the Program class.
    • Inside this class, we define two methods: Main and ConvertToNegative.
  2. Main Method:
    • The Main method is the entry point of the program.
    • It first prompts the user to enter a positive number using Console.Write("Enter a positive number: ").
    • It then reads the user’s input using Console.ReadLine() and converts it to an integer using Convert.ToInt32(...).
    • This positive number is stored in the variable positiveNumber.
  3. ConvertToNegative Method:
    • This method takes an integer number as input and returns its negation.
    • Inside the method, it uses the unary minus (-) operator to negate the input and returns the result.
  4. Main Method (Continued):
    • After obtaining the positive number, the program calls the ConvertToNegative method, passing positiveNumber as an argument. This converts the positive number into its negative equivalent.
    • The result is stored in the variable negativeNumber.
  5. Display the Result:
    • Finally, the program uses Console.WriteLine(...) to display the original positive number and its negative counterpart. It uses string interpolation ($) to incorporate the variables into the output message.
    For example, if the user enters 25 as the positive number, the program will output:

Code: The negative of 25 is -25

  • The program works by taking a positive number, passing it through the ConvertToNegative method, and then displaying the original and negated values.

Please note that the program assumes that the user will input a valid positive integer. If the user enters something that’s not a valid integer (like a letter or a decimal), the program might throw an exception. In a real-world application, you would want to add error handling to account for such cases.

Input/Output

Share:

Leave A Reply

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

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...