April 29, 2020

Write a program that should accept two numbers from the users.

Write a program that should accept two numbers from the users and perform the following mathematical operations:
  • Addition
  • Subtraction
  • Multiplication 
  • Division
In case of division, an error message should be displayed if the second number is zero.

Write a program that should accept two numbers from the users.Addition,Subtraction, Multiplication, Division

Sample Solution

C# Sharp Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
    class Program
    {
   static int r, n, n1;
   public static void addition()
    {
     r = n + n1;
     Console.WriteLine(" The sum of two number is : {0}", r);
     }
   public static void subraction()
    {
    r = n - n1;
 Console.WriteLine(" The subtraction of two number is : {0}", r);
    }
    public static void multiplication()
    {
    r = n * n1;
 Console.WriteLine("The multiplication of two number is : {0}", r);
    }
   public static void division()
    {
       if (n1 == 0)
         {
         Console.WriteLine(" you cannot divide by 0 ");
         }
       else
        {
        r = n / n1;
 Console.WriteLine(" The division of two number is : {0}", r);
        }
        }
        static void Main(string[] args)
        {

            Console.WriteLine("Enter the first number");

            n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the second number");

            n1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Main menu");
            Console.Write("1.Addition -> ");
            addition();
            Console.Write("2.subtraction -> ");
            subraction();
            Console.Write("3.Multiplication -> ");
            multiplication();
            Console.Write("4.Division -> ");
            division();

            Console.ReadLine();

        }


    }
}

                                                               Sample Output

Write a program that should accept two numbers from the users.Addition,Subtraction, Multiplication, Division




No comments:

Post a Comment