You are on page 1of 29

int x; // declaration

x = 123; // initialization

int y = 321; // declaration + initialization

int z = x + y;

int age = 21; // whole integer


double height = 300.5; // decimal number
bool alive = false; //true or false
char symbol = '@'; // single character
String name = "Bro"; // a series of characters

Console.WriteLine("Hello " + name);


Console.WriteLine("Your age is " + age);
Console.WriteLine("Your height is " + height + "cm");
Console.WriteLine("Are you alive? " + alive);
Console.WriteLine("Your symbol is: " + symbol);

String userName = symbol + name;

Console.WriteLine("Your username is: " + userName);

Console.ReadKey();

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// constants = immutable values which are known at compile time
// and do not change for the life of the program

const double pi = 3.14;

//pi = 420; //can't change this constant

Console.WriteLine(pi);

Console.ReadKey();
}
}
}
static void Main(string[] args)
{
// type casting = Converting a value to a different data type
// Useful when we accept user input (string)
// Different data types can do different things

double a = 3.14;
int b = Convert.ToInt32(a);

int c = 123;
double d = Convert.ToDouble(c);

int e = 321;
String f = Convert.ToString(e);

String g = "$";
char h = Convert.ToChar(g);

String i = "true";
bool j = Convert.ToBoolean(i);

Console.WriteLine(b.GetType());
Console.WriteLine(d.GetType());
Console.WriteLine(f.GetType());
Console.WriteLine(h.GetType());
Console.WriteLine(j.GetType());

Console.ReadKey();
}
}
}

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What's your age?");
String name = Console.ReadLine();
Console.WriteLine("What's your age?");
int age = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Hello " + name);


Console.WriteLine("You are " + age + " years old");

Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{
int friends = 5;

friends = friends + 1;
//friends += 1;
//friends++;

//friends = friends - 1;
//friends -= 1;
//friends--;

//friends = friends * 2;
//friends *= 2;

//friends = friends / 2;
//friends /= 2;

//int remainder = friends % 2;


//Console.WriteLine(remainder);

Console.WriteLine(friends);

Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{
//if statement = a basic form of decision making

Console.WriteLine("Please enter your name: ");


String name = Console.ReadLine();

if (name == "")
{
Console.WriteLine("You did not enter your name!");
}
else
{
Console.WriteLine("Hello " + name);
}

Console.ReadKey();
}
}
}

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// switch = an efficient alternative to many else if statements

Console.WriteLine("What day is it today?");


String day = Console.ReadLine();

switch (day)
{
case "Monday":
Console.WriteLine("It's Monday!");
break;
case "Tuesday":
Console.WriteLine("It's Tuesday!");
break;
case "Wednesday":
Console.WriteLine("It's Wednesday!");
break;
case "Thursday":
Console.WriteLine("It's Thursday!");
break;
case "Friday":
Console.WriteLine("It's Friday!");
break;
case "Saturday":
Console.WriteLine("It's Saturday!");
break;
case "Sunday":
Console.WriteLine("It's Sunday!");
break;
default:
Console.WriteLine(day + " is not a day!");
break;
}

Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{

// logical operators = Can be used to check if more than 1 condition is true/false

// && (AND)
// || (OR)

Console.WriteLine("What's the temperature outside: (C)");


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

if (temp >= 10 && temp <= 25)


{
Console.WriteLine("It's warm outside!");
}
else if (temp <= -50 || temp >= 50)
{
Console.WriteLine("DO NOT GO OUTSIDE!");
}

Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{

// while loop = repeats some code while some condition remains true

String name = "";

while (name == "")


{
Console.Write("Enter your name: ");
name = Console.ReadLine();
}

Console.WriteLine("Hello " + name);

Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{

// for loop = repeats some code a FINITE amount of times

// Count up to 10
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}

// Count down from 10


for (int i = 10; i > 0; i--)
{
Console.WriteLine(i);
}
Console.WriteLine("HAPPY NEW YEAR!");

Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{
// nested loops = loops inside of other loops
// Uses vary. Used a lot in sorting algorithms

Console.Write("How many rows?: ");


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

Console.Write("How many columns?: ");


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

Console.Write("What symbol: ");


String symbol = Console.ReadLine();

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < columns; j++)
{
Console.Write(symbol);
}
Console.WriteLine();
}

Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
do
{
double num1 = 0;
double num2 = 0;
double result = 0;

WriteLine("------------------");
WriteLine("Calculator Program");
WriteLine("------------------");

Write("Enter number 1: ");


num1 = Convert.ToDouble(Console.ReadLine());

Write("Enter number 2: ");


num2 = Convert.ToDouble(Console.ReadLine());

WriteLine("Enter an option: ");


WriteLine("\t+ : Add");
WriteLine("\t- : Subtract");
WriteLine("\t* : Multiply");
WriteLine("\t/ : Divide");
Write("Enter an option: ");

switch (Console.ReadLine())
{
case "+":
result = num1 + num2;
Console.WriteLine($"Your result: {num1} + {num2} = " + result);
break;
case "-":
result = num1 - num2;
Console.WriteLine($"Your result: {num1} - {num2} = " + result);
break;
case "*":
result = num1 * num2;
Console.WriteLine($"Your result: {num1} * {num2} = " + result);
break;
case "/":
result = num1 / num2;
Console.WriteLine($"Your result: {num1} / {num2} = " + result);
break;
default:
Console.WriteLine("That was not a valid option");
break;
}
Console.Write("Would you like to continue? (Y = yes, N = No): ");
} while (Console.ReadLine().ToUpper() == "Y");

Console.WriteLine("Bye!");
Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{
// array = a variable that can store multiple values. fixed size

//String[] cars = {"BMW", "Mustang", "Corvette"};

String[] cars = new string[3];

cars[0] = "Tesla";
cars[1] = "Mustang";
cars[2] = "Corvette";

for (int i = 0; i < cars.Length; i++)


{
Console.WriteLine(cars[i]);
}

Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{
// method = performs a section of code, whenever it's called "invoked".
// benefit = Let's us reuse code w/o writing it multiple times
// Good practice is to capitalize method names (I forgot in this video)

String name = "Bro";


int age = 21;

SingHappyBirthday(name, age);

Console.ReadKey();
}
static void SingHappyBirthday(String birthdayBoy, int yearsOld)
{
Console.WriteLine("Happy birthday to you!");
Console.WriteLine("Happy birthday to you!");
Console.WriteLine("Happy birthday dear " + birthdayBoy);
Console.WriteLine("You are " + yearsOld + " years old!");
Console.WriteLine("Happy birthday to you!");
Console.WriteLine();
}
}
}

{
class Program
{
static void Main(string[] args)
{
// return = returns data back to the place where a method is invoked

double x;
double y;
double result;

Console.WriteLine("Enter in number 1: ");


x = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter in number 2: ");


y = Convert.ToDouble(Console.ReadLine());

result = Multiply(x, y);

Console.WriteLine(result);

Console.ReadKey();
}
static double Multiply(double x, double y)
{
return x * y;
}
}
}

{
class Program
{
static void Main(string[] args)
{
// exception = errors that occur during execution

// try = try some code that is considered "dangerous"


// catch = catches and handles exceptions when they occur
// finally = always executes regardless if exception is caught or not
int x;
int y;
double result;

try
{
Console.Write("Enter number 1: ");
x = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter number 2: ");


y = Convert.ToInt32(Console.ReadLine());

result = x / y;

Console.WriteLine("result: " + result);


}
catch (FormatException e)
{
Console.WriteLine("Enter ONLY numbers PLEASE!");
}
catch (DivideByZeroException e)
{
Console.WriteLine("You can't divide by zero! IDIOT!");
}
catch (Exception e)
{
Console.WriteLine("Something went wrong!");
}
finally
{
Console.WriteLine("Thanks for visiting!");
}

Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{
// conditional operator = used in conditional assignment if a condition is true/false

//(condition) ? x : y
double temperature = 20;
String message;

message = (temperature >= 15) ? "It's warm outside!" : "It's cold outside!";

Console.WriteLine(message);

Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{
// string interpolation = allows us to insert variables into a string literal
// precede a string literal with $
// {} are placeholders

String firstName = "Bro";


String lastName = "Code";
int age = 21;

Console.WriteLine($"Hello {firstName} {lastName}.");


Console.WriteLine($"You are {age,10} old.");

Console.ReadKey();
}
}
}

{
class Program
{
static void Main(string[] args)
{
// object = An instance of a class
// A class can be used as a blueprint to create objects (OOP)
// objects can have fields & methods (characteristics & actions)

Human human1 = new Human();


Human human2 = new Human();

human1.name = "Rick";
human1.age = 65;

human2.name = "Morty";
human2.age = 16;

human1.Eat();
human1.Sleep();

human2.Eat();
human2.Sleep();

Console.ReadKey();
}
}
class Human
{
public String name;
public int age;

public void Eat()


{
Console.WriteLine(name + " is eating");
}
public void Sleep()
{
Console.WriteLine(name + " is sleeping");
}
}
}

////////////////////////////////////////////////////

int num = int.Parse("25");


Console.WriteLine(num + num + "");
Console.WriteLine("{0}{0}", num);

///////////////////////////////////////////////

using static System.Console;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// int num = int.Parse("25");
//Console.WriteLine(num + num + "");
//Console.WriteLine("{0}{0}", num);

Write("Enter grade 1: ");


int grade1 = int.Parse(ReadLine());

}
}
}
///////////////////////////////////////////////

using static System.Console;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{

Write("Please enter your user name: ");


string userName = ReadLine();

Write("Please enter your department: ");


string dept = ReadLine();

Write("Please enter your faculty: ");


string faculty = ReadLine();

string asterikLine = new string('*', 64);


WriteLine(asterikLine);
WriteLine("*{0, 20}: {1, -40}*", "Name", userName);
WriteLine("*{0, 20}: {1, -40}*", "Department", dept);
WriteLine("*{0, 20}: {1, -40}*", "faculty", faculty);
WriteLine(asterikLine);

Write("Enter grade 1: ");


int grade1 = int.Parse(ReadLine());

Write("Enter grade 2: ");


int grade2 = int.Parse(ReadLine());

Write("Enter grade 3: ");


int grade3 = int.Parse(ReadLine());

double avg = (grade1 + grade2 + grade3) / 3;

WriteLine(asterikLine);
WriteLine("*{0, 20}: {1, -40}*", "Name", userName);
WriteLine("*{0, 20}: {1, -40}*", "Department", dept);
WriteLine("*{0, 20}: {1, -40}*", "faculty", faculty);
WriteLine("*{0, 20}: {1, -40:F2}*", "AvgGrade", avg);
WriteLine(asterikLine);

///////////////////////////////////////////////

using System;
using static System.Console;

namespace _1175Lab1
{
class Program
{
static void Main(string[] args)
{

int grade1 = 23;


int grade2 = 21;
WriteLine ("Max Grade: " +Math.Max(grade1, grade2)); //Math.Max is a pre-
defined math method that gets the maximum of two numbers

SayHello(); //using other method below, and when it is completed, it will


come back to here

//arguments send values into Parameters, Parameters is always variable

//Age Decremnt operation prefix


Write("Please enter your age as a whole nunmber: ");
int age = int.Parse(ReadLine());
WriteLine("Lets try prefix decrement operator...Age after fix decrement is
{0}", --age);
WriteLine("Age after the previous decrment operator is " + age);

WriteLine();
//Age Decrement operation postfix
Write("Please enter your age as a whole nunmber: ");
int age2 = int.Parse(ReadLine());
WriteLine("Lets try postfix decrement operator...Age after fix decrement
is {0}", age2--);
WriteLine("Age after the previous decrment operator is " + age2);

//conjunctive concatenation
WriteLine();
string msg = "Hello, ";
msg += "How are you";
WriteLine("New Message is...{0}", msg);

}
// all user-defined class methods are defined outside the Main method
//but must be inside the Program class
//all class methods must be defined as static and must be defined here
//class methods are defined here, and may be called by other methods including
the Main method

//Method definition consists of method header and method body


static void MyMethod1()
{
//this is a dummy method inside the Program class with no input parameters and
void return type (no value return)

static void MyMethod2(int param1)


{
//this method has 1 input parameter and void return type

static int MyMethod3(double param1, double param2)


{
// must have a return statement inside the method defintion that
//returns a value that matches the data type of the return data type
defined in the header
return -5; //this sample return int statement matches the header defintion

}
static void SayHello()
{
string helloMsg = "Hi, Priya"; //variable is local to this method
WriteLine(helloMsg);

}
}

///////////////////////////////////////////////

using System;
using static System.Console;

namespace ChiHangY_Assign1
{
class DogFood
{
public static void Main(string[] args)

{
WriteLine("***************************************************************************
");
WriteLine("* WoofYum Online Dog Food Ordering System
*");
WriteLine("* Place your order now!
*");

WriteLine("***************************************************************************
");

WriteLine("***************************************************************************
");
WriteLine("* Dog Food Brands in stock given below
*");
WriteLine("* Can Unit Price for Zignature: $5.25
*");
WriteLine("* Can Unit Price for Royal Canin: $4.99
*");
WriteLine("* Can Unit Price for Purina: $3.99
*");

WriteLine("***************************************************************************
");
WriteLine();
WriteLine();
WriteLine("Let us begin by entering the number of cans you need need for
each of these dog food brands...");
WriteLine();
WriteLine("Enter the number of cans for Zignature: ");
int can1 = int.Parse(Console.ReadLine()); // ReadLine to get user input

WriteLine("Enter the number of cans for Royal Canin: ");


int can2 = int.Parse(Console.ReadLine());

WriteLine("Enter the number of cans for Purina: ");


int can3 = int.Parse(Console.ReadLine());

WriteLine("The number of cans for each brand name has been entered");
WriteLine();
WriteLine();

Start:
WriteLine();
WriteLine();
WriteLine("What would you like to do?");
WriteLine("Press 1 for View Order, Press 2 for Update Order, Press 3 for
quitting the application:");
int num = int.Parse(ReadLine()); //ReadLine getting user input

if (num == 1)
{
double result; //declare variables
double a = can1 * 5.25;
WriteLine("**********************************************************");
WriteLine(" BrandName: Zignature ");
WriteLine(" UnitPrice: $5.25 ");
WriteLine(" NumCans: " + can1, "" );
WriteLine(" BrandTotal: ${0:F2}", + a ); //F2 is
2 decimal points, and $ is dollar sign
WriteLine();
double b = can2 * 4.99;
WriteLine(" BrandName: Royal Canin ");
WriteLine(" UnitPrice: $4.99 " );
WriteLine(" NumCans: " + can2, "" );
WriteLine(" BrandTotal: ${0:F2}", +b );
WriteLine();
double c = can3 * 3.99;
WriteLine(" BrandName: Royal Canin ");
WriteLine(" UnitPrice: $3.99 ");
WriteLine(" NumCans: " + can3, "");
WriteLine(" BrandTotal: ${0:F2}", +c );
WriteLine();

WriteLine();
result = a + b + c;
WriteLine(" Total before discount: ${0:F2}", result);

if (result > 75)


{
WriteLine(" Discount: ${0:F2}", +result * 0.15);

WriteLine(" Total after discount: ${0:F2}", +result * 0.85);

WriteLine("**********************************************************");
}

if (result <= 75)


{
WriteLine(" Discount: ${0:F2}", +result * 0);

WriteLine(" Total after discount: ${0:F2}", +result);


WriteLine("***********************************************");

}
goto Start; //Jumps to "Start:"
}
else if (num == 2)
{
WriteLine("Updating your dog food order");
WriteLine();
WriteLine();
WriteLine("Press 1 to update number of cans for Zignature");
WriteLine("Press 2 to update number of cans for Royal Canin");
WriteLine("Press 3 to update number of cans for P" +
"urina");
WriteLine("Enter the number (1, 2 or 3): ");
int update = int.Parse(ReadLine());
if (update == 1)
{

WriteLine("Enter the new quantity for Zignature");


can1 = int.Parse(Console.ReadLine());
WriteLine("Great! Quantity for Zignature has been updated to " +
can1);

goto Start; //Jumps to "Start:"


}
if (update == 2)
{
WriteLine("Enter the new quantity for Royal Canin");
can2 = int.Parse(Console.ReadLine());
WriteLine("Great! Quantity for Royal Canin has been updated to " +
can2);

goto Start; //Jumps to "Start:"


}
if (update == 3)
{
WriteLine("Enter the new quantity for Purina");
can3 = int.Parse(Console.ReadLine());
WriteLine("Great! Quantity for Purina has been updated to " +
can3);

goto Start; //Jumps to "Start:"

}
else if (num == 3) // if user press "3" , it will go to the end right
away.
{
WriteLine("Thank you for placing the dog food order. Good Bye!");

}
}
}
}

///////////////////////////////////////////////
static void Main(string[] args)
{
int x; // declaration
x = 123; // initialization

int y = 321; // declaration + initialization

int z = x + y;

int age = 21; // whole integer


double height = 300.5; // decimal number
bool alive = false; //true or false
char symbol = '@'; // single character
String name = "Bro"; // a series of characters

Console.WriteLine("Hello " + name);


Console.WriteLine("Your age is " + age);
Console.WriteLine("Your height is " + height + "cm");
Console.WriteLine("Are you alive? " + alive);
Console.WriteLine("Your symbol is: " + symbol);

String userName = symbol + name;

Console.WriteLine("Your username is: " + userName);

Console.ReadKey();
}
}
}

///////////////////////////////////////////////
static void Main(string[] args)
{
// type casting = Converting a value to a different data type
// Useful when we accept user input (string)
// Different data types can do different things

double a = 3.14;
int b = Convert.ToInt32(a);

int c = 123;
double d = Convert.ToDouble(c);

int e = 321;
String f = Convert.ToString(e);

String g = "$";
char h = Convert.ToChar(g);

String i = "true";
bool j = Convert.ToBoolean(i);

Console.WriteLine(b.GetType());
Console.WriteLine(d.GetType());
Console.WriteLine(f.GetType());
Console.WriteLine(h.GetType());
Console.WriteLine(j.GetType());

Console.ReadKey();
}
}
}

///////////////////////////////////////////////

static void Main(string[] args)


{
Console.WriteLine("What's your name?");
String name = Console.ReadLine();

Console.WriteLine("What's your age?");


int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Hello " + name);
Console.WriteLine("You are " + age + " years old");

Console.ReadKey();
}
}
}
///////////////////////////////////////////////

static void Main(string[] args)


{
int friends = 5;

friends = friends + 1;
//friends += 1;
//friends++;

//friends = friends - 1;
//friends -= 1;
//friends--;

//friends = friends * 2;
//friends *= 2;

//friends = friends / 2;
//friends /= 2;

//int remainder = friends % 2;


//Console.WriteLine(remainder);

Console.WriteLine(friends);

Console.ReadKey();
}
}
}

///////////////////////////////////////////////
static void Main(string[] args)
{

double x = 3;
double y = 5;

double a = Math.Pow(x, 2);


double b = Math.Sqrt(x);
double c = Math.Abs(x);
double d = Math.Round(x);
double e = Math.Ceiling(x);
double f = Math.Floor(x);
double g = Math.Max(x, y);
double h = Math.Min(x, y);

Console.WriteLine(a);

Console.ReadKey();
}
}
}

///////////////////////////////////////////////
using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
//if statement = a basic form of decision making
// -------------------- Example 1 --------------------

Console.WriteLine("Please enter your age: ");


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

if (age >= 18)


{
Console.WriteLine("You are now signed up!");
}
else if (age < 0)
{
Console.WriteLine("You haven't been born yet!");
}
else
{
Console.WriteLine("You must be 18+ to sign up!");
}
// -------------------- Example 2 --------------------

Console.WriteLine("Please enter your name: ");


String name = Console.ReadLine();

if (name == "")
{
Console.WriteLine("You did not enter your name!");
}
else
{
Console.WriteLine("Hello " + name);
}

Console.ReadKey();
}
}
}
///////////////////////////////////////////////

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// switch = an efficient alternative to many else if statements

Console.WriteLine("What day is it today?");


String day = Console.ReadLine();

switch (day)
{
case "Monday":
Console.WriteLine("It's Monday!");
break;
case "Tuesday":
Console.WriteLine("It's Tuesday!");
break;
case "Wednesday":
Console.WriteLine("It's Wednesday!");
break;
case "Thursday":
Console.WriteLine("It's Thursday!");
break;
case "Friday":
Console.WriteLine("It's Friday!");
break;
case "Saturday":
Console.WriteLine("It's Saturday!");
break;
case "Sunday":
Console.WriteLine("It's Sunday!");
break;
default:
Console.WriteLine(day + " is not a day!");
break;
}

Console.ReadKey();
}
}
}
/////////////////////////////////////////////////

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{

// logical operators = Can be used to check if more than 1 condition is


true/false

// && (AND)
// || (OR)

Console.WriteLine("What's the temperature outside: (C)");


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

if (temp >= 10 && temp <= 25)


{
Console.WriteLine("It's warm outside!");
}
else if (temp <= -50 || temp >= 50)
{
Console.WriteLine("DO NOT GO OUTSIDE!");
}
Console.ReadKey();
}
}
}

///////////////////////////////////////////////

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{

// while loop = repeats some code while some condition remains true

String name = "";

while (name == "")


{
Console.Write("Enter your name: ");
name = Console.ReadLine();
}

Console.WriteLine("Hello " + name);

Console.ReadKey();
}
}
}

///////////////////////////////////////////////

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{

// for loop = repeats some code a FINITE amount of times

// Count up to 10
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}

// Count down from 10


for (int i = 10; i > 0; i--)
{
Console.WriteLine(i);
}
Console.WriteLine("HAPPY NEW YEAR!");
Console.ReadKey();
}
}
}

///////////////////////////////////////////////

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// nested loops = loops inside of other loops
// Uses vary. Used a lot in sorting algorithms

Console.Write("How many rows?: ");


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

Console.Write("How many columns?: ");


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

Console.Write("What symbol: ");


String symbol = Console.ReadLine();

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < columns; j++)
{
Console.Write(symbol);
}
Console.WriteLine();
}

Console.ReadKey();
}
}
}

///////////////////////////////////////////////

using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
bool playAgain = true;
int min = 1;
int max = 100;
int guess;
int number;
int guesses;
String response;
while (playAgain)
{
guess = 0;
guesses = 0;
response = "";
number = random.Next(min, max + 1);

while (guess != number)


{
Console.WriteLine("Guess a number between " + min + " - " + max +
" : ");
guess = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Guess: " + guess);

if (guess > number)


{
Console.WriteLine(guess + " is to high!");
}
else if (guess < number)
{
Console.WriteLine(guess + " is to low!");
}
guesses++;
}
Console.WriteLine("Number: " + number);
Console.WriteLine("YOU WIN!");
Console.WriteLine("Guesses: " + guesses);

Console.WriteLine("Would you like to play again (Y/N): ");


response = Console.ReadLine();
response = response.ToUpper();

if (response == "Y")
{
playAgain = true;
}
else
{
playAgain = false;
}
}

Console.WriteLine("Thanks for playing! ... I guess");

Console.ReadKey();
}
}
}
///////////////////////////////////////////////
using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
do
{
double num1 = 0;
double num2 = 0;
double result = 0;
Console.WriteLine("------------------");
Console.WriteLine("Calculator Program");
Console.WriteLine("------------------");

Console.Write("Enter number 1: ");


num1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter number 2: ");


num2 = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter an option: ");


Console.WriteLine("\t+ : Add");
Console.WriteLine("\t- : Subtract");
Console.WriteLine("\t* : Multiply");
Console.WriteLine("\t/ : Divide");
Console.Write("Enter an option: ");

switch (Console.ReadLine())
{
case "+":
result = num1 + num2;
Console.WriteLine($"Your result: {num1} + {num2} = " +
result);
break;
case "-":
result = num1 - num2;
Console.WriteLine($"Your result: {num1} - {num2} = " +
result);
break;
case "*":
result = num1 * num2;
Console.WriteLine($"Your result: {num1} * {num2} = " +
result);
break;
case "/":
result = num1 / num2;
Console.WriteLine($"Your result: {num1} / {num2} = " +
result);
break;
default:
Console.WriteLine("That was not a valid option");
break;
}
Console.Write("Would you like to continue? (Y = yes, N = No): ");
} while (Console.ReadLine().ToUpper() == "Y");

Console.WriteLine("Bye!");
Console.ReadKey();
}
}
}

///////////////////////////////////////////////

static void Main(string[] args)


{
// return = returns data back to the place where a method is invoked
double x;
double y;
double result;

Console.WriteLine("Enter in number 1: ");


x = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter in number 2: ");


y = Convert.ToDouble(Console.ReadLine());

result = Multiply(x, y);

Console.WriteLine(result);

Console.ReadKey();
}
static double Multiply(double x, double y)
{
return x * y;
}
}
}

///////////////////////////////////////////////
using System;

namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
//params keyword = a method parameter that takes a variable number of
arguments.
// The parameter type must be a single - dimensional array

double total = CheckOut(3.99, 5.75, 15, 1.00, 10.25);

Console.WriteLine(total);
Console.ReadKey();
}

static double CheckOut(params double[] prices)


{
double total = 0;

foreach (double price in prices)


{
total += price;
}
return total;
}

}
}

///////////////////////////////////////////////

You might also like