Design Simple Calculator using if else condition || Find Grade using if else condition

Introduction

                                  In this article we discuss if else condition in c# which is very import. if else condition are used in all language. Its used for decision making.

Design a simple calculator using if else that prompts the user to select operator.

1. Enter two numbers

2. Enter ‘+’ for addition

3. Enter ‘-‘ for subtraction

4. Enter ‘*’ for multiplication

5. Enter ‘/’ for division

6. Enter your choice

Procedure

Int this code i can use nested if else condition. First declare 3 floats for two number which are use to get user input and third float use for store result. Addition, Subtraction, Division and Multiplication condition check in string. First if else condition check user input.

Code

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

namespace Qustions
{
    class Program
    {
        static void Main(string[] args)
        {




            float numb1, numb2, result;

            string choice;

            Console.WriteLine("Enter ‘+’ for addition \n" + "Enter ‘-‘ for subtraction \n" + "Enter ‘*’ for multiplication  \n" + "Enter ‘/’ for division");
            Console.WriteLine("Enter your choice: ");
            choice = Console.ReadLine();


            if (choice == "+" && choice == "-" && choice == "*" && choice == "/")
            {
                Console.WriteLine("Enter First Number: ");
                numb1 = float.Parse(Console.ReadLine());

                Console.WriteLine("Enter Second Number: ");
                numb2 = float.Parse(Console.ReadLine());

                if (choice == "+")
                {

                    result = numb1 + numb2;
                    Console.WriteLine("Result is: " + result);
                }
                else if (choice == "-")
                {
                    result = numb1 - numb2;
                    Console.WriteLine("Result is: " + result);
                }

                else if (choice == "*")
                {
                    result = numb1 * numb2;
                    Console.WriteLine("Result is: " + result);
                }
                else if (choice == "/")

                {
                    result = numb1 / numb2;
                    Console.WriteLine("Result is: " + result);
                }
            }

            else
            {
                Console.WriteLine("Enter Wrong");
            }
            Console.ReadKey();
    
        }

       
    }
}

Output


 

Write a program using nested if else that takes numbers from user and prints grade. Grade division is as follows.

90 A
80 B
70 C
60 D
50 E
< 50 F
Using if else condition.

Code

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

namespace Qustions
{
    class Program
    {
        static void Main(string[] args)
        {
            float TotalMarks,GainMarks,TotalPercentage=100;

            

            Console.WriteLine("Enter Total Marks: ");
            TotalMarks = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter Gain Marks: ");
            GainMarks = int.Parse(Console.ReadLine());

           float DivideResult = GainMarks /TotalMarks;


            float percentage = DivideResult * TotalPercentage;

            Console.WriteLine("\n\nYour Percentage is : "+percentage+"\n\n");


            if(percentage>=90)
            {
                Console.WriteLine("Your Gade is A");

            }



          else if (percentage >= 80 && percentage <90) { Console.WriteLine("Your Gade is B"); } else if (percentage >= 70 && percentage < 80) { Console.WriteLine("Your Gade is C"); } else if (percentage >= 60 && percentage < 70) { Console.WriteLine("Your Gade is D"); } else if (percentage >= 50 && percentage < 60)
            {
                Console.WriteLine("Your Gade is E");

            }


            else if (percentage <50)
            {
                Console.WriteLine("Your Gade is F");

            }



            Console.ReadKey();
        }

    }
}

Output

 

One thought on “Design Simple Calculator using if else condition || Find Grade using if else condition”

Leave a Reply

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