• This C# program uses the ' Console.WriteLine ' method to output the specified string to the console.
• The ' Main ' method is the entry point of the program, and it gets executed when the program is run.
• ' using System; ' is an import statement that allows the program to use types from the ' System ' namespace, which includes the ' Console ' class used for console input and output.
Variables and Input:
using System;
class Program {
static void Main() {
double height;
Console.Write("Enter your height in meters: ");
height = Convert.ToDouble(Console.ReadLine());
double doubledHeight = height * 2;
Console.WriteLine("Your height doubles is: " + doubledHeight);
} }
• Declares a double variable named ' height ' .
• Prompts the user to enter their height in meters, reads the input, and converts it to a double .
• Performs a simple calculation ( doubling the height) and stores the result in the ' doubledHeight ' variable .
• Displays the result using ' Console.WriteLine ' .
Arithmetic Operations:
using System;
class Program {
static void Main() {
int num1 = 2;
int num2 = 4;
int product = num1 * num2;
Console.WriteLine($"Product: {product}");
} }
• Declares two integer variables (' num1 ' and ' num2 ') .
• Performs multiplication using the ' * ' operator and stores the result in the ' product ' variable .
• Displays the result using ' Console.WriteLine ' .
Conditional Statements:
using System;
class Program {
static void Main() {
Console.Write("Enter your age : ");
int age = Convert.ToInt32(Console.ReadLine());
if (age >= 18)
{
Console.WriteLine("You are eligible to vote. ");
}
else
{
Console.WriteLine("You are not eligible to vote yet. ");
} } }
• The ' if-else ' statement checks the condition ' (age >= 18) ' .
• If the condition is true, it executes the code block isnide the first set of curly braces.
• If the condition is false, it executes the code block inside the second set of curly braces (in the ' else ' block) .
• In this example, it determines whether the user is eligible to vote based on their age .
For Loop:
using System;
class Program {
static void Main() {
int sum = 0;
for (int i = 1; i <= 5; i++)
{
sum += i;
}
Console.WriteLine("Sum : " + sum);
} }
• This ' for ' loop initializes ' i ' to 1, iterates as long as ' i ' is less than or equal to 5, and increments ' i ' after each iteration .
• Inside the loop , it adds the value of ' i ' to the ' sum ' variable .
• After the loop , it prints the sum of the numbers from 1 to 5 .
While Loop:
using System;
class Program {
static void Main() {
Console.Write("Enter the number of stars to print : ");
int starsToPrint = Convert.ToInt32(Console.ReadLine());
int count = 0;
while (count < starsToPrint)
{
Console.Write(" * ");
count++;
} } }
• This ' while ' loop asks the user how many stars to print and prints them.
• It prompts the user to enter the number of stars using ' Console.ReadLine ' .
• It continues iterating as long as the condition (' count < starsToPrint ') is true .
• Inside the loop , it prints a star and increments the ' count ' .