Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

C# Simple calculator on Windows application

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MY_first_windows_form_application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
       

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int num1, num2, res;
            num1 = Convert.ToInt32(textBox1.Text);
            num2 = Convert.ToInt32(textBox2.Text);
            res = num1 + num2;
            label3.Text = res.ToString();
         
        }

        private void button2_Click(object sender, EventArgs e)
        {

            int num1, num2, res;
            num1 = Convert.ToInt32(textBox1.Text);
            num2 = Convert.ToInt32(textBox2.Text);
            res = num1 - num2;
            label3.Text = res.ToString();


        }

        private void button3_Click(object sender, EventArgs e)
        {
            int num1, num2, res;
            num1 = Convert.ToInt32(textBox1.Text);
            num2 = Convert.ToInt32(textBox2.Text);
            res = num1 * num2;
            label3.Text = res.ToString();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            int num1, num2, res;
            num1 = Convert.ToInt32(textBox1.Text);
            num2 = Convert.ToInt32(textBox2.Text);
            res = num1 / num2;
            label3.Text = res.ToString();

        }

        private void button5_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
    }
}

OUTPUT




Continue Reading

c# how to create exception by itself

using System;
using System
.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exceptionhandling_02{

    public class book:ApplicationException{
        public book(string issued)
            : base(issued)
        {
       
        }
       
    }
    public class gain_info {
   
        public string  iss;
        public void get_info()
        {


            Console.WriteLine("WELCOME TOO OXFORD LIBRARY");
            Console.WriteLine("enter your book name");
            iss = Console.ReadLine();
            if (iss == "C#" || iss == "c#")
            {
                throw (new book("OOP BOOK CANT BE ISSUED"));
               
            }
            else
            {

                Console.WriteLine("you have issued this book :{0}", iss);

            }



        }
 
    }
    class program
    {

       
        static void Main(string[] args)
        {
            shuru:
            gain_info obj = new gain_info();

            try {

                obj.get_info();
           
            }
            catch(book e)
            {
                Console.WriteLine("exception caught ",e.Message);
            }
           
            goto shuru;
         

        }
    }
}

OUTPUT



Continue Reading

C# how to use delegetion in your code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


delegate void player();

namespace delegationkeyword
{

    class players
    {
       public  static int choice;
       public  static void list() {
            Console.WriteLine("HERE IS THE LIST OF FIVE PLAYERS");
       Console.WriteLine("1- VIRAT KOHLI   \n 2- STEVEN SMITH \n 3- JOE ROOT \n 4- YOUNUS KHAN  \n 5- kANE WILLIAMSOM");
       Console.WriteLine("now enter favourite player input ");
       choice = Convert.ToInt32(Console.ReadLine());
       if (choice == 1)
       {
       Console.WriteLine("MATCHES= 12  RUNS=1215       AVERAGE= 75.93             S-R=60.41");

      
       }
            if(choice==2){
           
              Console.WriteLine("MATCHES=11   RUNS=1079       AVERAGE=71.93              S-R=58.73");


           
            }
             if(choice==3){

                 Console.WriteLine("MATCHES=17   RUNS=1477          AVERAGE=49.23              S-R=60.70");


           
            }
             if (choice == 4)
             {
                 Console.WriteLine("MATCHES=10   RUNS=673           AVERAGE=37.38              S-R=50.48");


            
            
             }
             if (choice == ){
                 Console.WriteLine("MATCHES=10   RUNS=753           AVERAGE=47.06              S-R=53.4 }

        }  


        static void Main(string[] args)
        {
            player abc = new player(list);
            list();
            Console.ReadKey();
        }
    }

}
OUTPUT



Continue Reading

C# how to use enum keyword

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace enumkeyword
{
    class Program
    {
        enum traffic { red, yellow, green }
        static void Main(string[] args)
        {

            int trafstart = (int)traffic.red;
            int trafmiddle = (int)traffic.yellow;
            int trafend = (int)traffic.green;
            Console.WriteLine("Red {0}",trafstart);
            Console.WriteLine("Yellow {0}", trafmiddle);
            Console.WriteLine("Green {0}",trafend);
          
            Console.ReadKey();

        }
    }

}
Output


Continue Reading

C# how TOO USE exception handling divided by zero expression


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

           start :

            Console.WriteLine("WELCOME TOO EXCEPTION HANDLING");
            try
            {

                float a, b,result;
                Console.WriteLine("enter your first number");
                a = float.Parse(Console.ReadLine());
                Console.WriteLine("enter second number");
                b = float.Parse(Console.ReadLine());
                if (b == 0)
                {

                    throw new DivideByZeroException();

                }
                else
                    result = a / b;
                Console.WriteLine("your ans is after division : {0}", result);
            }
           
            catch
            {

                Console.WriteLine("SORRY  Number can't divide by zero ");

            }
            finally {

                Console.WriteLine("thankx for using our calculator");
           
            }
            goto start;

        

        }
    }

}

OUTPUT

Continue Reading

C# Create an interface Itransaction contain void showTransaction(), double getAmount() ,Create class Transaction that inherit Itransaction and contains private string tCode; private string date; private double amount; add default constructor and parametric constructor to initialize values and implement void showTransaction(), double getAmount()in class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLOGGER
{
    interface Itransaction
    {
        void showTransaction();
        double getAmount();
    }
    class Transaction : Itransaction
    {
        string tCode;
        string date;
        double amount;
        public Transaction()
        {

        }
        public Transaction(string a, string b, double c)
        {
            this.tCode = tCode;
            this.date = date;
            this.amount = amount;
        }
        public double getAmount()
        {
            Console.WriteLine("Enter amount");
            amount = Convert.ToDouble(Console.ReadLine());
            return amount;
        }
        public void showTransaction()
        {
            Console.WriteLine("Enter transaction code");
            tCode = Console.ReadLine();
            Console.WriteLine("Enter date of transaction");
            date = Console.ReadLine();
        }
        public void show()
        {
            Console.WriteLine("The tcode is " + tCode);
            Console.WriteLine("The date is " + date);
            Console.WriteLine("The amount is " + amount);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Transaction obj = new Transaction();
            obj = new Transaction("0", "0", 0);
            obj.getAmount();
            obj.showTransaction();
            obj.show();
            Console.ReadKey();


        }
    }
}
OUTPUT



Continue Reading

C# how to make abstract class and how to use abstract keyword..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace abstract_keyword
{
  abstract class JHON
    {
     public  abstract void showdata();
  }
    class b:JHON{

        public override void showdata() {

            Console.WriteLine("This is test of abstract modifier");
        }
   
    }
    class c : JHON {

        public override void showdata()
        {
            Console.WriteLine("AFTER INHERITE and ovveride the result is this");
        }
   
    }

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

            anas obj1 = new b();
            obj1.showdata();
            anas obj2 = new c();
            obj2.showdata();
            Console.ReadKey();

        }
    }
}

OUTPUT



Continue Reading

C# How to make interface using multilevel inheritance

using System;

namespace @interface
{
    interface ifirst
    {
         void name();
    }
    interface isecond
    {
      void sec();
   
    }
    class show:ifirst,isecond  {
       void ifirst.name()
        {
        Console.WriteLine("STUDENT NAME IS Jhon");
       
        }
    void isecond.sec(){
   
    Console.WriteLine("SEC - C 2017");
   
    }
 
    }

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

            show obj1 = new show();
            ((ifirst)obj1).name();
            ((isecond)obj1).sec();
            Console.ReadKey();
        }
    }

}

OUTPUT




Continue Reading

Reverse a number


Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace reverse_number
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, t, r, rev = 0;
            Console.WriteLine("Enter any number");
            n = Int32.Parse(Console.ReadLine());
            t = n;
            while (t > 0)
            {
                r = t % 10;
                t = t / 10;
                rev = rev * 10 + r;
            }
            Console.WriteLine("Reverse of number::{0}", rev);
           
            Console.ReadLine();
        }
    }
}


Output:





Continue Reading

C# Program Working of Constructor in Inheritance

using system;

public class BaseClass  // This is the main class
{
    public BaseClass()
    {
        // statement of 1st Base class constructor
    }

    public BaseClass(int Age)
    {
        // statement of 2nd Base class constructor
    }

    // Other classes comes here
}

public class DerivedClass : BaseClass  //  inheriting the class here.

{
    public DerivedClass()
    {
        // statement of 1st DerivedClass constructor
    }

    public DerivedClass(int Age):base(Age)
    {
        // statement of 2nd DerivedClass constructor
    }

    // Other classes comes here
}
Continue Reading

C# Program of Polymorphism Using Virtual and Override keyword

using System;

namespace ConsoleApplication43
{
    class vehicle
    {
        public int doors = 6;
        public string color = "BLACK", model = "2017", engine = "D15 B";

        public vehicle()
        {

        }
        public vehicle(string color, string model, string engine, int door)
        {

            Console.WriteLine("Colour "+color);
            Console.WriteLine("Model is "+ model);
            Console.WriteLine("Engine is "+ engine);
            Console.WriteLine("There are " + doors + " doors\n");
        }

     
        public virtual int calculatespeed()
        {
            return 0;
        }

        public class car : vehicle
        {
            public override int calculatespeed()
            {
                return (123+123)/10;
            }
        }

        public class bike : vehicle
        {
            public override int calculatespeed()
            {
                return (3 * 60) / 10;
            }
        }
     
     
    class Program
        {
            static void Main(string[] args)
            {
                vehicle Obj = new vehicle("Black","2017","D15B",4);
                car Obj1 = new car();
                bike Obj2 = new bike();
                Console.WriteLine("the speed of car is {0}",Obj1.calculatespeed());
                Console.WriteLine("the speed of bike is {0}",Obj2.calculatespeed());
                Console.ReadLine();
               
            }
        }
    }
}
Continue Reading