Monday, 28 July 2014

Use of String in C programming

String is a one dimensional array where we store group of characters. Some time we call it “character array”. Character array or strings are used by programming languages to manipulate text such as words and sentence.
Syntax of declaring a string
char string_name [string_size]
Syntax of initializing a string
char string_name [string_size]=”Your String”
Here “string_size” is the size of maximum character accepted by “string_name”. writing “char” before declaring says it is a string.
Now let’s write a program on it
#include<stdio.h>
void main()
{
    char name[]="Programs Page";
    printf("%s",name);
}
The output of this program will be
Programs Page
Here “%s” is a format specification in “printf()” to print string. We will use same specification for receiving inputs from users.
Let’s write a program on it
#include<stdio.h>
void main()
{
    char name[30];
    printf("Enter String\n");
    scanf("%s",name);
    printf("\nWelcome %s",name);
}
Above program is a small example of receiving inputs from user.in declaration of “name[30]” sets 30 byte of space for string “name[]”. Output of this program is
Enter String
Programs Page
Welcome Program
Now wait! Where is the string after space? This is a problem because “scanf()” is not capable of receiving multi-word strings. For that string “Programs Page” string become un accepted. Now let’s use “gets()” and “puts()”
#include<stdio.h>
void main()
{
    char name[5];
    printf("Enter String\n");
    gets(name);
    puts("Welcome ");
    puts(name);

}
Output of this program
Enter String
Programs Page
Welcome
Program Page
“gets()” is use for store the string inside “name” and “puts()” is use for printing. But they have some problem too. “gets()” is capable of receiving one sting at a time and “puts()” can display one string at a time.
There is a way to make “scanf()” accept multi-word string. Let’s write a program
#include<stdio.h>
void main()
{
    char name[5];
    printf("Enter String\n");
    scanf("%[^\n]s",name);
    printf("\nWelcome %s",name);
}
Output of this program
Enter String
Programs Page
Welcome Program Page
Here, [^\n] indicates that “scanf()” will keep receiving character into “name[]” until you press enter.

Saturday, 26 July 2014

2D array in C programming

In C programming it is also possible to have two or more than two dimension in array. Two dimensional array or 2D array is maximum use for matrix operation because you can operate it like a matrix. In two dimensional array we have rows and columns. The syntax of declaring two dimensional array is
data_type array_name [row][column]
we can access the element in row wise or column wise. Now let see how rows and columns index help to store value in memory.
S[0][0] S[0][1] S[1][0] S[1][1]
25 36 55 98
Now if we write it in matrix format then it will looks like
25 36
55 98
This is a 2X2 matrix.
Now let’s write a program on two dimensional matrix
#include<stdio.h>
void main()
{
    int a[3][2];
    int i,j;
//creating 2D array
    for(i=0;i<3;i++)    
{
        for(j=0;j<2;j++)
        {
            printf("enter value for [%d][%d] = ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
    printf("your matrix is\n");
//printing 2D array
    for(i=0;i<3;i++)
    {
        for(j=0;j<2;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
}
In above program we use for loop to insert value in 2D array. Here variable “i” work as row index and “j” work as column index. As we declare the data type of array as an integer so we put value as integer. In this program we create a 3X2 matrix. Now let’s see the output of this program
enter value for [0][0] = 25
enter value for [0][1] = 45
enter value for [1][0] = 88
enter value for [1][1] = 32
enter value for [2][0] = 66
enter value for [2][1] = 78
your matrix is
25  45
88  32
66  78
As you can see in output, I arranger program in this way that it will looks like a matrix, if not then it will print all value in a single line. Here 25 stored in a[0][0] and 45 stored in a[0][1] and so on. By above program we understand a 2D array is nothing but a collection of data stored in memory as a matrix.
Now in programming it may happen you need only one row to print then just change the code for printing matrix in above example with
    i=1;
    for(j=0;j<2;j++)
    {
        printf("%d ",a[i][j]);
    }
In this case only row number 2 will be printed that is
88  32
Now if we want to print a single column then just change the code for printing matrix in above example with
    j=1;
    for(i=0;i<3;i++)
    {
        printf("%d ",a[i][j]);
    }
In this case only column number 2 will be printed that is
45  32  78
In this way we can print a single value from matrix.

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