Exercise
Multiply If Not Zero
Objective
Write a C# program to ask the user for a number; if it is not zero, then it will ask for a second number and display their sum; otherwise, it will display "0"
Write Your C# Exercise
C# Exercise Example
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
int firstNumber; // Declaring a variable to store the first number entered by the user
int secondNumber; // Declaring a variable to store the second number entered by the user
// Asking the user to enter the first number and reading the input
Console.Write("Enter a number: ");
firstNumber = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer
// Checking if the first number is not zero
if (firstNumber != 0) // If the first number is not zero
{
// Asking the user to enter the second number and reading the input
Console.Write("Enter a second number: ");
secondNumber = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer
// Displaying the sum of the two numbers
Console.WriteLine("The sum is: {0}", firstNumber + secondNumber); // Printing the sum
}
else // If the first number is zero
{
// Displaying "0" if the first number is zero
Console.WriteLine("0"); // Printing "0"
}
}
}