Tuesday, April 19, 2016

Check the number is Armstrong or not



What is Armstrong Number

An Armstrong Number is the number which is equal to the sum of each digit raised to the Nth power.

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

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, rem, sum = 0;
            Console.Write("enter the number");
            n = Int32.Parse(Console.ReadLine());
            for(int i=n;i>0;i=1/10)
            {
                rem=i%10;
                sum=sum+rem*rem*rem;
            }
            if(sum==n)
            {
                Console.Write("Number is armstrong");
            }
            else
            {
                Console.Write("number is not armstrong");
                Console.ReadLine();
            }

        }
    }
}
 

Monday, April 18, 2016

Check the Number is Palindrome or not


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

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, rev, sum = 0, temp;
            Console.Write("Enter the number");
            num = Convert.ToInt32(Console.ReadLine());
            temp = num;
            while (num != 0)
            {
                rev = num % 10;
                num = num / 10;
                sum = sum * 10 + rev;
            }
            if (temp == sum)
            {
                Console.WriteLine("Number is palindrome");
            }
            else
            {
                Console.WriteLine("Number is not palindrome");
            }
            Console.ReadLine();
        }
    }
}

Triangle Patterns in c#

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

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

            int v1 = 6;
            int i, j, k;
            for (i = 1; i <= v1; i++)
            {
                for (j = 1; j <= v1 - i; j++)
                {
                  
                }
                for (k = 1; k <= i; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine("");
            }
            Console.ReadLine();
        }
    }
}

Output :

Sunday, April 17, 2016

Conversion Functions

These are used for converting a value from one data type to other.There are 2 functions:

Convert (<Dtype>, <Exp>, [,style])

It converts the given expression into the specified data type.

Example

Convert (int, '100') (right)
Convert (int, '100A') (wrong)


The above statement will not execute because the given expression should be compatible of being converted into a newly given datatype.

Converting date into string

select Convert (varchar(20), Getdate ())   Answer Apr 17 2016 3:09PM

While storing the date in the database, we can store only in the default format i.e., mm/dd/yy. But we are provided with an option to retrieve the date in our own required format using the Convert function by specifying the optional parameter [style]. The value of the style between ranging between 100 to 114 or 1 to 14.

Example

select Convert (varchar(20), Getdate (),101) Answer 04/17/2016

Cast (<Exp> as <Dtype>)

It is similar to the convert function but much more descriptive. It doesn’t provide the Style option just like Convert.

Example

(1) select Cast (Getdate () as varchar (20))
(2) select Cast ('100' as int)
 
System Functions

System functions are as follows:

IsNumeric (Exp)

It returns '1' if the given Exp is a numeric value or else returns '0'.

Example

(1) select IsNumeric (100) Ans: 1
(2) select IsNumeric (100A) Ans: 0

IsDate (Exp)

It returns true if the given expression is in a valid date format.

Example

(1) select IsDate (‘10/24/80’) ans 1
(2) select IsDate (‘13/10/80’) ans 0

Example
 
select EId, Ename, Salary, Salary * 12 from Employee

NOTE
(1) It is allowed to perform mathematical calculations within the select statements.

(2) While performing Arithmetic operations if one value of the expression is a Null value, the result will be Null again because arithmetic operation on Null will be Null only.
IsNull (Exp1, Exp2)

If Exp1 is Not Null, it returns Exp1 only or else If Exp1 is Null it returns Exp2.

Example
(1) select IsNull (100, 200) Ans: 100
(2)
select IsNull (Null,200) Ans: 200
SALARY + IsNull (Comm, 0)
5000 + IsNull (Null, 0) Ans: 5000
3500 + IsNull (500, 0) Ans: 4000
Select EID, Ename, Salary, Comm, Sal + IsNull (Comm, 0) from Employee

Coalese (Exp1 … Expn)

It is similar to the IsNull function which returns the first Not Null value.

Example

(1) select Coalese (Null, Null, 100, Null)  100
(2) select EID, Ename, Salary, Comm, Sal + Coalese (Comm, 0) from Employee


DataLength (Exp)

It returns the no. of bytes occupied by the given expression within the memory.

Example


Len ('Hello') ans 5 char
DataLength ('Hello') ans 5 bytes

Friday, April 15, 2016

Sending ASPX page as body of the email

In this article I am going to explain how to send aspx or html page as mail body in asp.net c#

        StringWriter sw = new StringWriter();
        string content = null;
        Server.Execute(("Test.aspx”), sw);
        content = sw.ToString();
        content = content.Replace(("" + "\r"), "").Replace(("" + "\n"), "").Trim();
        sw.Close();
        sw.Dispose();
        MailSend obj = new MailSend();
        string Mail_CC, Mail_BCC, Mail_Subject, Mail_Body = "";
        Mail_Subject = "Test Sharad";
        Mail_CC = gupta.sharad2010@gmail.com;
        Mail_BCC = "";
        Mail_Body = (content.Replace("</body>", "").Replace("</html>", "") + " <br/>");


You can use "Mail_Body " string as your mail body.

Thursday, April 14, 2016

Conditional statement in c#



The statement that can be executed based on the condition in called conditional statement. The statement is a block of code.
They are following 2 types:
1. Conditional Branching
2. Conditional Looping 

Conditional Branching
They are also 2 types:
1. If statement
2. Switch statement

If statement – If statement is used to test whether or not a condition is met.
The example of the if statement is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b;
            Console.WriteLine("Enter the number");
            a = Int32.Parse(Console.ReadLine());
            b = Int32.Parse(Console.ReadLine());
            if (a > b)
            {
                Console.WriteLine("a is greater");
            }
            else if (b > a)
            {
                Console.WriteLine("b is geater");
            }
            else
            {
                Console.WriteLine("Both are equal");
            }
            Console.ReadLine();
        }
    }
}

Switch Statement – switch statement is used to compare two logical expressions.
The example of the switch statement is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Switch
{
    class Program
    {
        static void Main(string[] args)
        {
            int value = 5;
            switch (value)
            {
                case 1:
                    Console.WriteLine(1);
                    break;
                case 5:
                    Console.WriteLine(5);
                    break;
                default:
                    Console.WriteLine("you can choose correct value");
                    break;
            }
                    Console.ReadKey();


            }
        }
    }

Conditional loops – Conditional loops is divided into 4 categories
For Loop
While Loop
Do while Loop
Foreach Loop
Each loop contains following 3 features
Initialization – it sets the starting points of loops
Condition – it sets the ending points of loops
Iteration – it is used to set the forward or backward direction of loops
The example of the For Loop is given below:
Program to generate the factorial of a number using for loop:
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 WindowsFormsApplication13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int a, fact;
            a = Int32.Parse(textBox1.Text);
            fact = 1;
            for (int i = 1; i <= a; i++)
            {
                fact = fact * i;

            }
            textBox2.Text = fact.ToString();
        }
    }
}

Output:


The example of the while loop is given below:
Program to generate the factorial of a number using while loop:
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 WindowsFormsApplication13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int a, fact;
            a = Int32.Parse(textBox1.Text);
            fact = 1;
            int i = 1;
            while (i <= a)
            {
                fact = fact * i;
                i++;

            }
            textBox2.Text = fact.ToString();
        }
    }
}

Output:



The example of the do while loop is given below:
Program to generate the factorial of a number using do while loop:
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 WindowsFormsApplication13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int a, fact;
            a = Int32.Parse(textBox1.Text);
            fact = 1;
            int i = 1;
            do
            {
                fact = fact * i;
                i++;

            }
            while (i <= a);
            textBox2.Text = fact.ToString();
        }
    }
}

Output:

For each Loops – For each loop in c# executes a block of code on each element in array .The syntax of the for each loops is given below:
string[] days = { "Sunday", "Monday", "TuesDay"};
  foreach (string day in days)
  {
                  MessageBox.Show("The day is : " + day);

  }
Author :