Sponsored Ad

Sunday, May 6, 2012

Find Out if a Given Number is Divisible by 3 in C#

To check if number is divisible by 3 or not you have to use the mode operator and if reminder is 0 it means it is divisible.

Find Out if a Given Number is Divisible by 3 in C#

using System;

namespace ConsoleHub
{
    class Programs
    {       
        static void Main(string[] args)
            {

                Console.WriteLine("Enter a Number:");
                int intDiv3 = Convert.ToInt16( Console.ReadLine());
                if (intDiv3 % 3 == 0)
                {
                    Console.WriteLine("The Number {0} is Divisible by 3", intDiv3);
                }
                else
                {
                    Console.WriteLine("The Number {0} is NOT Divisible by 3", intDiv3);
                }

                Console.ReadLine();
            }       
    }   
}

Return Object of a Class from a Function in C#

You can return a object of a class from a method. This is simple program which is returning the object of share class. You can see that the class variable value is also returning.

Return Object of a Class from a Function in C#

using System;

namespace ConsoleHub
{
    class Share
    {
        public int ShareNumber = 299;
    }
    class Programs
    {       
        static void Main(string[] args)
            {
                Share ObjS = ObjFactory(); 

                Console.WriteLine("The Return Object is: ");
               Console.WriteLine(ObjS.ToString());
            Console.WriteLine(ObjS.ShareNumber);

                Console.ReadLine();
            }

            static Share ObjFactory()
            {
                Share objShare = new Share();
                objShare.ShareNumber = 201;
                return objShare;
            }       
    }   
}

Return an Array from a Method in C#

This program is a example of returning an integer array from a method. Create an integer array in the function and return it. Remember that the function return type should be array of integer.

Return an Array from a Method in C#

using System;

namespace ConsoleHub
{
    class Programs
    {       
        static void Main(string[] args)
            {
                int[] ReturnArray = ArrayFactory();

                Console.WriteLine("The Return Array is: ");
                foreach (int d in ReturnArray)
                {
                    Console.Write(" {0}", d);
                }

                Console.ReadLine();
            }

            static int[] ArrayFactory()
            {
                int[] NumberQueue = { 5, 6, 3, 8, 9 };
                return NumberQueue;
            }       
    }
}

Sponsored Ad

Development Updates