class 3.2 - sandyprati - c#.net - programs - console applications
DESCRIPTION
\\ C#.NetTRANSCRIPT
SandyPrati C#.NET – Programs Console 26 [email protected]
Class 3.2 – Agenda Implement Console Application Programs using C#.NET
ConApp01: Program to Display SandyPrati on Console and Escape Sequenceusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp01{ class Program { static void Main(string[] args) // Entry Point { // Using - Console.Write - Display Text Entire Text in same line
Console.Write("Sandy"); Console.Write("Prati");
// Using - Console.WriteLine - Display Text Entire Text in different lines
Console.WriteLine("Sandy"); Console.WriteLine("Prati"); // Usage of Escape Sequence - \n - To create a New Line
Console.WriteLine("\n");
// Usage of Escape Sequence - \t - To create a new Horizontal Tab
Console.WriteLine("Sandy \t Prati"); } }}
ConApp02: Program on basic Data Types - Integral, Floating, Miscellaneoususing System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConApp02{ class Program { static void Main(string[] args) { /*
Dedicated to my Love
SandyPrati C#.NET – Programs Console 27 [email protected]
Program on basic Data Types - Integral, Floating, Miscellaneous */
// Integral Data Types
byte a = 19; // Only Positive sbyte b = -23; short c = 1923; ushort d = 19; // Only Positive int e = 2319; uint f = 19; // Only Positive long g = 19191919; ulong h = 23232323; // Only Positive
// Display Values of Integral Data Types
Console.WriteLine(" A \t = " + a.ToString()); Console.WriteLine(" B \t = " + b.ToString()); Console.WriteLine(" C \t = " + c.ToString()); Console.WriteLine(" D \t = " + d.ToString()); Console.WriteLine(" E \t = " + e.ToString()); Console.WriteLine(" F \t = " + f.ToString()); Console.WriteLine(" G \t = " + g.ToString()); Console.WriteLine(" H \t = " + h.ToString());
float i = 19.23232323F; // 8 Decimal Precision double j = 23.19191919191919; // 16 Decimal Precision decimal k = 19.2319231923192319231923192319M; // 28 Decimal Precision
// Display Values of Floating Data Types
Console.WriteLine(" I \t = " + i.ToString()); Console.WriteLine(" J \t = " + j.ToString()); Console.WriteLine(" K \t = " + k.ToString());
char l = 'P'; string m = "Prati"; bool n = true; object o = new object();
// Display Values of Miscellaneous Data Types
Console.WriteLine(" L \t = " + l.ToString()); Console.WriteLine(" M \t = " + m.ToString()); Console.WriteLine(" N \t = " + n.ToString()); Console.WriteLine(" O \t = " + o.ToString()); } }
Dedicated to my Love
SandyPrati C#.NET – Programs Console 28 [email protected]
}
ConApp03: Program on Static Definition of a variableusing System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConApp03{ class Program { static void Main(string[] args) { // Program on Static Definition of a Variable Declared
// Note: A variable declared need to be initialized in MS.NET
Int32 n; // Variable Declaration
n = 19; // Variable Static Definition
// Display Variable Value
Console.WriteLine(" Value of N is " + n.ToString()); } }}
ConApp04: Program on Dynamic Definition of a variableusing System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConApp04{ class Program { static void Main(string[] args) { // Program on Dynamic Definition of a Variable Declared
Int32 sid; // Variable Declaration
Dedicated to my Love
SandyPrati C#.NET – Programs Console 29 [email protected]
String sname; // Variable Declaration
Console.WriteLine("\n May I Know Your ID? \t"); sid = Int32.Parse(Console.ReadLine()); // Variable Dynamic Definition
Console.WriteLine("\n May I Know Your Name? \t");
sname = Console.ReadLine(); // Variable Dynamic Definition
// Display Values
Console.WriteLine("\n My ID is \t" + sid.ToString());
Console.WriteLine("\n My Name is \t" + sname); } }}
ConApp05: Program on Arithmetic Operations - Add, Subtract, Multiply, Divide, Modulususing System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConApp05{ class Program { static void Main(string[] args) { // Program on Arithmetic Operations - Add, Subtract, Multiply, Divide, Remainder
Int32 s; Int32 p;
s = 19; p = 23;
Console.WriteLine("\n Sum of " + s.ToString() + " and " + p.ToString() + " = " + (s + p).ToString()); Console.WriteLine("\n Difference of " + s.ToString() + " and " + p.ToString() + " = " + (s - p).ToString()); Console.WriteLine("\n Product of " + s.ToString() + " and " + p.ToString() + " = " + (s * p).ToString()); Console.WriteLine("\n Quotient of " + s.ToString() + " and " + p.ToString() + " = " + (s / p).ToString()); Console.WriteLine("\n Remainder of " + s.ToString() + " and " + p.ToString() + " = " + (s % p).ToString()); }
Dedicated to my Love
SandyPrati C#.NET – Programs Console 30 [email protected]
}}
ConApp06: Program on Postfixusing System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConApp06{ class Program { static void Main(string[] args) { /* Program on Postfix 1. Source variable S is Assigned to destination variable P 2. Source variable is incremented by value 1 */
Int32 s, p;
s = 19;
p = s++; // Postfix
Console.WriteLine("\n Value of S is \t " + s.ToString());
Console.WriteLine("\n Value of P is \t" + p.ToString()); } }}
ConApp07: Program on Prefixusing System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConApp07{ class Program { static void Main(string[] args) { /* Program on Prefix
Dedicated to my Love
SandyPrati C#.NET – Programs Console 31 [email protected]
1. Source Variable S is incremented by value 1 2. Incremented Source Variable S is assigned to destined variable P */
Int32 s, p;
s = 19;
p = ++s; //Prefix
Console.WriteLine("\n value of S is \t" + s.ToString()); Console.WriteLine("\n value of P is \t" + p.ToString()); } }}
ConApp08: Program to create Student Report Cardusing System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConApp08{ class Program { static void Main(string[] args) { /* Program on Student Report Card - ID, Name, Maths, Physics, Total, Average */
Int32 sid;
String sname;
Double maths, physics, total, avg;
char gender;
Console.WriteLine("\n May I know your ID \t"); sid = Int32.Parse(Console.ReadLine());
Console.WriteLine("\n May I know your Name \t"); sname = Console.ReadLine();
Console.WriteLine("\n How much you scored in maths? \t"); maths = Double.Parse(Console.ReadLine());
Dedicated to my Love
SandyPrati C#.NET – Programs Console 32 [email protected]
Console.WriteLine("\n How much you scored in physics? \t"); physics = Double.Parse(Console.ReadLine());
total = maths + physics;
avg = (total/2);
Console.WriteLine("\n -------- Student Report Card -----------");
Console.WriteLine("\n\n");
Console.WriteLine("\n Student ID \t " + sid.ToString()); Console.WriteLine("\n Student Name \t " + sname); Console.WriteLine("\n Maths Marks \t" + maths.ToString()); Console.WriteLine("\n Physics Marks \t" + physics.ToString()); Console.WriteLine("\n Total Marks \t" + total.ToString()); Console.WriteLine("\n Average \t" + avg.ToString()); } }}
ConApp09: Program to find Area of Rectangleusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp09{ class Program { static void Main(string[] args) { /* Program to find Area of Rectangle */
double l, b, area;
l = 1.9;
b = 2.3;
area = (l * b);
Console.WriteLine("\n Area of rectangle with length " + l.ToString() + " and " + b.ToString() + " = "
+ area.ToString());
Dedicated to my Love
SandyPrati C#.NET – Programs Console 33 [email protected]
} }}
ConApp10: Program to find Area of Circle using Constant and Math Classusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp10{ class Program { static void Main(string[] args) { /* Program to find Area of Circle using Constant and Math Class */
const double pi = 3.14159;
double radius, area;
radius = 1.9;
area = (pi * Math.Pow(radius, 2));
Console.WriteLine("\n Area of Square with Radius " + radius.ToString() + " = " + area.ToString()); } }}
ConApp11: Program to calculate distance between two points S(x1,y1) and P(x2,y2)using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp11{ class Program { static void Main(string[] args) { /* Program to calculate distance between two points P1(x1,y1) and P2(x2,y2) */
Int32 x1, y1, x2, y2,dx, dy;
Dedicated to my Love
SandyPrati C#.NET – Programs Console 34 [email protected]
double distance;
x1 = 1; y1 = 9;
x2 = 2; y2 = 3;
dx = x2 - x1; dy = y2 - y1;
distance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
Console.WriteLine("\n Distance between P1(" + x1.ToString()+ "," + y1.ToString() + ") and P2(" +
x2.ToString() + "," + y2.ToString()+ ") = " + distance.ToString()); } }}
ConApp12: Program on Ternary or Conditional Operator using relational operatorsusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp12{ class Program { static void Main(string[] args) { /* Program on Conditional Operator */
Int32 numresult;
string nameresult;
numresult = (19 > 23) ? 1 : 0;
nameresult = (19 > 23) ? "Sandy" : "Prati";
Console.WriteLine("\n Num Result = \t" + numresult.ToString());
Console.WriteLine("\n Name Result = \t" + nameresult.ToString()); }
Dedicated to my Love
SandyPrati C#.NET – Programs Console 35 [email protected]
}}
ConApp13: Program on Data Type Casting - Implicit and Explicitusing System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConApp13{ class Program { static void Main(string[] args) { /* Program on Data type casting - Implicit and Explicit Casting */
Int32 myInteger; char myChar; float myFloat;
myChar = 'P'; myInteger = myChar; // Implicit Cast - Char To Int32 myInteger = (Int32) myChar; // Explicit Cast - Char To Int32
Console.WriteLine("\n Value of Casted My Integer is \t " + myInteger.ToString());
myFloat = 19.23F; //myInteger = myFloat; // Implicit Cast - Not Possible myInteger = (Int32)myFloat; // Explicit Cast - Float To Int32
Console.WriteLine("\n Value of Casted My Integer is \t " + myInteger.ToString()); } }}
ConApp14: Program to Print a Smile Face Symbol on Consoleusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp14{ class Program
Dedicated to my Love
SandyPrati C#.NET – Programs Console 36 [email protected]
{ static void Main(string[] args) { /* Program to print a smile face symbol on console */
char myChar;
Int32 myInteger = 1;
myChar = (char) myInteger;
Console.WriteLine(myChar); } }}
ConApp15: Program to Swap Two Numbers using Temp Variableusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp15{ class Program { static void Main(string[] args) { /* Program to swap two numbers using temp variable */
Int32 s, p, temp;
s = 19; p = 23;
Console.WriteLine("\n Before Swap...!");
Console.WriteLine("\n Value of S \t = " + s.ToString());
Console.WriteLine("\n Value of P \t = " + p.ToString());
// Swap Logic - Using Temp
Dedicated to my Love
SandyPrati C#.NET – Programs Console 37 [email protected]
temp = s;
s = p;
p = temp; Console.WriteLine("\n After Swap...!");
Console.WriteLine("\n Value of S \t = " + s.ToString());
Console.WriteLine("\n Value of P \t = " + p.ToString()); } }}
ConApp16: Program to Swap Two Numbers without using Temp Variableusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp16{ class Program { static void Main(string[] args) { /* Program to swap two numbers without using temp variable */
Int32 s, p;
s = 19;
p = 23;
Console.WriteLine("\n Before Swap...!");
Console.WriteLine("\n Value of S \t = " + s.ToString());
Console.WriteLine("\n Value of P \t = " + p.ToString());
// Swap Logic - Without Using Temp
s = s + p; // 42 = 19 + 23
p = s - p; // 19 = 42 - 23
Dedicated to my Love
SandyPrati C#.NET – Programs Console 38 [email protected]
s = s - p; // 23 = 42 - 19
Console.WriteLine("\n After Swap...!");
Console.WriteLine("\n Value of S \t = " + s.ToString());
Console.WriteLine("\n Value of P \t = " + p.ToString()); } }}
ConApp17: Input 4 Digit Number (1923) through keyboard and calculate the sum of its digitsusing System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConApp17{ class Program { static void Main(string[] args) { /* Program to Input 4 Digit Number (1923) through keyboard and Calculate the sum of its digits.
*/
Int32 n, d1,d2,d3,d4,q1,q2,q3,q4; n = 1923;
d1 = n % 10; // d1 = 1923 % 10 = 3
q1 = n / 10; // q1 = 1923 / 10 = 192
d2 = q1 % 10; // d2 = 192 % 10 = 2
q2 = q1 / 10; // q2 = 192 / 10 = 19
d3 = q2 % 10; // d3 = 19 % 10 = 9
q3 = q2 / 10; // q3 = 19 / 10 = 1
Dedicated to my Love
SandyPrati C#.NET – Programs Console 39 [email protected]
d4 = q3 % 10; // d4 = 1 % 10 = 1
q4 = q3 / 10; // q4 = 1 / 10 = 0
// Printing Digits
Console.WriteLine(" D1 \t " + d1.ToString()); Console.WriteLine(" D2 \t " + d2.ToString()); Console.WriteLine(" D3 \t " + d3.ToString()); Console.WriteLine(" D4 \t " + d4.ToString());
// Sum of digits
Console.WriteLine(" Sum of digits \t " + (d1 + d2 + d3 + d4).ToString()); } }}
ConApp18: Input 4 Digit Number (1923) through keyboard and calculate the reverse of numberusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp18{ class Program { static void Main(string[] args) { /* Input 4 Digit Number(1923) through keyboard and Calculate the reverse of number. */
Int32 n, d1, d2, d3, d4, q1, q2, q3, q4;
n = 1923;
d1 = n % 10; // d1 = 1923 % 10 = 3
q1 = n / 10; // q1 = 1923 / 10 = 192
d2 = q1 % 10; // d2 = 192 % 10 = 2
q2 = q1 / 10; // q2 = 192 / 10 = 19
Dedicated to my Love
SandyPrati C#.NET – Programs Console 40 [email protected]
d3 = q2 % 10; // d3 = 19 % 10 = 9
q3 = q2 / 10; // q3 = 19 / 10 = 1
d4 = q3 % 10; // d4 = 1 % 10 = 1
q4 = q3 / 10; // q4 = 1 / 10 = 0
// Printing Reverse
Console.Write(d1); Console.Write(d2); Console.Write(d3); Console.Write(d4); } }}
ConApp19: Input 4 Digit Number (1923) through keyboard and Print a new number by adding 1 to each of its digits.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp19{ class Program { static void Main(string[] args) { /* Input 4 Digit Number (1923) through keyboard and Print a new number by adding 1 to each of its digits. If digit is 9 then make new digit as 0. */
Int32 n, d1, d2, d3, d4, q1, q2, q3, q4;
n = 1923;
d1 = n % 10; // d1 = 1923 % 10 = 3
q1 = n / 10; // q1 = 1923 / 10 = 192
d2 = q1 % 10; // d2 = 192 % 10 = 2
Dedicated to my Love
SandyPrati C#.NET – Programs Console 41 [email protected]
q2 = q1 / 10; // q2 = 192 / 10 = 19
d3 = q2 % 10; // d3 = 19 % 10 = 9
q3 = q2 / 10; // q3 = 19 / 10 = 1
d4 = q3 % 10; // d4 = 1 % 10 = 1
q4 = q3 / 10; // q4 = 1 / 10 = 0
// Add 1 to each digit. If digit is 9 make it to 0.
d1 = (d1 != 9) ? ++d1 : 0; d2 = (d2 != 9) ? ++d2 : 0; d3 = (d3 != 9) ? ++d3 : 0; d4 = (d4 != 9) ? ++d4 : 0;
Console.WriteLine(d1); Console.WriteLine(d2); Console.WriteLine(d3); Console.WriteLine(d4); } }}
ConApp20: Program on Single Class Multi Main Formsusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp20{ class Program { /* ConApp20: Program on four Possible forms of MAIN function(Entry Point). Please Ensure that exactly one of the following MAIN functions is active at run time. */
static void Main( ) { Console.WriteLine("Main Function - NR and NA"); }
Dedicated to my Love
SandyPrati C#.NET – Programs Console 42 [email protected]
static void Main(string[] args) { Console.WriteLine("Main Function - NR and A"); }
static int Main() { Console.WriteLine("Main Function - R and NA"); return 0; }
static int Main(string[] args) { Console.WriteLine("Main Function - R and A"); return 0; } }}
ConApp21: Program on Multi Class Multi Main (Needs 2 class files)Program1.cs:using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace ConApp21{ class Program { /* ConApp21: Program on spreading MAIN function in more than one class */
static void Main() { Console.WriteLine("\n Program1 - Main Function - NR and NA"); } }
}programe2.cs:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp21{
Dedicated to my Love
SandyPrati C#.NET – Programs Console 43 [email protected]
class Program2 { static void Main(string[] args) { Console.WriteLine("\n Program2 - Main Function - NR and A"); } }}
ConApp22: Program to Print Command Line Argumentsusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp22{ class Program { /* ConApp22 : Program to display Command Line Arguments */
static void Main(string[] args) { int i = 0;
for (i = 0; i<args.Length; i++) { Console.WriteLine("\n Args[" + i + "] \t" + args[i].ToString()); } } }}
//ConApp23: Program on Parsing Functionsusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp23{ class Program { static void Main(string[] args) { Int32 s, p,t; // Method 1 - Int32.Parse
Dedicated to my Love
SandyPrati C#.NET – Programs Console 44 [email protected]
Console.WriteLine(" Input S Value"); s = Int32.Parse(Console.ReadLine());
// Method 2 - Convert.Int32 Console.WriteLine(" Input P Value"); p = Convert.ToInt32(Console.ReadLine());
// Method 3 - Int32.TryParse bool result; Console.WriteLine(" Input T Value"); result = Int32.TryParse(Console.ReadLine(),out t);
// Print Data Console.WriteLine(" Method 1 Value " + s); Console.WriteLine(" Method 2 Value " + p); Console.WriteLine(" Method 3 Value " + t); } }}
// ConApp24 : Program on Place Holdersusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConApp24{ class Program { static void Main(string[] args) { Int32 s, p; String t;
t = "SandyPrati";
Console.WriteLine(" Input S"); s = Int32.Parse(Console.ReadLine());
Console.WriteLine(" Input P"); p = Int32.Parse(Console.ReadLine());
// Place Holders Console.WriteLine(" {0} : {1} : {2} ", s, p,t); } }}
Dedicated to my Love