c# console programing notes

14

Click here to load reader

Upload: yasir-ahmed-khan

Post on 26-Oct-2014

105 views

Category:

Documents


0 download

DESCRIPTION

C# Console Programing Notes By: Yasir Ahmed Khan www.yasir.ucoz.net03337015014

TRANSCRIPT

Page 1: C# Console Programing Notes

CONSOLE PROGRAMS By: Yasir A k

PROGRAM 01: (Print Name)

static void Main(string[] args) { Console.WriteLine("Welcome");

//Console is a built-in class that contains the methods for displaying messages on the// screen and getting input from the keyboard.

string name; Console.Write("Enter Your Name: "); name = Console.ReadLine(); // Input Console.WriteLine("Your Name Is: " + name); Console.ReadKey(); // to hold output like getch() }

PROGRAM 02: (Print Integer)

static void Main(string[] args){ int myInteger; string myString; myInteger = 17; myString = "\"My Integer\" is"; Console.WriteLine(myString +" "+ myInteger); Console.ReadKey(); }

PROGRAM 03: (Addition, Subtraction, Multiplication and Division of a Number)

static void Main(string[] args) { double firstNumber, secondNumber; string userName; Console.WriteLine("Enter your Name: "); userName = Console.ReadLine(); Console.WriteLine("Welcome "+ userName); Console.WriteLine("Now give me a number:"); firstNumber = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Now give me another number:"); secondNumber = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("The sum of {0} and {1} is {2}.", firstNumber, secondNumber, firstNumber + secondNumber); Console.WriteLine("The result of subtracting {0} from {1} is {2}.", secondNumber, firstNumber, firstNumber - secondNumber); Console.WriteLine("The product of {0} and {1} is {2}.", firstNumber, secondNumber, firstNumber * secondNumber); Console.WriteLine("The result of dividing {0} by {1} is {2}.", firstNumber, secondNumber, firstNumber / secondNumber); Console.WriteLine("The remainder after dividing {0} by {1} is {2}.", firstNumber, secondNumber, firstNumber % secondNumber); Console.ReadKey(); }

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 2: C# Console Programing Notes

PROGRAM 04: (Marks Sheet)

static void Main(string[] args) { int phy, maths,eng,cp,isl; int total; double per; Console.WriteLine("\n\t\t\t MARKS CERTIFICATE "); Console.Write("\nEnter Physics Marks:"); phy = Convert.ToInt32(Console.ReadLine()); Console.Write("\nEnter Mathematics Marks:"); maths = Convert.ToInt32(Console.ReadLine()); Console.Write("\nEnter English Marks: "); eng = Convert.ToInt32(Console.ReadLine()); Console.Write("\nEnter Programming Marks:"); cp = Convert.ToInt32(Console.ReadLine()); Console.Write("\nEnter Islamiat Marks:"); isl = Convert.ToInt32(Console.ReadLine());

total = phy + maths+ isl + cp +eng; per = (total * 100) / 550; Console.WriteLine("Total is: "+Convert.ToInt32(total)); Console.WriteLine("Percentage is: {0} %" , Convert.ToDouble(per));

Console.WriteLine("\n"); if (per > 80.0) Console.WriteLine("You Got Grade: A1 "); else if (per > 70.0) Console.WriteLine("You Got Grade: A"); else if (per > 60.0) Console.WriteLine("You Got Grade: B"); else if (per > 50.0) Console.WriteLine("You Got Grade: C"); else if (per < 49.9) Console.WriteLine("Your Fail"); Console.WriteLine("\n\n\t\tThis Software is Developed By: Yasir Ahmed Khan"); Console.ReadKey(); }

PROGRAM 05: Table using For Loop

static void Main(string[] args) { int tab_no;

Console.WriteLine("\n\t\t\tTable Generator\n"); Console.Write("Enter Table No: ");

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

for(int x=1; x<=10;x++) {

Console.WriteLine("\n{0} * {1} = {2}",tab_no,x,(tab_no*x) );

} Console.ReadKey(); }

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 3: C# Console Programing Notes

PROGRAM 06: Sum of Two Number’s

{ int number1, number2, sum;

Console.Write("Enter First No:"); number1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter Second No:"); number2 = Convert.ToInt32(Console.ReadLine()); sum = number1 + number2;

Console.Write("Sum is " + sum);

Console.WriteLine("\n\n\t\t\t This Program is Written By: Yasir Ahmed Khan"); Console.ReadKey();

}

PROGRAM 07: Cities Temperature

{ double nshah, khi, hyd, multan, isd, lahore, average;

Console.WriteLine("\n\n\t\t\t AVERAGE TEMPERATURE"); Console.Write("\nEnter Temperature of Nawabshah City :"); nshah = Convert.ToDouble(Console.ReadLine()); Console.Write("\nEnter Temperature of Hyderabad City :"); hyd = Convert.ToDouble(Console.ReadLine()); Console.Write("\nEnter Temperature of Multan City :"); multan = Convert.ToDouble(Console.ReadLine()); Console.Write("\nEnter Temperature of Islamabad City :"); isd = Convert.ToDouble(Console.ReadLine()); Console.Write("\nEnter Temperature of Lahore City :"); lahore = Convert.ToDouble(Console.ReadLine()); Console.Write("\nEnter Temperature of Karachi City :"); khi = Convert.ToDouble(Console.ReadLine());

average = (nshah + hyd + multan + isd + lahore) / 6;

Console.WriteLine("\n\nAVERAGE TEMPERATURE IS: " + average);

Console.WriteLine("\n\n\t\t\t This Program is Written By: Yasir Ahmed Khan"); Console.ReadKey();

}

PROGRAM 08: Square & Cube of a Number

{

int num,square,cube,sq_root;

Console.Write("Enter Number: "); num = Convert.ToInt32(Console.ReadLine());

square = num * num; cube = square * num; Console.WriteLine("\n\n Square is " + square); Console.WriteLine("\n\n Cube is " + cube); Console.WriteLine("\n\n\t\t\t This Program is Written By: Yasir Ahmed Khan");

Console.ReadKey(); }

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 4: C# Console Programing Notes

PROGRAM 09: Number Compression

{

int num1, num2;

Console.Write("\n\n\t\t COMPARISSION B/W TWO NUMBER's");

Console.Write("\n\nEnter Ist Number "); num1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter IInd Number "); num2 = Convert.ToInt32(Console.ReadLine());

if (num1 == num2) Console.WriteLine(num1+" is Equals to "+num2); if(num1>num2) Console.WriteLine(num1 + " is Greater Than " + num2); if (num1 < num2) Console.WriteLine(num1 + " is Less Than " + num2); if (num1 != num2) Console.WriteLine(num1 + " is Not Equals to " + num2); if (num1 >= num2) Console.WriteLine(num1 + " is Greater Than or equals to " + num2); if (num1 <= num2) Console.WriteLine(num1 + " is Less Than or equals to " + num2);

Console.WriteLine("\n\n\t\t\t This Program is Written By: Yasir Ahmed Khan"); Console.ReadKey(); }

PROGRAM 10: Kilo Meter to meter

{ int meter, km;

Console.Write("\n\n\t\t Enter Value in Kilo Meter ");

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

meter = km * 1000 ;

Console.Write("\n\n\t\t The Value in Meter Is " + meter);

Console.WriteLine("\n\n\t\t\t This Program is Written By: Yasir Ahmed Khan"); Console.ReadKey(); }

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 5: C# Console Programing Notes

PROGRAM 11: Age Analyzer

{ int year, month,day;

Console.Write("\n\n\t\t Enter The Age in Year ");

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

Console.Write("\n\n\t\t Enter The Age in MOnth ");

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

day = year * 365 + month *30 ;

Console.Write("\n\n\t\t Your " + day + " days old");

Console.WriteLine("\n\n\t\t\t This Program is Written By: Yasir Ahmed Khan"); Console.ReadKey();

}

PROGRAM 12: KB to Byte Conversion

{ int bit,kb;

Console.Write("\n\n\t\t Enter The Data Size in Kilo Bytes ");

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

bit = kb * 1024 ; Console.Write("\n\n\t\t Your Data Size is " + bit + " Byte ");

Console.WriteLine("\n\n\t\t\t This Program is Written By: Yasir Ahmed Khan"); Console.ReadKey();

}PROGRAM 13: Temperature Conversion

static void Main(string[] args) { //Temperature of a city in Fahrenheit degrees is input through the keyboard. //Write a program to convert this temperature into Centigrade degrees.

double temp_f, temp_c; Console.WriteLine("Enter Your City Temperature: "); temp_f = double.Parse(Console.ReadLine()); temp_c = (temp_f - 32) * 5 / 9; Console.WriteLine("Temperature in Celsius is : " + temp_c); Console.WriteLine("\n\n\t\t\t This Program is Written By: Yasir Ahmed Khan");

Console.ReadKey();

}

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 6: C# Console Programing Notes

PROGRAM 14: Conversion of Length

static void Main(string[] args) { //The distance between two cities (in km.) is input through the keyboard. //Write a program to convert and print this distance in meters, feet, inches and centimeters.

double km, m, feet, inches, centimeter;

Console.Write("Enter Distance B/w Two Cities in Kilo Meter: "); km = double.Parse(Console.ReadLine());

m = km * 1000; feet = m * 3.28084; centimeter = m * 100; inches = m * 39.37008;

Console.WriteLine("Distance in Km : " + km); Console.WriteLine("Distance in m : " + m); Console.WriteLine("Distance in feet : " + feet); Console.WriteLine("Distance in centimeter : " + centimeter); Console.WriteLine("Distance in inches : " + inches); Console.WriteLine("\n\n\t\t\t This Program is Written By: Yasir Ahmed Khan");

Console.ReadKey();}

PROGRAM 15: Salary Sheet

static void Main(string[] args) { //Ramesh’s basic salary is input through the keyboard. //His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. //Write a program to calculate his gross salary.

int basic_salary; double dearnesss_allowance, house_rent_allowance, gross_salary;

Console.Write("\n\n \t\tPlease Enter Your Basic Salary: "); basic_salary = int.Parse(Console.ReadLine());

dearnesss_allowance = (basic_salary * 0.4); house_rent_allowance= (basic_salary * 0.2); gross_salary = dearnesss_allowance + house_rent_allowance + basic_salary;

Console.WriteLine("\n \t\tBasic Salary: " + basic_salary); Console.WriteLine("\n \t\tDearness Allowance: " + dearnesss_allowance); Console.WriteLine("\n \t\tHouse Rent Allowance: " + house_rent_allowance); Console.WriteLine("\n \t\tGross Salary: " + gross_salary); Console.WriteLine("\n\n\t\t\t This Program is Written By: Yasir Ahmed Khan");

Console.ReadKey(); }

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 7: C# Console Programing Notes

PROGRAM 16: Age Analyzer

static void Main(string[] args) { int age, days, month; Console.WriteLine("give your age progarme will convert it into days"); Console.WriteLine("give your age in years");

age = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("give your age in months"); month = Convert.ToInt32(Console.ReadLine()); days = age * 365 + month * 30; Console.WriteLine("conversion successful,the days will be " + days + " in " + age + " year " + "and" + month+ "months"); }

PROGRAM 17: Modulus / Absolute

static void Main(string[] args) { // Finding Absoulute Value or Modulus int modulus_no; Console.WriteLine("Enter No Which u want to want to find out modulus"); modulus_no = int.Parse(Console.ReadLine());

Console.WriteLine(Math.Abs(modulus_no)); Console.ReadKey(); }

PROGRAM 18: Largest Number

{ //Finding Greater Number B/w Three Number's int n1, n2, n3;

Console.WriteLine("Enter 1st Number: "); n1 = int.Parse(Console.ReadLine()); Console.WriteLine("Enter 2nd Number: "); n2 = int.Parse(Console.ReadLine()); Console.WriteLine("Enter 3rd Number: "); n3 = int.Parse(Console.ReadLine());

if (n1 > n2 && n1 > n3) Console.WriteLine("\n{0} is greater than {1} & {2}", n1, n2, n3); else if (n2 > n3 && n2 > n1) Console.WriteLine("\n{0} is greater than {1} & {2}", n2, n1, n3); else if (n3 > n1 && n3 > n2) Console.WriteLine("\n{0} is greater than {1} & {2}", n3, n2, n1); else Console.WriteLine("Number's Are Equals"); Console.ReadKey(); }

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 8: C# Console Programing Notes

PROGRAM 18: Book Shop

{ int choice,engchoice,pchoice,dchoice; Console.WriteLine("Welcome In Liberty Books Pvt Ltd. \n\n\n Please Enter Which Subject Book You Want To Buy \n\n\n1. English \n2. Programming \n3. Data Base\n"); Console.WriteLine("Enter Choice B/w 1-3"); choice = Convert.ToInt32(Console.ReadLine());

if (choice == 1) { Console.WriteLine("Now , \n Please Enter Which Book You Want To Buy \n\n\n1. English By Michel John \n2. English By Oxfor Printing Press \n3. English Dictonary h\n"); Console.WriteLine("Enter B/w 1-3"); engchoice = Convert.ToInt32(Console.ReadLine()); if (engchoice == 1) { Console.WriteLine("English By Michel John price is $ 45"); Console.ReadKey(); } if (engchoice == 2) { Console.WriteLine("English By Oxfor Printing Press price is $ 55"): Console.ReadKey(); } if(engchoice==3) { Console.WriteLine("English Dictonary Price is $ 10"); Console.ReadKey(); } else Console.WriteLine("Invalid Range"); Console.ReadKey(); } if(choice==2){Console.WriteLine("Now , \n Please Enter Which Book You Want To Buy \n\n\n1. C Programming \n2. C# Programming \n3. Java \n"); Console.WriteLine("Enter B/w 1-3"); pchoice = Convert.ToInt32(Console.ReadLine()); if (pchoice == 1) { Console.WriteLine("C Programming Book price is $ 45"); Console.ReadKey(); } if (pchoice == 2) { Console.WriteLine("C# Programming Book Press price is $ 55"); Console.ReadKey(); } if(pchoice==3) { Console.WriteLine("Java Programming Book Price is $ 10"); Console.ReadKey(); } else Console.WriteLine("Invalid Range"); } if (choice == 3) { Console.WriteLine("Now , \n Please Enter Which Book You Want To Buy \n\n\n1. Basic Data Base \n2. Oracle 10g \n3. My Bank DataBase \n"); Console.WriteLine("Enter B/w 1-3"); dchoice = Convert.ToInt32(Console.ReadLine()); if (dchoice == 1) { Console.WriteLine("Basic Database Book price is $ 45"); Console.ReadKey(); } if (dchoice == 2) { Console.WriteLine("Oracle 10.G Book Press price is $ 55"); Console.ReadKey(); } if (dchoice == 3) { Console.WriteLine("My Bank Database Book Price is $ 10"); Console.ReadKey(); } else Console.WriteLine("Invalid Range");} else { Console.WriteLine("Invalid Range"); } Console.WriteLine("\n\n This Program is Write By: Yasir Ahmed Khan"); }

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 9: C# Console Programing Notes

PROGRAM 19: Area Of Square

{

int width,height,area;

Console.WriteLine("Enter Width:");width = Int.Parse(Console.ReadLine());

Console.WriteLine("Enter Height:");height = Int.Parse(Console.ReadLine());

area = width * height;

Console.WriteLine("Area of Square is "+area);}

PROGRAM 20: (Area Of Circumference)

{

double radius,area;

Console.WriteLine("Enter Radius:");radius = Double.Parse(Console.ReadLine());

area = radius * 3.14;

Console.WriteLine("Area of circumference is "+area);}

PROGRAM 21: (Alpha , Beta , Theta )

static void Main(string[] args) { Double theta; Console.WriteLine("give any angle to fint its value"); theta=Convert.ToInt32(Console.ReadLine()); { Console.WriteLine("Sine of " + theta + " is " + Math.Sin(theta)); Console.WriteLine("Cosine of " + theta + " is " + Math.Cos(theta)); Console.WriteLine("Tangent of " + theta + " is " + Math.Tan(theta)); Console.WriteLine(); }

PROGRAM 22: (Table With Start and Ending Point )

static void Main(string[] args) { Console.WriteLine("give table no: which you want"); int a = int.Parse(Console.ReadLine()); Console.WriteLine("give starting point of table"); int b = int.Parse(Console.ReadLine()); Console.WriteLine("give ending point of table"); int c = int.Parse(Console.ReadLine()); for (int d = a; d <= c; d++) Console.WriteLine(a + "*" + d + "="+(a * d)) }

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 10: C# Console Programing Notes

PROGRAM 23: (Sun to Earth Distance )

static void Main(string[] args)

{

long inches;

long miles;

miles = 93000000; // 93,000,000 miles to the sun

// 5,280 feet in a mile, 12 inches in a foot.

inches = miles * 5280 * 12;

Console.WriteLine("Distance to the sun: " +

inches + " inches.");

}

PROGRAM 24: (Even or Odd Locator )

static void Main(string[] args)

{

int a;

Console.WriteLine("Give a no: to find that either it is even or odd");

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

if (a % 2 == 0)

Console.WriteLine(a + "no is even");

else

Console.WriteLine(a + "no:is odd");

}

PROGRAM 25: (Prime Number )

static void Main(string[] args)

{

int a;

Console.WriteLine("Give a no: to find either it is prime or not");

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

if (a % 2 == 1)

Console.WriteLine("no: is prime");

else

Console.WriteLine("no: is not prime");

}

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 11: C# Console Programing Notes

PROGRAM 26: (Discount Program)

static void Main(string[] args)

{

int price,quantity;

double discount;

double amounttobepaid;

Console.WriteLine("enter the price of the shoes");

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

Console.WriteLine("enter the quantity of pair of the shoes");

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

Console.WriteLine("enter the discount percentage");

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

amounttobepaid = price - ((discount/100*price))*quantity;

Console.WriteLine("the price of shoes after deduction of discount is" + amounttobepaid);

}

PROGRAM 27: (square of a number using method)

static void Main(string[] args) { square(5); } public static void square(int a) { Console.WriteLine( a*a); }

PROGRAM 28: (Even Number Series using method)

static void Main(string[] args) { evennumber(2, 20); // Starts From 2 , Ends at 20 } public static void evennumber(int a,int b) { for (int x = a; x <= b; x= x+ 2) Console.Write(x + ","); }

PROGRAM 29: (QUEST NAWABSHAH)

static void Main(string[] args) { char [] quest = {'Q','U','E','S','T','N','A','W','A','B','S','H','A','H'};

for (int a = 0; a < 13; a++) Console.Write(quest[a]); }

PROGRAM 30: (Pakistan)

static void Main(string[] args) {

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014

Page 12: C# Console Programing Notes

char[] quest = { 'P', 'a', 'k', 'i', 's', 't', 'a', 'n'};

for (int a = 0; a < 7; a++) Console.Write(quest[a]); }

PROGRAM 31: (1/1, 2/3, 3/5. 4/7. 5/9, 6/11, 7/13, 8/15, 9/17) static void Main(string[] args) { // 1/1, 2/3, 3/5. 4/7. 5/9, 6/11, 7/13, 8/15, 9/17

int a = 1, b = 1; do { Console.Write(" "+a + "/" + b); a++; b= b+ 2; }

while (a < 10 && b < 18); }

PROGRAM 32: (Swapping of Number)

int a, b;

Console.Write("Enter Number For a"); a = Int32.Parse(Console.ReadLine()); Console.Write("Enter Number For b"); b = Int32.Parse(Console.ReadLine());

Console.Write("Value of A before Swaping" + a); Console.Write("\nValue of B before Swaping" + b);

b = (a + b) - (a = b);

Console.Write("\n\nValue of A After Swaping" + a); Console.Write("\nValue of B After Swaping" + b);

PROGRAM 33: (Leap year)

static void Main(string[] args) { int year; Console.Write("Enter a year to check if it is a leap year"); year = Int32.Parse(Console.ReadLine());

if (year % 400 == 0) Console.Write(year + "is a leap Year"); else if (year % 100 == 0) Console.Write(year + "is not a leap Year"); else if (year % 4 == 0) Console.Write(year + "is a leap Year"); else Console.Write(year + "is not a leap Year"); }

PROGRAM 34: (Factorial)

int a,n,fact=1; Console.Write("Enter A Number to Calculate its Factorial "); n=Int32.Parse(Console.ReadLine()); for (a = 1; a <= n; a++) fact = fact * a;

Console.WriteLine("Factorial of " + n + " is " + fact);

Programs Are Compiled By: Yasir Ahmed Khan | [email protected] | 03337015014