Programming Course in C# ¡Free!

Divide if not zero

 Friday, April 05, 2013 published by Exercises C#
Proposed exercise

Write a C# program to ask the user for two numbers, and show their division if the second number is not zero; otherwise, it will display "I cannot divide".

Output



Solution


using System;
public class DivideZero
{
public static void Main()
{
int number1, number2;

Console.Write("Enter the first number:");
number1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second number:");
number2 = Convert.ToInt32(Console.ReadLine());

if (number1 != 0 && number2 != 0)
{
Console.WriteLine("The result for {0} / {1} is {2}", 
number1, number2, number1/number2);
}
else
Console.WriteLine("Result is 0");
}