C# PROGRAMMING       ByProf. A. Syed Mustafa
How to execute a C# program?Install setup package named dotNetFx40_Full_setup.exe can bedownloaded from Microsoft’s genera...
Program to add 2 numbers// type the program in notepad & save the program as  “add.cs”using System;class add{    public st...
Program to display Command linearguments// type the program in notepad & save the program as   “cmd.cs”using System;class ...
Program to display Command linearguments// type the program in notepad & save the program as   “cmd.cs”using System;class ...
Program to display Command linearguments// type the program in notepad & save the program as   “cmd.cs”using System;class ...
Program to use Math classusing System;class sqt{public static void Main(){     Console.WriteLine("Enter number");     int ...
Program to use Exceptionusing System;class Except1{public static void Main(){try{     int a=5,b=0;     Console.Write(“Mult...
Program to use Exceptionusing System;class Except1{public static void Main(){try{     int a=5,b=0;     Console.Write(“Mult...
Program to use Exceptionusing System;class Except3{public static void Main(){try{       int age=14;       if (age<15) thro...
Program to use Exceptionusing System;class Except4{public static void Main(){ int a=893434,b=78888;try{     int c=checked(...
Program to use Exceptionusing System;class Except{public static void Main(){try{     int a=5,b=0;     Console.WriteLine(“E...
Program to use customized Exceptionusing System;class AgeException:Exception{public AgeException(String msg):base(msg){ }}...
Program to use customized Exceptionusing System;class AgeException:Exception{String m;public AgeException(String msg){m=ms...
Program to use customized Exceptionusing System;class AgeException:Exception{ String m;public AgeException(String msg){m=m...
Upcoming SlideShare
Loading in...5
×

C# programs

2,345

Published on

C# programs

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,345
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
163
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

C# programs

  1. 1. C# PROGRAMMING ByProf. A. Syed Mustafa
  2. 2. How to execute a C# program?Install setup package named dotNetFx40_Full_setup.exe can bedownloaded from Microsoft’s general download area http://www.microsoft.com/en-us/download/details.aspx?id=17851Set Path varable through Mycomputer’s property –Environment VariablePATH: C:WINDOWSMicrosoft.NETFrameworkv4.0.30319Use Command prompt and then type the following command for compilation :c:mydircsc programname.csTo run the C# programc:mydirprogramname Prof. Syed Mustafa(Ph.D) 2
  3. 3. Program to add 2 numbers// type the program in notepad & save the program as “add.cs”using System;class add{ public static void Main() { Console.WriteLine("Enter 2 numbers"); int a=int.Parse(Console.ReadLine()); int b=int.Parse(Console.ReadLine()); int c=a+b; Console.WriteLine("sum of "+a+" and "+b+" is "+c); }} Prof. Syed Mustafa(Ph.D) 3
  4. 4. Program to display Command linearguments// type the program in notepad & save the program as “cmd.cs”using System;class cmd{ public static void Main(string[] s ) { Console.WriteLine("command line arguments"); for(int i=0;i<s.Length;i++) Console.WriteLine(s[i]); }} Prof. Syed Mustafa(Ph.D) 4
  5. 5. Program to display Command linearguments// type the program in notepad & save the program as “cmd.cs”using System;class cmd{ public static void Main(string[] s ) { Console.WriteLine("command line arguments"); foreach(string i in s) Console.WriteLine(i); }} Prof. Syed Mustafa(Ph.D) 5
  6. 6. Program to display Command linearguments// type the program in notepad & save the program as “cmd.cs”using System;class cmd{ public static void Main(string[] s ) { string[] a=Environment.GetCommandLineArgs(); Console.WriteLine("command line arguments"); for(int i=1;i<a.Length;i++) //s[0] is cmd.cs, here s[1] is first argument Console.WriteLine(a[i]); }} Prof. Syed Mustafa(Ph.D) 6
  7. 7. Program to use Math classusing System;class sqt{public static void Main(){ Console.WriteLine("Enter number"); int n=int.Parse(Console.ReadLine()); double r=Math.Sqrt(n); Console.WriteLine("square root of "+n+" is "+r);}} Prof. Syed Mustafa(Ph.D) 7
  8. 8. Program to use Exceptionusing System;class Except1{public static void Main(){try{ int a=5,b=0; Console.Write(“Multiple Exception"); int c=a/b; Console.WriteLine("C="+c); }catch(DivideByZeroException e) { Console.WriteLine("Divide by 0 Error"); }catch(ArithmeticException ae) { Console.WriteLine("Arithmetic Error"); }}} Prof. Syed Mustafa(Ph.D) 8
  9. 9. Program to use Exceptionusing System;class Except1{public static void Main(){try{ int a=5,b=0; Console.Write(“Multiple Exception"); int c=a/b; Console.WriteLine("C="+c); }catch(DivideByZeroException e) { Console.WriteLine("Divide by 0 Error"); }catch(ArithmeticException ae) { Console.WriteLine("Arithmetic Error"); } finally { Console.WriteLine("From finally..."); }}} Prof. Syed Mustafa(Ph.D) 9
  10. 10. Program to use Exceptionusing System;class Except3{public static void Main(){try{ int age=14; if (age<15) throw new ArithmeticException(); Console.WriteLine("Age=" + age); }catch(ArithmeticException ae) { Console.WriteLine("Age Error..."); }finally { Console.WriteLine("From finally..."); }}} Prof. Syed Mustafa(Ph.D) 10
  11. 11. Program to use Exceptionusing System;class Except4{public static void Main(){ int a=893434,b=78888;try{ int c=checked(a*b); Console.WriteLine("a=" + a); }catch(OverflowException ae) { Console.WriteLine("Overflow Error...“+ae.Message); Console.WriteLine(“Source Error...“+ae.Source); }finally { Console.WriteLine("From finally..."); }}} Prof. Syed Mustafa(Ph.D) 11
  12. 12. Program to use Exceptionusing System;class Except{public static void Main(){try{ int a=5,b=0; Console.WriteLine(“Exception"); int c=a/b; Console.WriteLine("C="+c); }catch(DivideByZeroException e) { Console.WriteLine("Divide by 0 Error: "+e); Console.WriteLine("Error Message is: "+e.Message); Console.WriteLine("Stack trace is: "+e.StackTrace); Console.WriteLine("Error Source is: "+e.Source); Console.WriteLine("Target Site is: "+e.TargetSite); Console.WriteLine("HelpLink is: "+e.HelpLink); } }} Prof. Syed Mustafa(Ph.D) 12
  13. 13. Program to use customized Exceptionusing System;class AgeException:Exception{public AgeException(String msg):base(msg){ }}class Except4custom{public static void Main(){ int age=4; try{ if(age<15) throw new AgeException("Under Age"); Console.WriteLine("age=" + age); }catch(AgeException ae) { Console.WriteLine("Error is...:"+ae); }finally { Console.WriteLine("From finally..."); }}} Prof. Syed Mustafa(Ph.D) 13
  14. 14. Program to use customized Exceptionusing System;class AgeException:Exception{String m;public AgeException(String msg){m=msg;}public override string Message{ get { return m; }}}class Except4custom2{public static void Main(){ int age=4; try{ if(age<15) throw new AgeException("Under Age"); Console.WriteLine("age=" + age); }catch(AgeException ae) { Console.WriteLine("Error is...:"+ae.Message); }} Prof. Syed Mustafa(Ph.D) 14}
  15. 15. Program to use customized Exceptionusing System;class AgeException:Exception{ String m;public AgeException(String msg){m=msg;}public override string Message{ get { m+="Age is not valid"; return m; }}}class Except4custom1{ public static void Main(){ int age=4; try{ if(age<15) throw new AgeException("Under Age"); Console.WriteLine("age=" + age); }catch(AgeException ae) { Console.WriteLine("Error is...:"+ae.Message); } } Prof. Syed Mustafa(Ph.D) 15}
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×