Sunday, 15 December 2013

C# program to swap two numbers without using 3rd variable



In this article we will discuss with you the program for swapping two numbers without using third variable using console application in c#.net:

Step 1: Take a new console application using c# as a language in the Visual studio.
Step 2: Declare 2 variable  int_First_No, int_Second_No and take the 2 input numbers in these variables for swapping. 
Step 3: now add the 2 variable and assign it to the “int_First_No”, after this minus the “int_Second_No” from this number we will get the first variable no in the Second variable. Likewise minus the “int_First_No” from this number we will get the Second variable no in the first variable. In this way two numbers gets swapped without use of the third number.

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

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

            int int_First_No, int_Second_No;

            Console.WriteLine("please the enter the two numbers for swapping");
            int_First_No = int.Parse(Console.ReadLine());
            int_Second_No = int.Parse(Console.ReadLine());

            Console.WriteLine("Before swapping the two numbers is " + int_First_No + " and " + int_Second_No);

            int_First_No = int_First_No + int_Second_No;
            int_Second_No = int_First_No - int_Second_No;
            int_First_No = int_First_No - int_Second_No;

            Console.WriteLine("After swapping the two numbers is " + int_First_No + " and " + int_Second_No);
            Console.ReadLine();
        }
    }
}


Output:



C# program to test number is Even or Odd no



In this article we will discuss with you the program for testing a number for even and odd number using console application in c#.net using :

Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Declare a variable “int_Number” for storing the value for checking whether it is even or odd.
Step3: now take the modulo of 2 of that number, if it result in zero that means it is even no otherwise it is odd no.


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

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

            int int_Number;
           
            Console.WriteLine("please the numbers to be tested for even and odd number");
            int_Number = int.Parse(Console.ReadLine());


            if (int_Number % 2 == 0)
            {
                    Console.WriteLine("Entered number is an Even number.");
            }
            else
            {
                    Console.WriteLine("Entered number is an Odd number.");
            }
            Console.ReadLine();


        }
    }
}

Output :