Showing posts with label .NET World. Show all posts
Showing posts with label .NET World. Show all posts

CSharp (C#) inserting and displaying elements in an array using methods

Inserting and displaying elements in an array, using methods. The size is given by user and an array with that size is created and displayed.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            arraysizeexample axe = new arraysizeexample();
            int[] localarray = axe.arrayexample(10);
            axe.showarray(localarray);                       
            Console.ReadLine();
        }
    }
    class arraysizeexample
    {
        public int[] arrayexample(int size)
        {
            int[] a = new int[size];
            for (int i =0;i
            {
                a[i] = i +1;
            }
           
            return a;
        }

        public void showarray(int[] intarry)
        {
            for (int i = 0; i < intarry.Length; i++)
            {
                Console.WriteLine("The {0} element of array is : {1}", i, intarry[i]);
            }
        }
    }
}

CSharp Ascending / Descending Order of Numbers using Arrays , Out parameter

A sample program for giving ascending and descending order of numbers using out parameter and methods returning arrays.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {           
            Sorting s = new Sorting();
            int[] num = new int[10]{-2,10,8,5,2,3,1,9,-1,0};
            int[] desc = new int[10];
            int[] final = s.Sort(num, out desc);
            Console.WriteLine("The numbers are : ");
            for (int i = 0; i < num.Length; i++)
            {
                Console.Write(num[i] + " ");
            }
            Console.WriteLine("\nThe ascending order is : ");
            for (int i=0;i
            {
                Console.Write(final[i] + " ");
            }
            Console.ReadLine();
            Console.WriteLine("The descending order is : ");
            for (int i = 0; i < desc.Length; i++)
            {
                Console.Write(desc[i] + " ");
            }
            Console.ReadLine();
        }
    }
   
    class Sorting
    {
        public int[] Sort(int[] numbers, out int[] descending)
        {
            int[] localcopy = numbers;
            int[] ascending = numbers;
            descending = new int[10];
        
            int temp = ascending[0];

            int first = 0;
            int second = 0;

            for (int j = 0; j < localcopy.Length; j++)
            {
                for (int i = 0; i < localcopy.Length - 1; i++)
                {
                    first = ascending[i];
                    second = ascending[i + 1];

                    if (first > second)
                    {
                        ascending[i] = second;
                        ascending[i + 1] = first;
                    } 
                }
            }

            for (int i = localcopy.Length -1,j=0; i >=0; i--,j++)
            {
                descending[i] = ascending[j];
            }
           
            return ascending;
        }
    }   
}

Output :

CSharp (C#) If..else..if ladder


An If..Else..If ladder is implemented in most of the programming logics. The execution happens from top to bottom. Once a condition is satisfied, remaining if else statements are all bypassed and the control moves to the next statement outside the if..else..if ladder. If none of the conditions are satisfied, then the final else will be executed.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the number between 2 and 10 : ");
            int number = Convert.ToInt32(Console.ReadLine());
            if (number % 2 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 2",number);
            }
            else if (number % 3 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 3",number);
            }
            else if (number % 5 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 5",number);
            }
            else if (number % 7 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 7",number);
            }
            else
            {
                Console.WriteLine("The number isnt between 1 and 10");
            }

            Console.ReadLine();
        }
    }

}

Output:

C Sharp ( C# ) Control Statements

CSharp (C#) Nested If..else loop Statement


Nested If is having an if condition within another if condition. When there are many conditions to be verified we could go for Nested..If

Using the similar example from If..Else

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //If a number is positive only then find if its an even number or not.
            int number = Convert.ToInt32(Console.ReadLine());
            if (number > 0)
            {
                int value = number % 2; //If the remainder is zero its an even number.
                if (value == 0)
                {
                    Console.WriteLine("The number {0} is positive and is an even number.", number);
                }
                else
                {
                    Console.WriteLine("The number {0} is positive and is an odd number.", number);
                }
            }
            else
            {
                Console.WriteLine("The number {0} is not a positive number.", number);
            }
            Console.ReadLine();
        }
    }
}

Output :



C Sharp ( C# ) Control Statements

C Sharp ( C# ) If..Else Statement


If..Else Statement is used whenever a particular condition is true, a certain set of tasks needs to be done and if the condition isn't true then another set of tasks are performed.

Syntax :
if (condition)
    {
        set of tasks or statements; (Set 1)
    }
else
    {
        set of tasks or statements; (Set 2)
    }

If the condition is satisfied, then the Set 1 statements are executed. If the condition fails, then the Set 2 statements are executed.

Example : Find if a value is even or odd. If even then print a statement, if not then print another statement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = Convert.ToInt32(Console.ReadLine());
            int value = number % 2; //If the remainder is zero its an even number.
            if (value == 0)
            {
                Console.WriteLine("The number {0} is an even number.", number);
            }
            else
            {
                Console.WriteLine("The number {0} is an odd number.", number);
            }

            Console.ReadLine();
        }
    }
}

Output :


C Sharp ( C# ) Control Statements

C Sharp ( C# ) Control Statements


Control Statements in C Sharp are statements that will help control the execution a set of statements.

Selection Statements : (Click on links below)
Iteration Statements :
  • For Loop
  • Foreach
  • While Loop
  • Do While Loop
Jump Statements :
  • break
  • continue
  • goto
  • return
  • throw

C Sharp ( C# ) Literals


C Sharp Literals is assigning a value to a variable.

In C#, literals refer to fixed values that are assigned to different variables  C# literals can be of any simple or primitive type. The way a variable is defined using a certain datatype tells us how the literal should be given.

Here are examples how literals are assigned.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //Assigning a char
            char a = 'Z';
            //Assigning a string
            string s = "Hello... how are you ?";
            //Assigning an integer
            int i = 10;
            //Assigning a uint
            uint j = 10u;
            uint k = 10U;
            //Assigning a float
            float f = 10.5678F;
            float g = 10.567f;
            //Assinging a long
            long l = 100l;
            long m = 100L;
            //Assigning a decimal
            decimal d = 5.6m;
            decimal e = 6.7M;
            //Assigning a double
            double z = 1.24d;
            double y = 3.456979879D;
            //Assigning a byte;
            byte b = 10;
            //Assiging a bool;
            bool value = true;
            bool value1 = false;
            //Assigning unsigned values for long
            ulong ul = 487UL;
            //Assigning signed byte
            sbyte sb = 20;
            //Assigning HexaDecimal Values
            int hexa = 0x2a; //This represents a decimal value of 2a which is 32+10 = 42           
        }
    }
}

C Sharp ( C# ) Decimal Data Type


The Decimal datatype is used in C# to accept number (both as plain integers and fractional values) but is mostly used for monetary calculations. The decimal type utilizes 128 bits to represent values within the range 1E–28 to 7.9E+28. The Decimal datatype eliminates most of the rounding errors that could happen in other datatypes. Thats the reason its mostly used for money calculations.

While assigning a value to the decimal datatype, suffix the value by "M" or "m".

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal price = 25.8M;
            decimal discountpercent = 10;
            decimal actualprice;

            actualprice = price - (price * discountpercent / 100);
           
            Console.WriteLine("The final price of ${0} price product after {1} percent discount is : ${2}",price,discountpercent,actualprice);
            Console.ReadLine();
        }
    }
}

Output:

C Sharp Datatypes Home Page

C Sharp (C#) Double datatype


The Double datatype is used whenever fractional values need to be assigned to a variable. Its similar to an integer datatype but has a very big range of values which can accept fractional values. Double is the most popular datatype used in most of the mathematical functions in C# while using the Math library or just assigning values.

When a variable is assigned a value which is double, then it needs to be suffix with "D" or "d".

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            double a = 22D;
            double b = 7D;
            double pivalue = a / b;// float answer = (a * a) + (b * b) + (2 * a * b); //a squared + b squared + 2ab
            Console.WriteLine("The final value of {0} / {1} is : {2}", a, b, pivalue);
            Console.ReadLine();

            double number = 1.2345D;
            double power = 2.5D;
            double finalanswer = Math.Pow(number, power);
            Console.WriteLine("The {0} to the power of {1} is {2}", number, power, finalanswer);
            Console.ReadLine();
        }
    }
}

Output:

C Sharp Datatypes Home Page

C Sharp (C#) Float Data type


Float data type is a numeric datatype that will accept numbers. Its a 32 bit sized datatype. The difference from integer datatype is that, float accepts fractional values. The float values when assigned to a variable are generally followed by a "F" at the end of the value.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            float a = 3.4567F;
            float b = 5.7679F;
            float answer = (a * a) + (b * b) + (2 * a * b); //a squared + b squared + 2ab
            Console.WriteLine("The final value is : {0}", answer);
            Console.ReadLine();
        }
    }
}

Output:

C Sharp Datatypes Home Page

C Sharp (C#) Long and ulong datatype


Long and ulong are very similar to Integer type ( int ) but has a very high range of numbers that it can accept. Long has a range of about –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and ulong has a range of about 0 to 18,446,744,073,709,551,615.

While using long datatype and assigning a long value to a variable, its recommended to suffix L with the number being assigned.
long x = 1234567890L; (something like this for long)
ulongx = 1234567890uL; (something like this for ulong)

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            long a = 1234567890L;
            long b = 1234567890L;
            long c = a * b;
            Console.WriteLine("The addition of two signed long numbers {0} and {1} is : {2}", a, b, c);
            Console.ReadLine();

            ulong d = 1234567890uL;
            ulong e = 1234567890uL;
            ulong f = d * e;
            Console.WriteLine("The addition of two unsigned long numbers {0} and {1} is : {2}", d, e, f);
            Console.ReadLine();
        }
    }
}

Output :

C Sharp Datatypes Home Page

C Sharp (C#) Short and ushort datatypes


The short and ushort datatypes that are used in C# are similar to int. The only difference between them is the range thats supported. Short supports from –32,768 to 32,767 and ushort supports from 0 to 65,535.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            short num1 = 20000;
            short num2 = 20000;
            short num3 = (short)(num1 + num2);
            Console.WriteLine("The addition of num1 and num2 is : {0}", num3);
            Console.ReadLine();

            //Here the numbers that can be accommodated by ushort but not by short.
            ushort num4 = 20000;
            ushort num5 = 20000;
            ushort num6 = (ushort)(num4 + num5);
            Console.WriteLine("The addition of num4 and num5 is : {0}", num6);
            Console.ReadLine();           
        }
    }
}

Output:

C Sharp Datatypes Home Page