Wednesday, February 16, 2011

Mathematical Table

Mathematical Table
Lets make a Program that take input and will show mathematical table.



static void Main(string[] args)
        {
            int table;            
            Console.WriteLine("Enter Table Number");
            table = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i < 13; i++)
            {
                Console.WriteLine(table + " x " + i + " = " + i * table); 
            }
            Console.ReadKey();   
        }


Lets make another program that will show mathematical table from 1 to 12.



static void Main(string[] args)
        {
            for (int i = 1; i < 11; i++)
                for (int j = 1; j < 13; j++)
                    Console.WriteLine(i + " x " + j + " = " + i * j);
            
            Console.ReadKey();   
        }

Factorial Program

Factorial Program
Lets make a factorial program using FOR loop. First of all we should understand what's the mechanism of factorial. Factorial number are those number which multiply themselves by its lower number. If you want to find out factorial of 4 that will be ( 4 x 3 x 2 x 1 ) 24.



static void Main(string[] args)
        {
            int num;
            int result=1;
            Console.WriteLine("Enter Any Number To Find its Factorial");
            num = Convert.ToInt32(Console.ReadLine());
            for (int i = num; i > 1 ; i--)
                result *= i;
            Console.WriteLine("Factorial of "+ num +" is " + result);
            Console.ReadKey();
            
        }

Monday, February 14, 2011

Iteration Statements

Iteration Statements

We have discuss about conditional statements that what will you do if you have to take decision among multiple statements but if you have to repeat one or more statements 100 or more times then what will you do you can't write that statements 100 times and it will also reduce efficiency of you program. Iteration Statements are used to repeat statements number of times you want. Iteration or sometime called loop are used when you have to repeat a certain part of statements over and over until the expression or condition you have applied is met. Following are some Iteration Statements of Loops

  • For Loop
  • While Loop
  • Do-While Loop

For Loop
The syntax of For Loop is

for ([initializer] ; [condition] ; [iterators])
{
// For Loop Statement Body
}

Initializer : initializer is the starting value for the loop.
Condition : relational test that determines whether to execute the for loop statement body or not
Iterator : Iterator control the loop by increment o decrement (or any logical operation you want to apply )


How its Works
First of all the initializer are executed and initialize whatever value is given.
Then the condition expression is executed and if it is TRUE it will enter FOR LOOP BODY.
The FOR statement body is executed and after it iterators expression is executed.
After iterator the condition expression is executed to check if condition is still TRUE to enter the FOR body or FALSE to skip the body and end the loop.


Example


static void Main(string[] args)
        {
            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine("I = " + i);
            }
            Console.ReadKey();
        }

While Loop
The syntax of while loop is


while(condition)
{
// While Loop Block
}


While loop have only condition in its structure but initializer is made before while body and iterator can be make in while loop block.


How its Works
Initialization is make before while loop.
Condition is test to enter WHILE loop or skip the loop.
If condition is TRUE then it will enter while loop and after executing while block condition will be checked again.
And Iteration can be made inside while loop block.
Let see an example to make it clear


Example



static void Main(string[] args)
        {
            int i = 0;
            while (i < 9)
            {
                Console.WriteLine("I = " + i);
                i++;
            }
            Console.ReadKey();
        }

do-While Loop
do-while is same as while loop with additional keyword of do. do-while executes statement at least one time whether the condition is true or false.
The Syntax of do-while loop is


do
{
//do-while loop block
}
while(condition);


How its Works
Do-while loop start executing from do.
First of all do-while block is executed doesn't matter the condition is TRUE of FALSE.
After it the condition is tested to check whether to repeat the do-while body again or not. 


Example



static void Main(string[] args)
        {
            int i = 0;
            do
            {
                Console.WriteLine("I = " + i);
                i++;
            }
            while (i < 9);
            Console.ReadKey();
        }