Pages

Monday, 29 January 2018

Declaring constants in C#.NET

Declaring constants in C#.NET
using System;

namespace Constants
{
    class Program
    {
        static void Main(string[] args)
        {
            //declaring const variable PI
            const double PI = 3.14;
            Console.WriteLine("*** www.ProgrammingPosts.blogspot.com ***");
            Console.WriteLine(">>> c# Program to find Area and Perimeter of Circle <<< \n");
            Console.Write("Enter radius value: ");
            int r = Convert.ToInt32(Console.ReadLine()); //taking radius as input
            double area = PI * r * r; //getting area of circle
            Console.WriteLine("Are of circle is: " + area);

            //if we try to assign a value to PI , we get compile-time error
            //PI = 123;

            double perimeter = 2 * PI * r; //getting perimeter of circle
            Console.WriteLine("Perimeter of circle: " + perimeter);
         
            Console.Read();
        }
    }

}