Multiply If Not Zero - C# Programming Exercise

In this C# exercise, you will learn how to create a program that asks the user for a number. If the entered number is not zero, the program will ask for a second number and display their sum. Otherwise, it will simply display "0". This type of exercise is useful for practicing conditional logic and handling user input using structures like if. The concept of making decisions based on specific conditions is fundamental in C# programming.

By completing this exercise, you will enhance your skills in creating interactive programs that make decisions based on user inputs, a crucial skill for developing more complex applications in C#.

 Category

Flow Control

 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

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"
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Divide if not zero

    In this C# exercise, you will learn how to create a program that asks the user for two numbers. If the second number is not zero, the program will perform the division...

  •  Divide if not zero (Using else)

    In this C# exercise, you will learn how to modify the previous program using the else control structure. The program will ask the user for two numbers, and if the sec...

  •  Greatest of three numbers

    In this C# exercise, you will learn how to write a program that prompts the user to enter three numbers and displays the greatest one. The program will use conditional struc...

  •  Repeat until 0

    In this C# exercise, you will learn how to write a program that asks the user to enter a number "x" and displays the result of multiplying it by 10. The program will keep as...

  •  Repeat until 0 (Use Do While)

    In this C# exercise, you will learn how to write a program that asks the user to enter a number "x" and displays the result of multiplying it by 10. The program will keep re...

  •  While + Counter

    In this C# exercise, you will learn how to write a program that displays the numbers from 1 to 10 on the screen using a while loop. The while loop is a control...