Search This Blog

Tuesday, 9 June 2015

A program to find whether a given number is prime or not using c#

using System;
class Program
{
static void Main(string[]args)
{
int i;
Console.WriteLine("enter any number");
int a = int.Parse(Console.ReadLine());
for(i=2;i<a;i++)
{
  if(a%i==0)
  break;
}
if(a==1||i==a)
Console.WriteLine("the number is prime");
else
Console.WriteLine("the number is not pirme");
Console.Read(); 
}
}


OUTPUT



CLICK TO DOWNLOAD




A program to find factorial of a number using functions in c#

class Program
        {
            static int Factorial(int n)
            {
                if (n <= 1)
                    return 1;
                int fact = 1;
                for (int i = 2; i <= n; i++)
                {
                    fact = fact * i;
                }
                return fact;
            }
            static void Main(string[] args)
            {
                Console.Write("Enter a Number to find factorial: ");
                int n = Convert.ToInt32(Console.ReadLine());
                int r = Factorial(n);
                Console.WriteLine(n.ToString() + "! = " + r.ToString());
                Console.ReadLine();
            }
        }
    }

OUTPUT


CLICK TO DOWNLOAD

A program to display the astrick pattern as given below

using System;
    class Class2
    {
        static void Main(string[] args)
        {
            int i, j;
            for (i = 0; i <6; i++)
            {
                for (j = 5; j > i; j--)
                {
                    Console.Write("");
                }
                for (int k = 0; k < i; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }


OUTPUT

CLICK TO DOWNLOAD

 

A program to check whether a given number is Armstrong or not

using System;
    class Class1
    {
        static void Main(string[] args)
        {
            int a, n, i, m = 0;
            Console.WriteLine("Enter any number");
            a = int.Parse(Console.ReadLine());
            n = a;
            for (i = 0; n > 0; i++)
            {
                int x = n % 10;
                m = x * x * x;
                n = n / 10;
            }
            if (a == m)
                Console.WriteLine("the given number is an armstrong number");
            else
                Console.WriteLine("the given number is not armstrong number");
            Console.ReadLine();
        }
    }




OUTPUT

CLICK TO DOWNLOAD
 

A program to calculate GCD for two given numbers

class Class3
{
static void Main(string[]args)
{
int a,b,c,i,j;
Console.WriteLine("Enter the first number");
a=int.Parse(Console.ReadLine());
Console.WriteLine("Enter the second number");
b=int.Parse(Console.ReadLine());
if (a>b)
i=b;
else
i=a;
for(j=i;j>1;j--)
{
if (a%j==0&&b%j==0)
break;
}
Console.WriteLine("The greatest common divisor of {0} and {1} is {2}",a,b,j);
Console.ReadLine();
}
}

OUTPUT

CLICK TO DOWNLOAD


A windows application to authenticate a user and password

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Data source= HP-PC\\SQLEXPRESS;Initial Catalog=Login_data;Integrated Security=True;");
        public Form1() {
                    InitializeComponent(); }
                private void button1_Click(object sender, EventArgs e)  {
            try {
                            String s;
                con.Open();
                SqlCommand com = new SqlCommand("select *from Login_info where U_name=@User_name AND U_password = @Password", con);
                com.Parameters.AddWithValue("@User_name", textBox1.Text);
                com.Parameters.AddWithValue("@Password", textBox2.Text);
                SqlDataReader rd = com.ExecuteReader();
                if (rd.Read())
                {
                    s = rd.GetString(0);
                    MessageBox.Show("Welcome" + textBox1.Text, "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }
                                else {
  MessageBox.Show("incorrect Username or password \n please enter correct username and password.", "Login failed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                }
                con.Close();
            }
            catch {
                MessageBox.Show("Database connection failure", "error",
                    MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }}}}
       


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    namespace WindowsFormsApplication3
    {
        static class Program
        {
            static void main()
        {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }}}
namespace WindowsFormsApplication3

    partial class Form1
    {
        private System.ComponentModel.IContainer components =null;
        protected override void Dispose(bool disposing)
        {
        if(disposing && (components !=null))
        {
        components.Dispose();
        }
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.textBox1=new System.Windows.Forms.TextBox();
            this.textBox2=new System.Windows.Forms.TextBox();
            this.button1=new System.Windows.Forms.Button();
            this.label1=new System.Windows.Forms.Label();
            this.label2=new System.Windows.Forms.Label();
            this.SuspendLayout();
            this.textBox1.Location=new System.Drawing.Point(95,23);
            this.textBox1.Name="textBox1";
            this.textBox1.Size=new System.Drawing.Size(311,20);
            this.textBox1.TabIndex=0;
            this.textBox2.Location=new System.Drawing.Point(95,64);
            this.textBox2.Name="textBox2";
            this.textBox2.Size=new System.Drawing.Size(311,20);
            this.textBox2.TabIndex=1;
            this.button1.Location=new System.Drawing.Point(313,90);
            this.button1.Name="button1";
            this.button1.Size=new System.Drawing.Size(81,23);
            this.button1.TabIndex=2;
            this.button1.Text="Login>>";
            this.button1.UseVisualStyleBackColor=true;
            this.button1.Click +=new System.EventHandler(button1_Click);
            this.label1.AutoSize=true;
            this.label1.Location=new System.Drawing.Point(23,26);
            this.label1.Name="label1";
            this.label1.Size=new System.Drawing.Size(56,13);
            this.label1.TabIndex=4;
            this.label1.Text="Password";
            this.AutoScaleDimensions= new System.Drawing.SizeF(6F,13F);
            this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize=new System.Drawing.Size(428,121);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.button1);
            this.Name="Form1";
            this.Text="Form1";
            this.ResumeLayout(false);
            this.PerformLayout();
    }
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
      }
}

OUTPUT


CLICK TO DOWNLOAD






A window application in which user enter 5 subject marks & print the grade of each student in a message box

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
   static void main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}}}
partial class Form1{
    private System.ComponentModel.IContainer components=null;
    protected override void Dispose(bool disposing){
        if(disposing &&(components!=null){
            components.Dispose();}
        base.Dispose(disposing);
    }
    private void InitializeComponent(){
        this.label1=new System.Windows.Forms.Label();
        this.button1=new System.Windows.Forms.Button();
         this.textbox2=new System.Windows.Forms.TextBox();
        this.textbox5=new System.Windows.Forms.TextBox();
        this.textbox4=new System.Windows.Forms.TextBox();
        this.textbox3=new System.Windows.Forms.TextBox();
        this.textbox1=new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        this.label1.Font=new System.Drawing.Font("Arial Narrow",24F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((byte)(0)));
        this.label1.Location=new System.Drawing.Point(29,23);
        this.label1.Name="label1";
        this.label1.Size=new System.Drawing.Size(614,45);
        this.label1.TabIndex=14;
        this.label1.Text="Enter the marks for various subjects out of 100";
        this.label1.TextAlign=System.Drawing.ContentAlignment,MiddleCenter;
        this.button1.Location=new System.Drawing.Point(252,269);
        this.button1.Name="button1";
         this.button1.Size=new Systemm.Drawing.Size(148,58);
        this.button1.TabIndex="13";
         this.button1.Text="Show Grade";
        this.button1.UseVisualStyleBackColor=true;
        this.button1.Click+=new System.EventHandler(this.button1_Click);
        this.textBox2.Location=new System.Drawing.Point(252,109);
        this.textBox2.Multiline=true;
        this.textBox2.Name="textBox2";
        this.textBox2.RightToLeft=new System.Windows.Forms.RightToLeft.Yes;
        this.button2.Size=new System.Drawing.Size(148,31);
        this.textBox2.TabIndex=12;
        this.textBox5.Location=new System.Drawing.Point(348,31);
        this.textBox5.Multiline=true;
        this.textBox5.Name="textBox5";
        this.textBox5.RightToLeft=new System.Windows.Forms.RightToLeft.Yes;
        this.textBox5.Size=new System.Drawing.Size(148,31);
        this.textBox5.TabIndex=11;
         this.textBox4.Location=new System.Drawing.Point(151,180);
        this.textBox4.Multiline=true;
        this.textBox4.Name="textBox4";
        this.textBox4.RightToLeft=new System.Windows.Forms.RightToLeft.Yes;
           this.textBox4.Size=new System.Drawing.Size(148,31);
          this.textBox4.TabIndex=10;
         this.textBox3.Location=new System.Drawing.Point(438,109);
this.textBox3.Multiline=true;
        this.textBox3.Name="textBox3";
        this.textBox3.RightToLeft=new System.Windows.Forms.RightToLeft.Yes;
        this.textBox3.Size=new System.Drawing.Size(148,31);
         this.textBox3.TabIndex=9;
        this.textBox1.Location=new System.Drawing.Point(65,109);
        this.textBox1.Multiline=true;
        this.textBox1.Name="textBox1";
        this.textBox1.RightToLeft=new System.Windows.Forms.RightToLeft.Yes;
         this.button1.Size=new System.Drawing.Size(148,31);
         this.textBox1.TabIndex=8;
        this.AutoScaleDimensions=new System.Drawing.SizeF(6F,13F);
        this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize=new System.Drawing.Size(676,389);
        this.Controls.Add(this.label1);
         this.Controls.Add(this.button1);
         this.Controls.Add(this.textbox2);
        this.Controls.Add(this.textbox5);
        this.Controls.Add(this.textbox4);
        this.Controls.Add(this.textbox3);
        this.Controls.Add(this.textbox1);
        this.Name="Form1";
        this.Text="Form1";
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.Label label1;
     private System.Windows.Forms.Button button1;
     private System.Windows.Forms.TextBox textBox2;
     private System.Windows.Forms.TextBox textBox5;
     private System.Windows.Forms.TextBox textBox4;
     private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.TextBox textBox1;
}}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
public partial class Form1:Form {
    public Form1();
    InitializeComponent();}
private void button1_Click(object sender,EventArgs e) {
int a,b,c,d,m,f;
try{
a=int.parse(textBox1.Text);
b=int.parse(textBox2.Text);
c=int.parse(textBox3.Text);
d=int.parse(textBox4.Text);
f=int.parse(textBox5.Text);
if(a>100||b>100|c>100||d>100||m>100)
throw new Exception();
for=(a+b+c+d+m)/5;
if(f>80){
MessageBox.Show("the grade of the student is A");}
else{
if(f>60)
MessageBox.Show("the grade of the student is B");}
else{
if(f>45){
MessageBox.Show("the grade of the student is C");}
else{
MessageBox.Show("the grade of the student is D");
}}}
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
textBox4.Text="";
textBox5.Text="";
}
}  }  }





OUTPUT


CLICK TO DOWNLOAD

A web application to implement the various validation controls



<% Page Language="c#" AutoEventWireup="true"
 CodeBehind="WEbForm1.aspx.cs"Inherits="WebApplication5.WebForm1"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:/www.w3.org/1999/xhtml">
<head runat="server">
<title>Window Form </title>
<head/>
<body>
<form id="form1">runat="server">
<asp:Label ID="Label1"runat="server"Text="ENTER YOUR NAME"style="z-index:1;text-align:center;left:231px;top:87px;position:absolute;height:27px;width:115x"BackColor="white"></asp:Label>
<asp:TextBox ID="TextBox1"runat="server"BackColor="#0066FF"style="z-index:1;left:429px;top:81px;position:absolute;height:36px;width:165px"ToolTip="name"></asp:TextBox>
<asp:Label ID="Label2"runat="server"Text="ENTER YOUR BRANCH"style=z-index:2;left:231px;top:137px;position:absolute;height:36px;width>115px;"></asp:label>
<asp:TextBox ID="TextBOX2"runat="server" Backcolor="#0066FF"style="z-index:2;left:429px;top:131px;position:absolute;height:36px;width:165px"ToolTip="branch"></asp:TextBox>
<asp:Label ID="Label3" runat="server"Text="ENTER YOUR ID"style="z-index:3;left231px;top:188px;position:absolute;height:27px;width:15px;"></asp:label>
<asp:TextBox ID="TextBox3"runat="server"Backcolor="#0066FF"
style="z-index: 3;left:429px;top:181px;position:absolute;height:36px; width:165px" CausesValidation="true"Tooltip="id"></asp:TextBox>
<asp:Label ID="Label4"runat="server"Text="ENTER YOUR YEAR"style="z-index:3;left:231px;top:237px;position=absolute;height:27px;width:115;"></asp:Label>
<asp:TextBox Id="TextBox4"runat="server"Backcolor="#0066FF"
style="z-index:4;left:429px;top:231px;position:absolute;height:36px;width:165px;"ToolTip="year"></asp:TextBox>
<asp:Button ID="button1"runat="server"Text="submit"
style="position:absolute;left:350px;top:331px;width:141px
;height:39px;"Backcolor="#3366FF" Font-Bold="true"Font-size="X-large"
onclick="button1_click"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"runat="server"
style="top:87px;left:640px;position:absolute"ControlToValidate="TextBox1"
ErrorMessage="cumpulsary to fill" Forecolor="Red">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RequiredFieldValidator1"
runat="server" style="'top:137px;z-index:1;left:10px;height:23px; width:173px;position:absolute ControlTovalidate="TextBox2"
Errormessage="ENTER VALID BRANCH"
ValidationExpression="cse/eee/ec/me/civil"
Backcolor="white"Forecolor="Red"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"runat="server"
ControlTovalidate="TextBox2"ErrorMessage="cumpulsary to fill"
Forecolor="red'style="z-index:1;left:641px;top:139px;position:absolute"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="Rangevalidator1"runat="server"
controltovalidate="Textbox3"Errormessage="please enter a valid id"
Forecolor="red"maximum value="20000" maximum value="10000"
style="z-index:1;left:8px;top:186px:position:absolute;height:27px;width:180px;right:853px"></asp:Rangevalidator>
<asp:RequiredFieldValidator="RequiredFieldValidator3"runat="server" ControlTovalidate="TextBox4" Errormessage="cumpulsary to fill"
Forecolor="Red"style="z-index:1;left:642px;top:189px;position:absolute"></asp:RequiredfIeldvalidator>
<asp:RangeValidator ID="RangeValidator2"runat="server"
ControlTovalidate="TextBox4"Errormessage="PLEASE ENTER A VALID YEAR OF COURSE"Forecolor="Red"Maximumvalue="1"
style="z-index:1;left:9px; top:228px; position:absolute; width:202px;></asp:RangeValidator> <asp:RequiredFieldValidator ID="RequiredFieldValidator4"runat="server"
ControlTovalidate="TextBox4"ErrorMessage="cumpulsary to fill"
Forecolor="Red"style="z-index:1;left:640px;top:241px;position:absolute"></asp:RequiredFieldValidator>
</form>
</body>
</html>



OUTPUT



 

A program to implement class using inheritance

class Shape
        {
            public void setWidth(int w)
            {
                width = w;
            }
            public void setHeight(int h)
            {
                height = h;
            }
            protected int width;
            protected int height;
        }
        class Rectangle : Shape
        {
            public int getArea()
            {
                return (width * height);
            }
        }
        class RectangleTester
        {
            static void Main(string[] args)
            {
                Rectangle Rect = new Rectangle();
                Rect.setWidth(5);
                Rect.setHeight(7);
                Console.WriteLine("implemrnting inheritance in class......");
                Console.WriteLine("Total area: {0}", Rect.getArea());
                Console.ReadKey();
            }
        }
    }



 Download: