Skip to content
The Programming School

The Programming School

  • Home
  • About
  • Contact Us
  • Toggle search form

Variables and Input examples in C# Language

Here are some Variables and Input examples in C# Language

Simple Integer Input and Output:

using System;

class Program
{

static void Main()
{

Console.Write("Enter an integer: ");

int userInput = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("You entered : " + userInput);

}
}

• Uses ' Console.Write ' to prompt the user to enter an integer.

• Reads the user input using ' Console.ReadLine ' and converts it to an integer using ' Convert.ToInt32 ' .

• Displays the entered integer using ' Console.WriteLine ' .

Double Input:

using System;

class Program
{

static void Main()
{

Console.Write("Enter a double : ");

double userInput = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("You entered : " + userInput);

}
}

• This program prompts the user to enter a double (floating-point nymber) .

• It uses ' Console.ReadLine() ' to read the user input as a string .

• ' Convert.ToDouble ' is used to convert the string input to a double .

• The entered double is then displayed using ' Console.WriteLine ' .

Sum of Two Integers:

using System;

class Program
{

static void Main()
{

Console.Write("Enter the first integer : ");

int num1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second integer : ");

int num2 = Convert.ToInt32(Console.ReadLine());

int sum = num1 + num2;

Console.WriteLine("The sum of " + num1 + " and " + num2 + " is : " + sum);

}
}

• Prompts the user to enter the first integer using ' Console.Write ' and reads the input using ' Console.ReadLine ' . Converts the input to an integer using ' Convert.ToInt32 ' .

• Prompts the user to enter the second integer in a similar manner .

• Calculates the sum of the two integers using the ' + ' operator .

• Displays the result using ' Console.WriteLine ' .

String Input:

using System;

class Program
{

static void Main()
{

Console.Write("Enter a string : ");

string userInput = Console.ReadLine();

Console.WriteLine("You entered : " + userInput);

}
}

• This Program prompts the user to enter a string of characters.

• It uses ' Console.ReadLine() ' to directly read the user input as a string.

• The entered string is then displayed using ' Console.WriteLine ' .

Average of Three Integers:

using System;

class Program
{

static void Main()
{

Console.Write("Enter the first integer : ");

int num1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter second integer : ");

int num2 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the third integer : ");

int num3 = Convert.ToInt32(Console.ReadLine());

double average = (num1 + num2 + num3) / 3.0;

Console.WriteLine($"The average of {num1} , {num2} , and {num3} is : {average}");

}
}

• The program prompts the user to enter three integers using ' Console.Write ' and ' Console.ReadLine ' .

• ' Convert.ToInt32 ' is used to convert the user input (strings) to integers .

• The three integers are stored in variables ' num1 ' , ' num2 ' , and ' num3 ' .

• The average is calculated by adding the three integers and dividing the sum by 3.0 to ensure a decimal result (' / 3.0 ') .

• The result is displayed using ' Console.WriteLine ' along with the entered integers and the calculated average .

  • Home
  • About
  • Contact Us

Copyright © 2025 The Programming School.

Powered by PressBook Masonry Dark