Wednesday March 28th, 2018

C# If Else

By Ebubekir Sezer

In programming language, to make decisions are important and for make a decision, we use some terms. I will use If Else terms in C#.Actually, I was talk about the if else terms in C programming language but For the starting programming with the C#, they can click here and they can read my essay.

Program will ask the user income for a month and then program will calculate how much of the income will go for the tax. I will use if else terms. Code Sequence;

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

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

            double income;
            double tax;
            Console.WriteLine("Income for a month");
            income = double.Parse(Console.ReadLine());
            if (income < 1000)
            {
                tax = income * 0.1;
            }
            else if (income > 1000 && income < 2000)
            {
                tax = income * 0.3;
            }
            else
            {
                tax = income * 0.5;
            }
            Console.WriteLine("The tax you have to pay is :" + tax);
            Console.ReadKey();
        }
    }
}

Screen Output;

Income for a month
1454.5
The tax you have to pay is :7272,5

Now, program will get number from the user and will ask the user how much you want to add to the number and then program will press the sum on the screen but if the user want to out of the program user have to press -1. Code Sequence;

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

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

            int number;
            int add;
            int sum = 0;
            int a = 1;
            //I define the a because i want to keep going with the while loop until user want to out
            while (a > 0)
            {
                Console.WriteLine("Enter the number(Press -1 for the out):");
                number = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter the add number:");
                add = int.Parse(Console.ReadLine());
                if (number == -1)
                {
                    break;
                }
                else
                {
                    sum = number + add;

                }
                Console.WriteLine("Sum:" + sum);
                sum = 0;
            }
            Console.ReadKey();
        }
    }
}

Screen Output;

Enter the number(Press -1 for the out):
456
Enter the add number:
32
Sum:488
Enter the number(Press -1 for the out):
45
Enter the add number:
-46
Sum:-1
Enter the number(Press -1 for the out):
12
Enter the add number:
-12
Sum:0
Enter the number(Press -1 for the out):
-1
Enter the add number:
0