Monday, June 25, 2012

Factorial using Recursion

Program to calculate Factorial using Recursion.

A
1: #include<stdio.h>  
2: int factorial(int);  
3: int main() {  
4:     int n,f;  
5:     printf("Enter number you want ot calculate factorial\n");  
6:     scanf("%d",&n);  
7:     f = factorial(n);  
8:     printf("Factorial of %d is = %d",n,f);  
9:     return(0);  
10: }  
11: int factorial(int f) {  
12:     int fact;  
13:     if(f <= 1) {  
14:        return(1);  
15:     }  
16:     else {  
17:       fact = (f*(factorial(f-1)));  
18:       return(fact);  
19:     }  
20: }  

Output :- 

1:  Enter number you want to calculate factorial 5  
2:  Factorial of 5 is = 120  


Calculate Factorial

Program to calculate Factorial.

1:  void fact() {  
2:   int n ,i,f = 1;  
3:      printf("Enter number you want to calculate factorial\n");  
4:      scanf("%d",&n);  
5:   for(i = 1 ; i <= n ; i++) {  
6:   f = f*i;  
7:   }  
8:   printf("\n Factorial of %d is = %d\n",n,f);  
9:  Output :- Enter number you want to calculate factorial 5  
10:           Factorial of 5 is = 120  

Call this function from your main 
Like :-


1:  int main() {     
2:       fact();  
3:  }  

Number is Armstrong or not?

Program to check that the number is Armstrong or not ?

1:  void armstrong() {  
2:      int arm,qou,rem,n;  
3:      for(n = 1 ; n <= 999 ; n++) {  
4:             qou = (n/100);  
5:             arm = (qou*qou*qou);  
6:             rem = (n%100);  
7:             qou = (rem/10);  
8:             rem = (rem%10);  
9:             arm = arm+(qou*qou*qou)+(rem*rem*rem);  
10:             if(n == arm) {  
11:                  printf("%d is armstrong\n",n);  
12:             }  
13:      }  
14:  }  
15:  Output :-  
16:  1 is armstrong  
17:  153 is armstrong  
18:  370 is armstrong  
19:  371 is armstrong  
20:  407 is armstrong  

Call this function from your main
Like :-



1:  void main() { 
2:        armstrong();  
3:  }  

Sunday, June 17, 2012

C Language Basics

In this blog i'll explain C in brief here i will explain only main topics of c like arrays,pointers,structures, functions etc.
 
        Decision control structures

        The loop control structures

Before starting C some things that you have to remember.
1. Size of int,float,char,short,long.

Compiler  int       float       char        short     Long
 16 -Bit 2 Bytes   4 Bytes  1 Byte 2 Bytes 4 Bytes 
 32-Bit 4 Bytes 4 Bytes 1 Bytes 2 Bytes 4 Bytes

2. How to run C programs in Linux "UBUNTU"
Steps :-

1 step :-  open terminal
               Applications -> accessories -> Terminal
2 Step :- Type command
               sudo apt-get install gcc
gcc is a compiler which will compile your C code.
3 Step :- Type command
               sudo apt-get install vim
vim is an editor.
4 Step :- Make a folder on your desktop of any name, lets say c
5 Step :- Type in your terminal
               cd Desktop    then press enter
               cd c                then press enter
               now you have entered to c folder
6 Step :- Open your folder that you have created on your desktop then right click
               Create document -> empty file
               name the file with extension .c like firstcprgrm.c
7 Step :- open the file then write your c code and save it
8 Step :- Go to terminal and type gcc "yourfile.c"
               here gcc firstcprgrm.c
               After compiling your file a file has been created with name a.out this file is used to hold the
               machine code of  a prgrm.
9 Step :- Now type ./a.out
               it will execute your c program
If you don't want to open your file again and again by going to your folder for some modification in your program then don't worry because you can open your file in your terminal .

How to open your file in terminal?
Go to terminal and mount your folder by using cd command. Now type command
 vim "filename.c" file that you want to open This command will open your file in terminal.
But to modify your code or rewrite your code by vim editor is not simple because it works in various modes when you entered your file in terminal then press i now you are in insert mode then press Esc and to save your file press :w then enter, now your file is saved, For to back to terminal press Esc and for quit type :q then press enter, now you are back to terminal you can also save and quit your file at the same time by pressing Esc then type :wq then press enter.

Now we will start c........


What is c? 
C is a powerful programming language developed at AT & T's Bell laboratories of USA in 1972. It was designed and written by a man name Dennis Ritchie . It becomes very popular without any advertisement because this language is very reliable , simple and easy to use.


Decision Control Structures

Decision controls are those which helps you to take decisions There are various decision control structures in c
1. if Statement
2. if - else statement
3. Nested if Statement
4. else if Statement
5. if under else statement

IF statement :-
syntax :-

if(condition)
{
            statement 1;
            statement 2;
            -------------
            -------------
            statement n;
}
if the condition is true then all the statements will be executed otherwise no statement will be executed

Example :-
main()
{
          int x = 10;
          if(x == 10)
          {
                   printf("condition is true\n");
                   printf("hello\n");
           }
            if(x == 2)
            {
                   printf("condition is false\n");
                   printf("bye\n");
            }
}
Output :- condition is true
                hello

In the above example first if condition is true so statements are executed successfully but the second if condition is not true so nothing will be executed.

Note :- If you will not put the braces after if condition the by default only one statement will be executed that is "condition is true" second statement will also be executed but not according to if condition .
for example :- 
main()
{
          int x = 10;
          if(x == 2)
          printf("hello");
          printf("bye");
}
Output :- bye
in the above example hello will not be printed because the condition is not true but the bye will be printed because as i told you after if without any braces by default only one statement will be executed.

if-else statement :-
Syntax :-

if(condition)
{
                   statement 1;
                   statement 2;
                   --------------
                   --------------
                  statement n;
}

else
{
                   statement 1;
                   statement 2;
                   --------------
                   --------------
                  statement n;
}

If condition is true then all the statement that comes under if condition will be executed if condition is not true then statement that comes under else will be executed.

Example :-
main()
{
           int x;
           printf("enter value of x \n");
           scanf("%d",&x);
           if(x == 10)
           {
                    printf("condition is true\n");
                    printf("I am in if block\n");
            }
            else
            {
                    printf("condition is false\n");
                    printf("i am in else block\n");
            }
}

output:-  enter value of x 10
              condition is true
              i am in if block

              enter value of x 9
              condition is false
              i am in else block

Observe that when i entered the value  of x is 10 then condition become true and statements that comes under if block gets executed and when i entered the value of x is 9 then condition become false and statements that comes under else block gets executed .  

Note :- Like "if" ,if there is no braces after else block then only one statement gets executed means only one statement that comes under else will be executed after false condition.

Nested if statement :-
syntax :-
if(condition 1)
{
         statements 1;
         if(condition 2)
         {
                   statements 2;
                   ------------
                   {
                           if(condition n)
                           {
                                       statements n;
                           }
                    }
        }
}

If the condition one is true then statements 1 will be executed and control will go to the condition 2 if condition 2 is also true the statements 2 will be executed and control will go to the next if statement and so on...... and if condition 1 is false then no other if statements will be executed. If conditions and statements continuously gets executed successfully till the condition get false.
else if statement :-
Syntax:-
if(condition 1)
{
        statement 1;
        statement 2;
        ---------------
        ---------------
        statement n;
}

else if(condition 2)
{
        statement 1;
        statement 2;
        ---------------
        ---------------
        statement n;
}

else if(condition 3)
{
        statement 1;
        statement 2;
        ---------------
        ---------------
        statement n;
}
---------------------
---------------------

else if(condition n)
{
        statement 1;
        statement 2;
        ---------------
        ---------------
        statement n;
}

Else if is used when number of condition arises in this if the condition 1 is true then the statements that comes under if block will be executed and other conditions will no be checked . if condition 1 is false then condition 2 is checked if condition 2 is true then statements that comes under esle if(condition 2) will be executed if condition 2 is false then control is passed to the next else if and condition 3 is checked and so on.........

Example :-

main()
{
           int x;
           printf("enter value of x \n");
           scanf("%d",&x);
           if(x == 10)
           {
                     printf("i am in if block\n");
                     printf("hello\n");
            }
            else if(x == 8)
           {
                     printf("i am in 1st else if block\n");
                     printf("hello\n");
            }
            if(x == 9)
           {
                     printf("i am in 2nd else if block\n");
                     printf("hello\n");
            }
            if(x == 7)
           {
                     printf("i am in 3rd else if block\n");
                     printf("bye\n");
            }
}

output :- enter value of x 10
              i am in if block
              hello

              enter value of x 8
              i am in 1st else if block
              hello

              enter value of x 9
              i am in 2nd else if block
              hello

              enter value of x 7
              i am in 3rd else if block
              bye

              enter value of x 11

How we got this output try it yourself and if you have any problem then you can comment on this blog.

If under else :-
Syntax :-


if(condition 1)
{
        statement 1;
        statement 2;
        ---------------
        ---------------
        statement n;
}


else
{
        statements;
        if(condition 2)
        {
               statements;
        }
   
}


If condition 1 is false then control will go to else bock after executing statements of else block condition  2 will be checked if condition 2 is true then statements will be executed otherwise not.
Example :-
main()
{
           int x;
           printf("enter value of x \n");
           scanf("%d",&x);
           if(x == 10)
           {
                    printf("condition is true\n");
                    printf("I am in if block\n");
            }
            else
            {
                    printf("condition is false\n");
                    printf("i am in else block\n");
                    if(x == 5)
                    {
                               printf("condition is true\n");
                               printf("Dixit singla\n");
                      }
            }
}
Output :-enter value of x 10
              condition is true
              i am in if block

              enter value of x 5
              condition is false
              i am in else block
              condition is true
              Dixit singla

Note :- if under else is same as else if this is just the another way of use of else if statement else if is better because it is easy and short.


Loop control structures 

Some times condition arises when computer  has to perform set of operations repeatedly. Computer will perform set of operations repeatedly for specific number of times or until a condition is being satisfied. this can be done through loop control instructions.
There are three main loop control instructions are :
1. For loop instruction
2. While loop instruction
3. Do - While loop instruction

For loop instruction :-
Syntax :-
for( initialize counter ; test value ; increment counter)
{
          do this;
          and this;
}
In the for loop three things are main that are
1. initialize counter
2. test value
3. increment counter
let us understand these three things with an example hope you will understand better
Example :-
main()
{
            int i;
            for(i = 0 ; i < 5 ; i++)
            {
                        printf("%d     ",i);
             }
             printf("\n loop ends");
}

Output :- 0     1     2    3     4
                loop ends
How we got this output ?
first of all i is initialized to zero then the condition is checked that is , i < 5 condition is true and statement is executed and 0 will be printed on the console then i is incremented to 1 and again condition is checked that i < 5 again condition is true and 1 will be printed on the console and so on till false condition.

i = 0     initialization
i < 5     condition is checked that is true 0 is less than 5
            0 will be printed on the console
i++       i++ means i = i + 1  that is i is incremented to 1
now i = 1
i < 5      condition is checked and it is true
             1 will be printed on the console
i++        i is incremented to 2
now i = 2

i < 5     condition is checked that is true 2 is less than 5
            2 will be printed on the console
i++       i is incremented to 3
now i = 3
i < 5     condition is checked that is true 3 is less than 5
            3 will be printed on the console
i++       i is incremented to 4
now i = 4
i < 5     condition is checked that is true 4 is less than 5
            4 will be printed on the console
i++       i is incremented to 5
now i = 5

i < 5     condition is checked and now it is false 5 is not < 5 , 5 is equal to 5
            so printf statement will not be executed
And now the lop ends and control will be passed to the next statement after for loop and "loop ends" will be printed on the console.

Note :- Like "if" statement if there is no braces after for loop then only one statement will be considered after for loop.

Nested for loop :-
for loop can be nested 
syntax :-
for( initialize counter ; test value ; increment counter)
{
          do this;
          and this;
          for( initialize counter ; test value ; increment counter)
         {
                      do this;
                      and this;
         }
}
Example :-
main()
{
          int i,j;
          for(i = 0 ; i < 4 ; i++)
          {
                    for(j = 0 ; j < 2 ; j++)
                    {
                                printf("%d  %d\n",i,j);
                     }
                     printf("out of inner loop");
          }
           printf("out of outer loop");
}
Output :- 0  0
               0  1
               out of inner loop
               1  0
               1  1
               out of inner loop
               2  0
               2  1
               out of inner loop
               3  0
               3  1
               out of inner loop
               out of outer loop
Give it your try..............

While loop instruction :-

Syntax :-
initialization;
while(condition)
{
             do this;
             and this;
             increment;
}
Example :-
main()
{
         int x = 1;
         while(x <= 4)
         {
                printf("%d   ",x);
                i++;
         }
         printf("\nend of while loop");
}
Output :- 1  2  3  4
                end of while loop
i = 1
x<=4  condition true
1 will be printed on the console
i++ now value of i is 2

i = 2
x<=4  condition true
2 will be printed on the console
i++ now value of i is 3

i = 3
x<=4  condition true
3 will be printed on the console
i++ now value of i is 4

i = 4
x<=4  condition true
4 will be printed on the console
i++ now value of i is 5
i = 5
condition false 5 is not less than equal to 4 so control is passed to the next statement after while loop that is "printf("end of while loop");"
after executing this statement end of loop will be printed on the console
Note :- like for loop you can also nested the while loop like this
Example :- 
main()
{
       int x = 1,y = 1;       
       while(x <= 4)
        {
                 while(y <= 2)
                  {
                           printf("%d  %d\n",x,y);
                           y++;
                   }
                   x++;
        }
        printf("END\n");
}
Output :- 1  1
                 1  2
                 END
x = 1
y = 1
x <= 4 condition true
y <= 2 condition true
1  1 will be printed on the console
y++ , now y = 2
y <= 2 
1  2 will be printed on the console
y++, now y = 3
y <=2  condition false
control will come out from this loop 
x++ , now x = 2
x <=4 condition true
y <=2 condition false because we did not change the y , still the value of y is 3 so condition is false and control will not enter to the inner while loop and x is incremented to 3 ad so on.........

Do While loop :-
Syntax :-
do
{
            do this;
            and this;
}while(condition);

In do-while loop at least once all the statements will be executed then the condition will be checked .
The main difference between while and do-while loop is the place where the condition is tested . In while loop first the condition is tested and if the condition is true then the statements will be executed and in do-while first the statements will be executed then the condition is checked.
Means that do-while would execute its statements at least once, even if the condition fails for the first time .The while, on the other hand will not execute its statements if the condition fails for the first time.
Example 1:-
main()
{
          int x = 1;
          do
          {
                   printf("%d   ",x);
           }while(x >= 2);
           printf("\n out of do while loop\n");
}
Output :- 1
                out of do while loop
As you can see from the output x = 1 in the first pass condition is false but 1 will be printed on the console so we can say that do while would execute its statements at least once even if the condition fails for the first time.
Example 2 :-

main()
{
          int x = 1;
          do
          {
                   printf("%d   ",x);
                   x++;
           }while(x <= 4);
           printf("\n out of do while loop\n");
}
Output :- 1   2   3  4
                out of do while loop
x =1
1 will be printed on the console
x++ now x = 2
x <= 4  condition true

2 will be printed on the console
x++ now x = 3
x <= 4  condition true
3 will be printed on the console
x++ now x = 4
x <= 4  condition true
4 will be printed on the console
x++ now x = 5
x <= 4  condition false
control will relinquish the while loop and move to the next statement after the while loop and
"out of the do while loop" will be printed on the console