Wednesday, April 20, 2016

C # operators



An operator is the symbol that tells the compiler to perform mathematical or logical operations. There are different kinds of operators is given below:

Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc. Operators

Arithmetic Operators
If a=10,b=5



Operator
Description
Example
         +
Adds Tow operands
a+b=15
        -
Subtract second to first operands
a-b=5
        *
Multiply Both operands
a * b=50
        /
Divided numerator by de-numerator
a/b=2
       %
Reminder after integer division
a%b=0
      ++
Increase integer value one by one
a++=11
     --
Decrease integer value one by one
a--=9

Relational operators-
 If A=10, B=5
Operator
Description
Example
==
Checks the value of two operands is equal or not
A==B(it is not true)
!=
Checks the value of two operands is not equal
A!=B(It is true)
Checks the value of left operand is greater than value of right operand
A>B(It is true)
Checks the value of left operand is greater than value of right operand
A<B(It is not true)
>=
Checks the value of left operand is greater than or equal to the value of right operand
A>=B(It is true)
<=
Checks the value of the left operand is less than or equal to the value of right operand
A<=B(it is not true)

Logical operator-
Operator
Description
Example
&&
It is called AND operator the condition becomes true if both operand are non-zero
A&&B (false)
||
It is called OR operator the condition becomes true if any of the two operand is non-zero
A||B(true)
!
`it is called Logical NOT operator, if the condition is true then Logical NOT operator will make false
!(A&&B) true

Bitwise Operator -
Bitwise operator work on the bit, the truth table for &, |, ^ is given below:
A
B
A&B
A|B
A^B
0
0
0
0
0
0
1
0
1
1
1
1
1
1
0
1
0
0
1
1


Assignment Operator



Operator
Description
Example
=
Use to assign the values from right side operand to left side operand
C=A+B assign the value of A+B to C
+=
It adds right operand to left Operand and assign the value(result) to the left operand
A+=B is equal to A=A+B
-=
It subtract right operand from left operand and assign the result to left operand
A-=B is equal to A=A-B
*=
It multiply right operand to left operand and assign the value to left operand
A*=B is equal to A=A*B
/=
It divides right operand to left operand and assign the value to left operand
A/=B is equal to A=A/B
%=
It is used to find the modulus of two operand the assign the value to the left operand
C%=A is equal to C=C%A
<<==
Left shift AND assignment operator
A<<==2 is equal to A=A<<==2
>>==
Right shift AND assignment operator
A>>==2 is equal to A=A>>==2
&=
It is called Bitwise AND assignment operator
A&=2 is equal to A=A&2
^=
It is called bitwise exclusive OR and assignment operator
A ^=2 is equal to A=A ^2
|=
It is called bitwise inclusive OR and assignment operator
A|=2 is equal to A=A|2


Miscellaneous operator



Operator
Description
Example
Sizeof()
It return the size of a data type
Sizeof(int), return 4
typeof()
It return the type of class
Typeof(StreamReader);
&
Return the address of an variable
&a
*
Pointer to variable
*a
?:
Conditional Expression
If condition is true? Then value of X otherwise value of Y

 


 

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.