Saturday, 19 July 2014

Use Array in C programming

In c programming user can create a set of similar types of data. That is call Array. Arrays is a set where uses can add data, delete data and fetch data when it is needed. There is three types of Array,
Single-Dimensional Array
Two-Dimensional Array
Multi-Dimensional Array
Here we are going to talk about Single-Dimensional Array

Let’s write a program to understand Array
#include<stdio.h>
void main()
{
    int a[5]={23,5,4,33,56};
    printf("%d\n",a[0]);
    printf("%d\n",a[1]);
    printf("%d\n",a[2]);
    printf("%d\n",a[3]);
    printf("%d\n",a[4]);
}
In above program we execute a simple example of array. Here the syntax
    int a[5]={23,5,4,33,56};
Says it is a declaration of an Array. Writing the data type before “a[]” says what type of data it is. An Array can be “int”, “float”, “char” type. Here Array of character is called as “String”. When we write “a[5]” the compiler understand it is an Array by “[]”. The number inside this array declare that how many elements we are going to put in this array. Data types of all that element is same as data types of array. We can’t put integer type and float type data together in a array.
Now once we declared the elements it is stored inside the array with an index. In array index of first element is start with “0” then it will increase by “1” for next element. As we can see in this example that “a[0]” holds the value “23” which is the first element declared in that array.
Now let’s see the output
23
5
4
33
56
In first example we write an simple program where element of array is already written. There as a way that we can get value of elements dynamically with the help of Loop Structure. Let’s write a program for that
#include<stdio.h>
void main()
{
    int i,a[5];
    printf("Enter the value inside array \n");
    for(i=0;i<5;i++)
    {
        printf("enter the value ");
        scanf("%d",&a[i]);
    }
    printf("The output of array\n");
    for(i=0;i<5;i++)
    {
        printf("%d\n",a[i]);
    }
}
In this program we take value form user and show the output. Using for loop we put value inside array using “i” as index. We can enter as many element as we want in a Array.
Let’s see the output
Enter the value inside array
enter the value 25
enter the value 12
enter the value 65
enter the value 36
enter the value 17
The output of array
25
12
65
36
17
   

Monday, 14 July 2014

Use of for,while and do-while Loop Control Structure in C programming

In programming you may have to do same operation more than one time. If we write same operation again and again then it will increases the size of your program. So we can Loop Control Structure. Using loop we can avoid this problem. It will repeating same portion of the program either a specified number of times or until particular condition is being satisfied.

There are three way to use loop control structure.
Using while Statement
Using for Statement
Using do-while Statement

While Statement

It is the simplest way to create a loop, the general form of “while” statement is
Initialize loop counter
while ( check loop counter using a condition )
{
do some operations;
loop counter;
}
This loop will continue until condition is true. The operations inside while loop will executed as many time the loop condition is true. In loop we use a loop counter by which we control the loop. Without this counter this program will executed infinitely.
Let’s write a program for “while” statement
#include<stdio.h>
void main()
{
    int i=1;
    while(i<10)
    {
        printf("%d ",i);
        i++;
    }
}
As we can see in this program that this loop will continue execution until value of variable “I” is less than 10. We use increment operator as loop counter here.
Let’s see the output for this program
1 2 3 4 5 6 7 8 9

For Statement

Create loop using for statement is most useful and maximum time used by programmer. It is most popular may be for simplicity. We can set three instruction in single line. They are “initialize counter”, “test counter” and “increment counter” can be written in single line. The general form of “for” statement is
for (initialize counter; test counter; increment counter)
{        
do some operations;
}
When we executing a “for” statement we fist initialize counter, then it will check the condition is true or not, then it will execute the operations inside it and lastly increase counter. But remember initialize counter instruction will run for the first time only. Then from the next stapes it will not initialize during loop.
Now let’s write a program for “for” statement
#include<stdio.h>
void main()
{
    int i;
    for(i=1;i<10;i++)
    {
        printf("%d ",i);
    }
}
It is the same operation we use for “while” statement. Now let’s check output
1 2 3 4 5 6 7 8 9


Do-While Statement

There is a little difference between “while” and “do-while” loop. First let see the general for this
do
{
 do some operations;
} while ( check loop counter using a condition )
The main thing is here first step of loop will executed without checking the condition.  So the operation inside the loop will be executed at least one time even when condition is fails.
Let’s write a program for “do-while” statement
#include<stdio.h>
void main()
{
    int i=1;
    do
    {
        printf("%d ",i);
        i++;
    } while(i<10);
}
You can see the difference between “while” and “do-while”. Now let’s see the output
1 2 3 4 5 6 7 8 9
You can use any one of this three in your program to create loop.

Friday, 11 July 2014

Use Nested if-else in C programs

In programming it is totally alright that you write an if-else statement inside another if or else statement. If container statement is true then control goes to inside if-else statement. We use if-else statement for decision taking. In program it may happen that inside a decision there may be more decisions control have to take. The way we write if-else statement inside another if-else statement is called nested if-else.
Let’s write a program
#include<stdio.h>
void main()
{
    int a;
    printf("Enter value for a\n");
    scanf("%d",&a);
    if(a>60)
    {
        printf("Your value is bigger than 60");
    }
    else
    {
        if(a>40)
        {
            printf("Your value is bigger than 40");
        }
        else
        {
            if(a>10)
            {
                printf("Your value is bigger then 10");
            }
            else
            {
                printf("Your value is less then 10");
            }
        }
    }
}
Here we understand how nested if-else used. If the first condition is not true then it will go for else statement, then inside that else we write another if-else statement. Remember we can write if-else statement inside another if statement too. Using nested if-else will add a good clarity to a program and very useful in decision making.
Now let’s see the output for this program
Enter value for a
55
Your value is bigger than 40

Sunday, 6 July 2014

Use of Logical Operators in C programs

Logical Operator play very important role in C programming. Here mainly 3 types of logical operators,
&& – logical AND
|| – logical OR
! – logical NOT
Don’t write single & or single | which will make different meaning for this symbols. The first two operators, && and || help us to set two or more than two conditions in a single statement.
For && operator if both side conditions is true then it’s return true and for || operator if single side condition is true then it will return false.
If we create a table to see how it’s work then

For && operator
Condition1 Condition2 Output
True True True
True False False
False True False
False False False

For || operator
Condition1 Condition2 Output
True True True
True False True
False True True
False False False

Now let’s write a program for && operator
#include<stdio.h>
void main()
{
    int a,b,c;
    printf("Enter Value For a\n");
    scanf("%d",&a);
    printf("Enter Value For b\n");
    scanf("%d",&b);
    printf("Enter Value For c\n");
    scanf("%d",&c);
    //using only "if else" statement
    if(a>b)
    {
        if(a>c)
        {
            printf("a have max value\n");
        }
    }
    else if(b>c)
    {
        printf("b have max value\n");
    }
    else
    {
        printf("c have max value\n");
    }
    //using Logical operator
    if((a>b)&&(a>c))
    {
        printf("a have max value\n");
    }
    if((b>a)&&(b>c))
    {
        printf("b have max value\n");
    }
    if((c>a)&&(c>b))
    {
        printf("c have max value\n");
    }
}
Now let’s see output for this program.
Enter Value For a
45
Enter Value For b
32
Enter Value For c
66
c have max value
c have max value
first result for without using && and second result is with &&.
Now let’s write a program for || operator
Here before we see program let’s set the condition. If a given value is grater than 20 or equal to 10 or less than 5 then this condition is true.
#include<stdio.h>
void main()
{
    int a;
    printf("Enter value for a\n");
    scanf("%d",&a);
    if((a<5)||(a==10)||(a>20))
    {
        printf("Condition is True");
    }
    else
    {
        printf("Condition is Not True");
    }
}
Now let’s see the output for this program
Enter value for a
4
Condition is True
Enter value for a
11
Condition is Not True
So we know about && and || but we don’t know about “!” operator. This NOT(!) operator is not used so much in programming. Let’s clear the idea about it by writing a program.
#include<stdio.h>
void main()
{
    int a;
    printf("Enter value for a");
    scanf("%d",&a);
    if(!(a>7))
    {
        printf("Value of a is smaller than 7");
    }
    else
    {
        printf("Value of a is bigger than 7");
    }
}
Let me explain this for you. Here if “a” is smaller than 7 the condition becomes true, that’s means just opposite of (a>7) condition. I know that’s confusing, but you can avoid it in programming.
Now let’s see the output for this program.
Enter value for a
6
Value of a is smaller than 7

Thursday, 3 July 2014

Use of Relational Operators in C Programs

In programming it may happen sometime that control have to take decision. But problem is in programming there is two conditions. “True” and “False”. So we need help to express that condition. Relational Operators help us in this stage. There are total 6 type of rational operators
X==Y if true X is equal Y
X>Y if true X is greater then Y
Y<X if true Y is less than X
X!=Y if true X is not equal to Y
X=>Y if true X is greater than equal to Y
X=<Y if true Y is greater than equal to X
Let’s see an example with “if” statement
#include<stdio.h>
void main()
{
    int a,b;
    printf("Enter value for a\n");
    scanf("%d",&a);
    printf("Enter value for b\n");
    scanf("%d",&b);
    if(a==b)
    {
        printf("a is equal to b\n");
    }
    if(a>b)
    {
        printf("a is greater than b\n");
    }
    if(a<b)
    {
        printf("a is less than b\n");
    }
    if(a>=b)
    {
        printf("a is greater than equal to b\n");
    }
    if(a<=b)
    {
        printf("a is less than equal to b\n");
    }
    if(a!=b)
    {
        printf("a is not equal to b\n");
    }
}
The program is written in this way that if condition is true then that will be printed but if not true then not printed. So here the expression goes through all “if” statements. Now let’s see the output
Enter value for a
34
Enter value for b
23
a is grater then b
a is grater then equal to b
a is not equal to b
So all true condition is printed.

Wednesday, 2 July 2014

Know about Arithmetic and Assignment Operator with example in C programming

Arithmetic Operator is very important for programming. All mathematical operations are done by this operator. This operators are “+”,”–“,”*”,”/”. 
With Arithmetic Operators we use Assignment Operator that is “=”. The structure to use this operators is
“Variable = Arithmetic Operations”.
 There in Left side we use variable which hold the result value of Right side operations.
In any program we do multiple operations. The compiler follow the mathematical hierarchy that is “*” and “/” operation done first, then “+” and “-“. Sometime this hierarchy depend upon operation.
Now let’s see an example
#include<stdio.h>
void main()
{
    int a,b,c;
    printf("Enter value for variable A\n");
    scanf("%d",&a);
    printf("Enter value for variable B\n");
    scanf("%d",&b);
    c=a+b;
    printf("Result for Addition %d\n",c);
    c=a-b;
    printf("Result for Subtraction  %d\n",c);
    c=a*b;
    printf("Result for Multiplication %d\n",c);
    c=a/b;
    printf("Result for Division %d\n",c);
}
In this example we use all type of arithmetic operations. Now let’s see output
Enter value for variable A
12
Enter value for variable B
6
Result for Addition 18
Result for Subtraction 6
Result for Multiplication 72
Result for Division 2
We can execute multiple operations in a single line. For that we use brackets “()”. The operation inside “()” are done first. Now let’s find out the output of an equation
This equation is
(a+(b*c))/(d-c)
Let’s first see the program
#include<stdio.h>
void main()
{
    int a,b,c,d,e,f;
    float result;
    a=24;
    b=32;
    c=10;
    d=20;
    e=a+(b*c);
    f=d-c;
    result=(float)e/f;
    printf("So the result is %.2f",result);
}
As we can see first we divide the equation in two part and simplify the operation then calculate those part separately. At the end we conclude the final operation.
The result for This operation is
So the result is 34.40

Sunday, 29 June 2014

Use Increment and Decrement Operator in C Programming

In programming it may happen that you need to increase or decrease value of a variable by one. Increment and decrement operator are used for that purpose. Let’s take “a” as an integer variable, in Increment Operator it is written as “a++” or “++a” which have same mining as “a=a+1”. In Decrement Operator it is written as “a--” or “--a” which have same mining as “a=a-1”.
Now when we write “++a” called as Prefix Increment and for “a++” it is called as Postfix Increment. Prefix Increment make immediate increment of variable value where postfix make increment in next call.
Same way when we write “--a” called as Prefix Decrement and for “a--” it is called as Postfix Decrement. Prefix Decrement make immediate decrement of variable value where postfix make decrement in next call.
Now let’s see the example
#include<stdio .h>
void main()
{
    int a;
    a=1;//lets take value of a as 1
    printf("The Value Of a is %d\n",a++);
    printf("The Value Of a is %d\n",a);
    a=1;//reset the value of a
    printf("The Value Of a is %d\n",++a);
    printf("The Value Of a is %d\n",a);
    a=1;//reset the value of a
    printf("The Value Of a is %d\n",a--);
    printf("The Value Of a is %d\n",a);
    a=1;//reset the value of a
    printf("The Value Of a is %d\n",--a);
    printf("The Value Of a is %d\n",a);
}
To Know how increment operator works we must see output
The Value Of a is 1
The Value Of a is 2
The Value Of a is 2
The Value Of a is 2
The Value Of a is 1
The Value Of a is 0
The Value Of a is 0
The Value Of a is 0
Here for “a++” the value is not increased in first call but in next call it’s increase by 1. Now for “++a“ the value is increased in first call and in next call it show same result.
Here for “a--” the value is not decreased in first call but in next call it’s decrease by 1. Now for “--a“ the value is increased in first call and in next call it show same result.