using System; public class MainClass { static void Main(string[] args) { // Change the console appearance and redisplay. Console.Title = "Resized Console"; Console.ResetColor(); Console.Clear(); Console.SetWindowSize(100, 50); Console.BufferHeight = 500; Console.BufferWidth = 100; Console.CursorLeft = 20; Console.CursorSize = 50; Console.CursorTop = 20; Console.CursorVisible = false; } }
Author: coder
Displaying the sum of two numbers input from the keyboard.
using System; public class Addition { public static void Main( string[] args ) { int number1; int number2; int sum; Console.Write( "Enter first integer: " ); number1 = Convert.ToInt32( Console.ReadLine() ); Console.Write( "Enter second integer: " ); number2 = Convert.ToInt32( Console.ReadLine() ); sum = number1 + number2; Console.WriteLine( "Sum is {0}", sum ); } }
Printing multiple lines of text with string formatting.
using System; public class Welcome4 { public static void Main( string[] args ) { Console.WriteLine( "{0} {1}", "Welcome to", "C# Programming!" ); } }
Printing multiple lines with a single statement.
using System; public class Welcome3 { public static void Main( string[] args ) { Console.WriteLine( "Welcome to C# Programming!" ); } }
Printing one line of text with multiple statements.
using System; public class Welcome2 { public static void Main(string[] args) { Console.Write("Welcome to "); Console.WriteLine("C# Programming!"); } }
WriteLine and Write
using System; class ConsoleAdder { public static void Main() { int a = 15; int b = 744; int c = a + b; Console.Write("The sum of "); Console.Write(a); Console.Write(" and "); Console.Write(b); Console.Write(" equals "); Console.WriteLine(c); Console.WriteLine("The sum of " + a + " and " + b + " equals " + c); Console.WriteLine("The sum of {0} and {1} equals {2}", a, b, c); } }
Read a line of string and check its length
using System;
class MainEntryPoint {
static void Main(string[] args) {
Console.WriteLine(“Type in a string”);
string input;
input = Console.ReadLine();
if (input == “”) {
Console.WriteLine(“You typed in an empty string”);
} else if (input.Length < 5) {
Console.WriteLine("The string had less than 5 characters");
} else if (input.Length < 10) {
Console.WriteLine("The string had at least 5 but less than 10 characters");
}
Console.WriteLine("The string was " + input);
}
}
[/csharp]