Sunday, August 11, 2013

Simple Console applications/Logics

Simple Console applications
Even Number

using System;
class First
{
                static void Main()
                {
          Console.WriteLine("Even no's between 1 to 100");            for(byte Number=1;Number<=100;Number++)
                                {              
                                  if(Number % 2==0)
                                   {
                                               
                                                Console.WriteLine("{0}",Number);                                                                                                                                                                      }                                         }   

                }
}

Area of a Circle

using System;
public class Second
{
     public static void Main()
                {
          ulong Radius;
          Second ob=new Second();
          Console.WriteLine("enter the Radius of circle");
          Radius=ulong.Parse(Console.ReadLine());
          ob.Areac(Radius);
          ulong Basex;
          ulong Height;                    
          Console.WriteLine("enter the Base of triangle");
          Basex =ulong.Parse(Console.ReadLine());
          Console.WriteLine("enter the Radius of circle");
          Height=ulong.Parse(Console.ReadLine()); 
          ob.Areat(Basex,Height); 
          }
     void Areac(ulong x)
         {
               ulong Area= (ulong)(3.14 * (x)*x);
               Console.WriteLine("Area of circle is {0}",Area);                                                                                              }

         void Areat(ulong x,ulong y)
         {      ulong AreaofT=(ulong)((0.5)*x*y) ;
               Console.Write("the area of triangle is");
               Console.WriteLine ("{0}",AreaofT);
                 }

}             


Exponential

using System;
class Exponential
    {
        static void Main(string[] args)
        {
            Exponential ob=new Exponential();
            Decimal  x,eX=0,term;
            Console.WriteLine("enter the value of x");
            x = Decimal.Parse(Console.ReadLine());
            for(int i=0;i<=31;i++)
            {
               term= (ob.power (i,x)/ob.factorial (i));
               eX = eX + term;
            }
            Console .WriteLine ("The value of eX is {0}",eX);
           
        }
        public int factorial(int n)
        {
            int Fact = 1;
            for (int i = 1; i <= n; i++)
            {
                Fact = Fact * i;
            }
            return Fact;
        }
         public decimal power(int n,decimal x)
         {
             decimal val=1;
             if (n==0)
             {    val=1;
             }
             for(int i=1;i<=n;i++)
             {
                 val=val*x;
             }
             return val ;
         }
    }
Sum Average Percentage of 5 No

using System;
class Fivesub
    {
        static void Main(string[] args)
        {
            int Sub1, Sub2, Sub3, Sub4, Sub5;
            Console.WriteLine ("enter the 1st subject  marks");
            Sub1=int.Parse (Console .ReadLine ());
            Console.WriteLine ("enter the 2nd subject  marks");
            Sub2=int.Parse (Console .ReadLine ());
            Console.WriteLine ("enter the 3rd subject  marks");
            Sub3=int.Parse (Console .ReadLine ());
            Console.WriteLine ("enter the 4rth subject  marks");
            Sub4=int.Parse (Console .ReadLine ());
            Console.WriteLine ("enter the 5th subject  marks");
            Sub5=int.Parse (Console .ReadLine ());
            int Sum = Sub1 + Sub2 + Sub3 + Sub4 + Sub5;
            float  Avg;
            Avg = (float)(Sum / 5.0);
            float Percentage =(float) ((Sum / 5.0));
            Console.WriteLine("Sum of marks is {0}", Sum);
            Console.WriteLine("Average marks is {0}", Avg );
            Console.WriteLine("Total Percentage is {0}%",Percentage );

        }
    }

Leap year or Not

using System;
class Second
{
     public static void Main()
                {short year;
                Console.WriteLine("enter the year");
                year=short.Parse(Console.ReadLine());
                if(year % 4==0)
                {
                Console.WriteLine("year is leap year");
                }
                else{
                Console.WriteLine("year is not a leap year");
                }
        
   
         }                                                        
}             


Maximum of three

using System;
class Maxofthree
    {
        static void Main(string[] args)
        {
            int n1, n2, n3;
            Console .WriteLine ("enter the n1");
            n1 = int.Parse(Console.ReadLine());
            Console .WriteLine ("enter the n2");
            n2 = int.Parse(Console .ReadLine ());
            Console .WriteLine ("enter the n3");
            n3=int.Parse (Console .ReadLine ());
            int max=(n1>n2 &&n1>n3)?n1:((n2>n1 && n2>n3)?n2:((n3>n1 && n3>n1)?n3:0 ));
            if (max != 0)
            {
                Console.WriteLine("The max of three is {0}", max);
            }
            else
            {
                Console.WriteLine("all are equal");
            }
        }
    }


Compute nCr

using System;
class NCR
    {
        static void Main(string[] args)
        {
            int n,r,ncr;
            Console.WriteLine("enter n");
            n = int.Parse(Console.ReadLine());
            Console.WriteLine("enter r");
            r = int.Parse(Console.ReadLine());
            NCR ob=new NCR();
            ncr = ((ob.factorial(n)) / (ob.factorial(r) * ob.factorial(n - r)));
            Console.WriteLine("The value of Ncr is {0}", ncr);
        }
        public int factorial(int n)
        {
            int Fact = 1;
            for (int i = 1; i <= n; i++)
            {
                Fact = Fact * i;
            }
            return Fact;
        }


   }

Print Table of a Number

using System;
class Printtable
    {
        static void Main(string[] args)
        {
            int number;
            Console.WriteLine("enter the number");
            number = int.Parse(Console.ReadLine());
            for (int i = 1; i <= 10; i++)
            {
                int val = number * i;
                Console.WriteLine("{0} x {1}={2}", number, i, val);
            }
        }
    }

Convert Fahrenheit to Celsius and Celsius to Fahrenheit

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           
            float Fht;
            float Cel;
             Console.WriteLine("Enter your choice");
            A : Console.WriteLine ("Enter 1-for Fehrenhiet to celsius and 2-for Celsius to fehrenhiet");
            int ch = int.Parse(Console.ReadLine());
            switch (ch)
            {
                case 1:
                    Console.WriteLine ("enter the temperature in fehrehiet");
                    Fht =int.Parse((Console.ReadLine()));
                    Cel=((5/9)*(Fht-32)) ;
                    Console.WriteLine ("The temperature in celsius is {0}",Cel);
                    break;
                case 2:
                    Console.WriteLine ("enter the temperature in celsius");
                    Cel =int.Parse (Console.ReadLine());
                    Fht=(((9/5)*Cel) + 32) ;
                    Console.WriteLine ("The temperature in Fehrenhiet is {0}",Fht);
                    break;
                default  :
                    Console.WriteLine ("Wrong choice selection re-enter the choice");
                    goto A;
            }

        }
    }
}

Reverse the Order of a number

using System;
class Second
{
     public static void Main()
                {
        int No;
                int sum=0;
        Console.WriteLine("enter the number");
                No=(int.Parse)(Console.ReadLine());
        Console.WriteLine("Number in reverse order");  
                while(No>0)
           {
             int x=No%10;
                                 No=No/10 ;
                                sum=sum+No;
             Console.Write ("{0}",x);
    
           }
   
         }                                                        
}             


Calculate Sum of digits of a number

using System;
class Second
{
     public static void Main()
                {
        int No;
                int sum=0;
        Console.WriteLine("enter the number");
                No=(int.Parse)(Console.ReadLine());
           while(No>0)
           {
             int x=No%10;
                                 No=No/10 ;
                                sum=sum+x;
            
    
           }
    Console.Write ("{0}",sum);
         }                                                        
}             


Swap Numbers


using System;
class Second
{
     public static void Main()
                {int no1;
                 int no2;
                Console.WriteLine("enter the ist no");
                no1=int.Parse(Console.ReadLine());
        Console.WriteLine("enter the 2nd no");
                no2=int.Parse(Console.ReadLine());
                no1=no1-no2;
                no2=no1+no2;
                no1=no2-no1;
                Console.WriteLine("The nos are:{0}and{1}",no1,no2);
                               
}
        
                                                            
}             



Convert String to Upper without Using Direct Function

using System;
class Upperstring
    {
        static void Main(string[] args)
        {
           
                Console.WriteLine("Enter the lowercase character");
                char chr=char.Parse (Console.ReadLine());
                int val=(int)(chr);
                val=val-32;
                chr=(char)(val);
                Console.WriteLine("uppercase character is {0}",chr);
        }
    }





Simple 3 tier Asp.net Application

Application requires mainly three things
1.Business Logic Layer-(BLL)
2.Data Access Layer(DAL)
3.Presentation Layer.

Step-1  : Create a simple table Dept

Step2:-Add 3 files in website businessobjects.cs ,  DataAccess.cs , Helper.cs
Step4:-Add following code into businessObjects.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SimpleNtier
{
    public class Dept
    {
        public Dept(int Deptno, string Dname, string Loc)
        {
            this.Deptno = Deptno;
            this.Dname = Dname;
            this.Loc = Loc;
        }
        private int _Deptno;
        private string _Dname;
        private string _Loc;
        public int Deptno
        {
            get
            {
                return _Deptno;
            }
            set
            {
                _Deptno = value;
            }

        }
        public string Dname
        {

            get
            {
                return _Dname;
            }
            set
            {
                _Dname = value;
            }

        }
        public string Loc
        {
            get
            {
                return _Loc;
            }
            set
            {
                _Loc = value;
            }
        }
    }
}
This contains data objects for all the fields of the table.
Step4:- Add following code to data access.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace SimpleNtier
{
    public class DataAccess
    {

        public static void Add_dept(Dept b, ActionType action)
        {
            string sp_add = "spManageDept";
            SqlParameter pr_Action = new SqlParameter("@ActionType", SqlDbType.SmallInt);
            SqlParameter pr_Deptno = new SqlParameter("@Deptno", SqlDbType.Int);
            SqlParameter pr_Dname = new SqlParameter("@Dname", SqlDbType.VarChar);
            SqlParameter pr_Loc = new SqlParameter("Loc", SqlDbType.NVarChar);
            pr_Action.Value = action;
            if (action == ActionType.Add)
                pr_Deptno.Direction = ParameterDirection.Input;
            else
                pr_Deptno.Direction = ParameterDirection.Output;

            pr_Deptno.Value = b.Deptno;

            if ((action == ActionType.Add) || (action == ActionType.Modify))
            {
                pr_Dname.Value = b.Dname;
                pr_Loc.Value = b.Loc;
            }

            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.StoredProcedure, sp_add, pr_Action, pr_Deptno, pr_Dname, pr_Loc);

            if (action == ActionType.Add)
                pr_Deptno.Value = b.Deptno;

        }

        public static DataSet getDept()
        {
            string s_dt = "sp_GetDept";
            SqlParameter pr_Deptnp = new SqlParameter("@Deptno", SqlDbType.Int);
            pr_Deptnp.Value = -1;
            DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.ConnectionString, CommandType.StoredProcedure, s_dt, pr_Deptnp);
            ds.Tables[0].TableName = "Dept";
            return ds;
        }
        public static Dept getDeptById(int Deptno)
        {
            string s_dt = "sp_GetDept";
            SqlParameter pr_Deptnp = new SqlParameter("@Deptno", SqlDbType.Int);
            pr_Deptnp.Value = Deptno;
            SqlDataReader dr=SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.StoredProcedure, s_dt, pr_Deptnp);
            Dept objDept=null;
            if(dr.HasRows)
            {
                dr.Read();
                objDept=new Dept((int) dr["Deptno"],(string) dr["Dname"],(string) dr["Loc"]);              
            }
            dr.Close();
            return objDept;
        }
    }
}

step 5:- Add following code to Item.aspx and Item.aspx.cs pages

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace SimpleNtier
{
    public partial class Item1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Loadgrid();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (txtDeptno.Text != null)
            {
                Dept b = new Dept(Convert.ToInt32(txtDeptno.Text), txtDname.Text, txtLoc.Text);
                if (DataAccess.getDeptById(Convert.ToInt32(txtDeptno.Text)) != null)
                {
                    DataAccess.Add_dept(b, ActionType.Modify);
                    Response.Write("<script>alert('Successfully Updated')</script>");
                    Loadgrid();
                }
                else if (DataAccess.getDeptById(Convert.ToInt32(txtDeptno.Text)) == null)
                {
                    DataAccess.Add_dept(b, ActionType.Add);
                    Response.Write("<script>alert('Successfully Added')</script>");
                    Loadgrid();
                }
            }
           
        }


        private void Loadgrid()
        {
            DataSet ds = DataAccess.getDept();
            grdDept.DataSource = ds;
            grdDept.DataBind();
        }
    }
}


Item.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Item1.aspx.cs" Inherits="SimpleNtier.Item1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="pnlDept" runat="server" Width="100%">
    <table>
    <tr> 
        <td></td>
        <td><asp:Label ID="lblDeptno" runat="server"></asp:Label></td>     
        <td><asp:TextBox ID="txtDeptno" runat="server"></asp:TextBox></td>     
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                ControlToValidate="txtDeptno" ErrorMessage="*" SetFocusOnError="True"
                ValidationGroup="Item"></asp:RequiredFieldValidator>
        </td>
    </tr>
        <tr>       
        <td></td>
        <td><asp:Label ID="lblDname" runat="server"></asp:Label></td>
        <td><asp:TextBox ID="txtDname" runat="server"></asp:TextBox></td>
        <td></td>
    </tr>
       
    <tr>
        <td></td>       
        <td><asp:Label ID="lblLoc" runat="server"></asp:Label></td>
        <td><asp:TextBox ID="txtLoc" runat="server"></asp:TextBox></td>
        <td></td>
    </tr>
    <tr>
    <td></td>
    <td><asp:button ID="btnSave" runat="server" onclick="btnSave_Click"
            ValidationGroup="Item"></asp:button></td>
   
    </tr>

    </table>
    </asp:Panel>
    <asp:Panel ID="pnlGrid" runat="server">
   
        <asp:GridView ID="grdDept" runat="server">
        </asp:GridView>
   
    </asp:Panel>
   
    </div>
    </form>
</body>
</html>