PRACTICAL 1: CONSOLE APPLICATIONS AND RAZOR PAGE

A. CONSOLE APPLICATIONS

STEPS (same for every program):
1. Open Visual Studio and click "Create a new project".
2. Select "Console App" and click Next.
3. Give the project name and directory (location), then click Create.
4. Write the code given below in Program.cs.
5. Press F5 (or Ctrl+F5) to run the program and enter the values.


1. CALCULATOR

CODE:
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter number 1: ");
        double a = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter number 2: ");
        double b = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("1. Add  2. Subtract  3. Multiply  4. Divide");
        int opt = Convert.ToInt32(Console.ReadLine());

        switch (opt)
        {
            case 1: Console.WriteLine("Sum: " + (a + b)); break;
            case 2: Console.WriteLine("Difference: " + (a - b)); break;
            case 3: Console.WriteLine("Product: " + (a * b)); break;
            case 4: Console.WriteLine("Quotient: " + (a / b)); break;
            default: Console.WriteLine("Wrong option"); break;
        }
    }
}

OUTPUT: (inputs 5 and 2, option 4)
Enter number 1: 5
Enter number 2: 2
1. Add  2. Subtract  3. Multiply  4. Divide
4
Quotient: 2.5


2. EVEN ODD

CODE:
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int num = Convert.ToInt32(Console.ReadLine());

        if (num % 2 == 0)
            Console.WriteLine(num + " is an even number");
        else
            Console.WriteLine(num + " is an odd number");
    }
}

OUTPUT: (input 7)
Enter a number: 7
7 is an odd number


3. CELSIUS AND FAHRENHEIT

CODE:
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter temperature: ");
        double t = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("1. Celsius to Fahrenheit  2. Fahrenheit to Celsius");
        int opt = Convert.ToInt32(Console.ReadLine());

        if (opt == 1)
            Console.WriteLine("Fahrenheit: " + (t * 1.8 + 32));
        else if (opt == 2)
            Console.WriteLine("Celsius: " + (t - 32) / 1.8);
        else
            Console.WriteLine("Wrong option");
    }
}

OUTPUT: (input 100, option 1)
Enter temperature: 100
1. Celsius to Fahrenheit  2. Fahrenheit to Celsius
1
Fahrenheit: 212


4. ARMSTRONG NUMBER

CODE:
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int num = Convert.ToInt32(Console.ReadLine());

        int original = num;
        int sum = 0;
        int digits = num.ToString().Length;

        while (num > 0)
        {
            int rem = num % 10;
            sum += (int)Math.Pow(rem, digits);
            num = num / 10;
        }

        if (sum == original)
            Console.WriteLine(original + " is an Armstrong number");
        else
            Console.WriteLine(original + " is not an Armstrong number");
    }
}

OUTPUT: (input 153)
Enter a number: 153
153 is an Armstrong number


5. PALINDROME NUMBER

CODE:
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int num = Convert.ToInt32(Console.ReadLine());

        int original = num;
        int reverse = 0;

        while (num > 0)
        {
            int rem = num % 10;
            reverse = reverse * 10 + rem;
            num = num / 10;
        }

        if (reverse == original)
            Console.WriteLine(original + " is a palindrome number");
        else
            Console.WriteLine(original + " is not a palindrome number");
    }
}

OUTPUT: (input 121)
Enter a number: 121
121 is a palindrome number


6. FACTORIAL

CODE:
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        int num = Convert.ToInt32(Console.ReadLine());

        int fact = 1;

        if (num < 0)
        {
            Console.WriteLine("Factorial is not defined for negative numbers");
        }
        else
        {
            for (int i = 1; i <= num; i++)
            {
                fact = fact * i;
            }

            Console.WriteLine("Factorial: " + fact);
        }
    }
}

OUTPUT: (input 5)
Enter a number: 5
Factorial: 120


7. PRIME NUMBERS IN A RANGE

CODE:
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter starting number: ");
        int start = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter ending number: ");
        int end = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Prime numbers:");

        for (int num = start; num <= end; num++)
        {
            int count = 0;

            for (int i = 1; i <= num; i++)
            {
                if (num % i == 0)
                {
                    count++;
                }
            }

            if (count == 2)
            {
                Console.WriteLine(num);
            }
        }
    }
}

OUTPUT: (range 1 to 20)
Enter starting number: 1
Enter ending number: 20
Prime numbers:
2
3
5
7
11
13
17
19


B. RAZOR PAGE

STEPS:
1. Create a new project and select "ASP.NET Core Web Application", then click Next.
2. Give the project name and click Create.
3. Select the "Empty" template and click Create.
4. In Solution Explorer, right click on the project -> Add -> New Item.
5. Select "Razor Page" and click Add (e.g. Index.cshtml).

Index.cshtml generated by the Razor Page template:
@page
@model WebApplication1.IndexModel
@{
}
