Sunday, 16 November 2014

Use Character Data type in java programming

In java we use ‘char’ to store the character data type in variables. Maybe it looks same like ‘char’ we use in c and c++ programming. But it’s different. In c and c++ programming we use ‘char’ that is 8 bit wide. Btu for java programming it is 16 bit wide. As java use Unicode to define characters. Unicode is define a fully international character set that defines all character found in human languages. As java is made to use in worldwide it is significant to use Unicode.
Let’s write a program
public class useChar {
public static void main(String args[])
{
char chr1,chr2;
chr1 = 66;
chr2 = 'B';
System.out.println("chr1 and chr2 presnts "+chr1+" and "+chr2);
}
}
Let’s see output of above program
chr1 and chr2 presnts B and B
In above program we can see that ‘chr1’ hold a number but it print a character. It’s happen because of ASCII value. The number 66 represent character ‘B’. The ASCII character set occupies the first 127 values in the Unicode character set.
We can do arithmetic operation with char value as it is can also be thought of as an integer type. Let’s try an example
public class useChar {
public static void main(String args[])
{
char ch;
ch = 65;
System.out.println("chr presnts "+ch);
ch++;
System.out.println("chr presnts "+ch);
}
}
Output of above program
chr presnts A
chr presnts B
In above program we use increment operator where ch is a character type. It shows that it’s possible to do arithmetic operations with character data types.

Wednesday, 22 October 2014

Use pow and sqrt functions in C programming

In programming some time we need to use some mathematical operations. In math.h header defines various mathematical functions. Some important of them are pow and sqrt.. function of pow and sqrt is already defined in math.h header file. Let see how to use them
POW
Function pow is used for find out power of any integer. For example if we need to find out 25 then we use it.
Now let’s write the code
#include<stdio.h>
#include<math.h>
void main()
{
    float x,y,z;
    printf("enter the value");
    scanf("%f",&x);
    printf("enter the power");
    scanf("%f",&y);
    z=pow(x,y);
    printf("the answer is: %.2f",z);
}
The output of above program is
enter the value 2
enter the power 5
the answer is: 32.00
SQRT
Function sqrt used when we try to find out square root of any integer. For example if we need to find out √3 then we use it.
Now let’s write the code
#include<stdio.h>
#include<math.h>
void main()
{
    float x, z;
    printf("enter the value");
    scanf("%f",&x);
    z=sqrt(x);
    printf("the answer is: %.2f",z);
}
The output of above program is
enter the value 3
the answer is: 1.73

Friday, 17 October 2014

Find Factorial of a number using C programming

Find Factorial of a number using C programming
In mathematics, the factorial of a non-negative integer n, denoted by n! is the product of all positive integers less than or equal to n. For example,
    5! = 5 X 4 X 3 X 2 X 1 = 120.
Also factorial of 0! Is 1.
We are going to find out factorial by using C programming. Let’s see the code
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,k=1;
    printf("Enter your number");
    scanf("%d",&i);
    for(j=1;j<=i;j++)
    {
        k=k*j;
    }
    printf("Factorial of number %d is %d",i,k);
}
In above program we use for loop to calculate the factorial. When we insert any value in this loop that value will be multiplied by all other integer value less than inserted value until loop ends.
Now let’s see the output
Enter your number 5
Factorial of number 5 is 120

Saturday, 11 October 2014

Differences between C and C++


C and C++ are very important programming languages. As C is older than C++ there some difference between C and C++ language. They are:
1. C is Procedural Language but C++ is  multi-paradigm language i.e. procedural as well as object oriented.

2. The concept of virtual Functions are used in C++ but No virtual Functions are present in C.

3. In C Top down approach is used in Program Design where in C++ Bottom up approach adopted in Program Design.

4. No namespace Feature is present in C Language as it is not needed but Namespace Feature is present in C++ for avoiding Name collision example "using namespace std;".

5. Multiple Declaration of global variables are alloed in C but Multiple Declaration of global varioables are not allowed in C++.

6. In C scanf() Function used for Input and printf() Function used for output but in C++ Cin>> Function used for Input and Cout<< Function used for output.

7. Polymorphism is not possible in C but in C++ the concept of polymorphism is used.

8. Operator overloading is not possible in C where Operator overloading is a Feature of C++.

9. Mapping between Data and Function is difficult and complicated in C programming Mapping between Data and Function can be used using "Objects" in C++ programming.

10. In C, we can call main() Function through other Functions in that programming but in C++, we cannot call main() Function through other functions.

11. No inheritance is possible in C where Inheritance is possible in C++.

12. In C, malloc() and calloc() Functions are used for Memory Allocation and free() function for memory Deallocating where In C++,  new and delete operators are used for Memory Allocating and Deallocating.

13. In C, Exception Handling is not present where in C++, Exception Handling is done with Try and Catch block.

14. C requires all the variables to be defined at the starting of a scope C++ allows the declaration of variable anywhere in the scope i.e at time of its First use.

15. We can use functions inside structures in C++ programming but not in C programming.

16. We can say C is low level language but C++ is middle-level language.

17. C++ supports Exception Handling while C does not.

18. In case of C, the data is not secured but for C++, the data is secured.

Wednesday, 24 September 2014

Search an element in array

As we know an array can store more than one element.  In programming you may have to search that element in array. and then do some important operation.
Let’s write the program
#include<stdio.h>
#include<conio.h>
int arr[100],i,p=1,length;
int search(int);
int display(int);
void main()
{

    printf("enter the length of the arry:(<=100)");
    scanf("%d",&length);
    if(length<=100)
    {
    printf("length successfully entered.And the legth is:%d \n",length);
    }
    else
    {
    printf("length is invalid.\n");
    main();
    }
    length--;
    printf("Enter the elments of the array:\n");
    for(i=0;i<=length;i++)
    {
        printf("\n%d position's:",p++);
        scanf("%d",&arr[i]);
    }
    display(length);
search(length);

   getch();
}
int display(int length)
{
    printf("\nNow the array is:\n");
    for(i=0;i<=length;i++)
    {
        printf("%d\t",arr[i]);
    }
}
int search(int length)
{
   int c, first, last, middle, n, search;

    n=length;
    printf("\nEnter value to find\n");
    scanf("%d",&search);

    first = 0;
    last = n;
    middle = (first+last)/2;

    while( first <= last )
    {
        if ( arr[middle] < search )
        first = middle + 1;
        else if ( arr[middle] == search )
        {
            printf("%d found at location %d.\n", search, middle+1);
            break;
        }
        else
            last = middle - 1;

            middle = (first + last)/2;
    }
    if ( first > last )
    printf("Not found! %d is not present in the list.\n", search);

   return 0;
}
In above program we take an array and put element in it. Then we search for an element which is already in that array. If given element does not match with any element in array then it will show not found.
Now let’s see output
enter the length of the arry:(<=100)5
length successfully entered.And the legth is:5
Enter the elments of the array:
1 position's: 65
2 position's: 55
3 position's: 41
4 position's: 33
5 position's: 87
Now the array is:
65 55 41 33 87
Enter value to find 41
41 found at location 3

Friday, 19 September 2014

Array insertion and deletion operation in C programming

In array we hold elements. Now in programming some time you may need to add new value in array and delete old value from array. We can do that by some simple coding. But first we need to know insertion and deletion of new value in an array.
When we want to insert new element in array first we need to clear the position of array where we want to add new element. Then shift other element at the right side of target position by one to the right. Then add new element to that target position. For example
25 35 45 87 99
We want to insert new element at position 3, So first we need to shift other element at the right side of target position by 1 to the right.
25 35 _ 45 87 99
Now we put new element at target position, 79 is our new element
25 35 79 45 87 99
Now, for deletion of old element first we find the position of that old element. Then we need to shift other element at the right side of target position by 1 to the left. The element will deleted automatically. For example
25 35 45 87 99
Suppose we want to delete 45 then just move other element to the left by 1, for this case 87 and 99 at the right side of target position. So then move to the left of their position by 1. Then it become
25 35 87 99
Now let’s write the program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int arr[100],val,pos,i,p=0,length,ch,count=0,j;
int insert(int);
int deletion(int);
int display(int);
void main()
{
    printf("enter the length of the arry:(<=100)");
    scanf("%d",&length);
    if(length<=100)
    {
    printf("length successfully entered.And the legth is:%d \n",length);
    }
    else
    {
    printf("length is invalid.\n");
    main();
    }
    printf("Enter the elments of the array:\n");
    for(i=0;i<length;i++)
    {
        printf("\n%d position's:",p++);
        scanf("%d",&arr[i]);
    }
    length--;
    display(length);
    while(1)
    {
        printf("\nChose your option:1:insertion 2:deletion 3:display 4:Exit\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
            printf("enter the position you want to insert a value(position should be counted from 0):");
            scanf("%d",&pos);
            insert(pos);
            break;
            case 2:
            printf("Enter the value,which you want to delete:");
            scanf("%d",&val);
            deletion(val);
            break;
            case 3:
            display(length);
            break;
            case 4:
            exit(0);
            default:
            printf("Wrong choice....Pls Enter A valid entity...");
            switch(ch);
        }
    }getch();
}
int display(int length)
{
    printf("\nNow the array is:\n");
    for(i=0;i<=length;i++)
    {
        printf("%d\t",arr[i]);
    }
}
int insert(int pos)
{
    if(pos<=length+1)
    {
        if(pos<=100 && length<=100)
        {
            printf("Enter the value you want to insert:");
            scanf("%d",&val);
            length++;
            for(i=length;i>pos;i--)
            {
                arr[i]=arr[i-1];
            }
            arr[pos]=val;
        }
        display(length);
    }
    else
    {
        printf("Invalid position or length...");
        switch(ch);
    }
}
int deletion(int val)
{
    for(i=0;i<=length;i++)
    {
        if(arr[i]==val)
        {
            j=i;
            while(j<=length)
            {
                arr[j]=arr[j+1];
                j++;
            }
            length--;
            count++;
            i--;
        }
    }
    if(count!=0)
    {
        printf("\n%d value(s) have deleted",count);
    }
    else
    {
        printf("you have entered a wrong value");
    }
    count=0;
    display(length);
    switch(ch);
}
In above program we use switch-case for insertion and deletion operation. We can do those operation many times. Now let’s see the output
enter the length of the arry:(<=100)5
length successfully entered.And the legth is:5
Enter the elments of the array:

0 position's: 55
1 position's: 12
2 position's: 89
3 position's: 36
4 position's: 41
Now the array is:
55 12 89 36 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
1
enter the position you want to insert a value(position should be counted from 0):2
Enter the value,which you want to delete:75
Now the array is:
55 12 75 89 36 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
2
Enter the value,which you want to delete: 36
1 value(s) have deleted
Now the array is:
55 12 75 89 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
3
Now the array is:
55 12 75 89 41
Chose your option:1:insertion 2:deletion 3:display 4:Exit
4



Monday, 15 September 2014

Use break and continue in C programming

“break” and “continue” are the keyword in C language. They are used to control loop structure. In programming somewhere in loop where you want to stop loop if some condition is become true or false then we will use break; to stop that loop. Same way if you want to skip some part of your loop if your condition is become true or false, then we will use continue; to skip those part.
Now let’s write the program for break;
#include<stdio.h>
void main()
{
    int i;
    for(i=1;i<=5;i++)
    {
        if(i==3)
        {
            break;
        }
        printf("%d\n",i);
    }
}
Now here we can see that we use break if any time in loop “i==3” the loop will stop executing. Now let’s see the output of above program.
1
2
Now let’s write the same program for continue;
#include<stdio.h>
void main()
{
    int i;
    for(i=1;i<=5;i++)
    {
        if(i==3)
        {
            continue;
        }
        printf("%d\n",i);
    }
}
Now here we can see that we use continue if any time in loop “i==3” the loop will skip that part. Now let’s see the output of above program.
1
2
4
5


Sunday, 17 August 2014

6 important star pattern printing using C language

using C language we can print patterns, some of useful patterns are
Square printing
Code-
#include<stdio.h>
void main()
{
    int i,j,n;
    printf("Enter The limit");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            printf("* ");
        }
        printf("\n");
    }
}
Output
Enter The limit 5
*  *  *  *  *
*  *  *  *  *
*  *  *  *  *
*  *  *  *  *
*  *  *  *  *
Triangle printing
Code-
#include<stdio.h>
void main()
{
    int i,j,n;
    printf("Enter The limit");
    scanf("%d",&n);
for(i=0;i<n;i++)
        {
        for(j=0;j<i+1;j++)
            {
printf("*");
            }
printf("\n");
        }
}
Output
Enter The limit 5
*
*  *
*  *  *
*  *  *  *
*  *  *  *  *
Pyramid printing
Code-
#include<stdio.h>
void main()
{
    int i,j,k,n;
    printf("Enter The limit");
    scanf("%d",&n);
for(i=1;i<=n;i++)
        {
        for(k=n;k>=i;k--)
            {
                printf(" ");
            }
        for(j=1;j<i+1;j++)
            {
printf("* ");
            }
printf("\n");
        }
}
Output
Enter The limit 5
     *
    * *
   * * *
  * * * *
 * * * * *
Reverse pyramid printing
Code-
#include<stdio.h>
void main()
{
    int i,j,k,n;
    printf("Enter The limit");
    scanf("%d",&n);
for(i=1;i<=n;i++)
        {
        for(j=1;j<i+1;j++)
            {
printf(" ");
            }
        for(k=n;k>=i;k--)
            {
                printf("* ");
            }
printf("\n");
        }
}
Output
Enter The limit 5
 * * * * *
  * * * *
   * * *
    * *
     *
Diamond printing
Code-
#include<stdio.h>
void main()
{
    int i,j,k,n;
    printf("Enter The limit");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        {
        for(k=n;k>=i;k--)
            {
                printf(" ");
            }
        for(j=1;j<i+1;j++)
            {
printf("* ");
            }
printf("\n");
        }

for(i=1;i<=n;i++)
        {
        for(j=1;j<=i+1;j++)
            {
printf(" ");
            }
        for(k=n;k>i;k--)
            {
                printf("* ");
            }
printf("\n");
        }
}
Output
     *
    * *
   * * *
  * * * *
 * * * * *
  * * * *
   * * *
    * *
     *
Polygon printing
Code-
#include<stdio.h>
void main()
{
    int i,j,k,n;
    printf("Enter The limit");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if((i==0)||(i==n-1))
            {
                printf("* ");
            }
            else
            {
                if((j==0)||(j==n-1))
                {
                    printf("* ");
                }
                else
                {
                    printf("  ");
                }
            }
        }
        printf("\n");
    }
}
Output
Enter The limit 5
*  *  *  *  *
*           *
*           *
*           *
*  *  *  *  *


Tuesday, 12 August 2014

Find prime numbers using C programming

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example 7 is a prime number because you can’t divide 7 by any other number except 1 and 7. 9 is not a prime number because you can divide 9 by 3.
Some example of prime number is
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997….etc.
Let’s write a code to find out a number is prime or not
#include<stdio.h>
void main()
{   int n,i,flag=1;
    printf("Enter your number You want to check ");
    scanf("%d",&n);
    for(i=2;i    {
        if(n%i==0)
            flag=0;
    }
    if(flag==1)
    {   printf("This Number is Prime");
    }
    else
    {   printf("This is Not Prime");
    }
}
In above program we find out if given number is divided by any smaller number. For that we are using for loop to check every number. Also we set a flag to find the prime.
Output of above program
Enter your number you want to check 811
This Number is Prime
Enter your number you want to check 364
This is Not Prime
Now above program is a small example to find that your number is prime or not. But what if you need to find out all prime numbers within a range. After a few modification we can run program easily.
The code for finding prime numbers within a range is
#include<stdio.h>
void main()
{   int i,j,n,flag;
    printf("Enter your range ");
    scanf("%d",&n);
    for(j=1;j<=n;j++)
    {
        flag=1;
        for(i=2;i        {
            if(j%i==0)
            {
                flag=0;
            }
        }
        if(flag==1&&j!=1)
        {   printf("%d ",j);
        }
    }
}
Above program is written in same concept that used in first program. It will print all prime numbers within a range. Now let’s find output of above program.
Enter your range 20
2, 3, 5, 7, 11, 13, 17, 19,

Monday, 11 August 2014

Palindrome operations in C language

A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or backward. For example if we write “madam”, this string is same for forward and backward reading. Also number 121 is a palindrome as forward and backward reading return same value.
Now let’s write code for integer numbers
#include<stdio.h>
void main()
{
    int i=0,n,m;
    printf("Enter your number you want to check ");
    scanf("%d",&n);
    m=n;
    while(n!=0)
    {   i=(i*10)+(n%10);
        n=n/10;
    }
    if(i==m)
    {   printf("Your given Number %d is Palindrome",m);
    }
    else
    {   printf("Your given Number %d is not Palindrome",m);
    }
}
In above program we insert a “while loop” to reverse the number. Then check the number is Palindrome number. Now let’s see the output
Enter your number you want to check 121
Your given Number 121 is Palindrome
Enter your number you want to check 123
Your given Number 123 is not Palindrome
Now let’s do same operation on string, the code is
#include<stdio.h>
#include<string.h>
void main()
{
   char m[50], n[50];
   printf("Enter the string you want to check\n");
   gets(m);
   strcpy(n,m);
   strrev(n);
   if( strcmp(m,n) == 0 )
      printf("Your given string %s is a Palindrome\n",m);
   else
      printf("Your given string %s is not a Palindrome\n",m);
}
Here we do some string operation, where we first copy of string from “m” to “n” then we reverse the string of “n” then check if string “m” and “n” is same or not.
The output for above program
Enter the string you want to check
madam
Your given string madam is a Palindrome
Enter the string you want to check code
Your given string code is not a Palindrome

Create Fibonacci sequence using C language

Fibonacci sequence is the number with following sequence
0,1,1,2,3,5,8,13,21,34,55,89,144……
Where every number is the sum of his previous two numbers. For example find any number in series, take its position as “f” now value of “f” can be calculated by
f = (f-1) + (f-2)
Now let’s write the code for it
#include<stdio.h>
void main()
{
    int n,a=0,b=1,i,c=0;
    printf("Enter the range of your number ");
    scanf("%d",&n);
    printf("Your series is \n");
    printf("%d %d ",a,b);
    for(i=2;i    {   c=a+b;
        printf("%d ",c);
        a=b;
        b=c;
    }
}
In above program first two value is constant, for that we print them differently. The we create a “for loop” which will give other values. Now let’s see the output
Enter the range of your number 10
0 1 1 2 3 5 8 13 21 34

Sunday, 10 August 2014

swap variable value in C language using 3rd variable and without using 3rd variable


We can swap value of two variable using two different way. Using 3rd variable and without using 3rd variable. first we need to know what is swap operation? In swap operation we seap the value of two variable with each other. After completing operation they interchange there value.
Now let’s start with
Using 3rd variable
The code is
#include<stdio.h>
void main()
{
    int a,b,c;
    printf("Enter value for variable A");
    scanf("%d",&a);
    printf("Enter value for variable B");
    scanf("%d",&b);
    c=a;
    a=b;
    b=c;
    printf("After swap operation A = %d and B = %d",a,b);
}
This is a simple operation. We first put value of “a” inside “c”. Then put value of “b” inside “a”, so value of “a” remove by new value of “b”. then we put value of “c” inside “b”. so variable “a” and “b” interchange there value with 3rd using 3rd variable “c”
Output of above program is
Enter value for variable A 45
Enter value for variable B 29
After swap operation A = 29 and B = 45
Without using 3rd variable
The code is
#include<stdio.h>
void main()
{
    int a,b;
    printf("Enter value for variable A ");
    scanf("%d",&a);
    printf("Enter value for variable B ");
    scanf("%d",&b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("After swap operation A = %d and B = %d",a,b);
}
In above code we did not use any 3rd variable. first add value of “a” and “b” and put it to “a”. Now we subtract “b” from “a”, by which we get value of old “a” again ant put it to “b”. now we subtract “b” from “a” again by which we will get old “b” value again and put it in “a”.
Output of above program
Enter value for variable A 99
Enter value for variable B 34
After swap operation A = 34 and B = 99

Thursday, 7 August 2014

pointers in C language


Pointer is an important notation in c programming. Pointers points the location of memory. In programming when we declare a variable then, for example
int a = 36;
after looking at the declaration we understand that “a” is a location name in memory and 36 is the value that memory hold. But there is one more thing, the memory have its own location number. The location number may be 34456. Computer select that location itself to place value 36. It not constant, computer may set different location in next compilation. The important thing is “a” address to a memory is a number.
Let’s write a program to understand pointer
#include<stdio.h>
void main()
{
    int a=20;
    printf("address of a = %u\n",&a);
    printf("value of a = %d\n",a);
    printf("value of a = %d\n",*(&a));
}
Before we understand the program let’s see the output of above program is
address of a = 2686748
value of a = 20
value of a = 20
in first “printf()” we printing the memory address of variable “a”. “&” use in this statement is “address of” operator in C language. “&a” return the address of variable “a”, in this case which is “2686748”. This value is not constant, it may different for different program. Here we use “%u” to print address which is a format specifier for unsigned integer.
The other pointer operator in c is “*” called value at address.  It’s give the value stored in a particular address. In above program we print “*(&a)” is produce same result as “a”.
Let’s write another program
#include<stdio.h>
void main()
{
    int a=20;
    int *b;
    b=&a;
    printf("Address of a = %u\n",&a);
    printf("Address of a = %u\n",b);
    printf("Address of b = %u\n",&b);
    printf("Value of b = %u\n",b);
    printf("Value of a = %d\n",a);
    printf("Value of a = %d\n",*(&a));
    printf("Value of a = %d\n",*b);
}
In above program we declare “*b”. it means that variable b will contain an address. Then we write “b=&a” then variable “b” hold address of “b”. That’s why when we printing the value of “b” it will print the address of “a”. we can print the value stored in that address by “*b”.
The output of above program is
Address of a = 2686748
Address of a = 2686748
Address of b = 2686744
Value of b = 2686748
Value of a = 20
Value of a = 20
Value of a = 20

Sunday, 3 August 2014

structure in c programming

In C programming we need to store different types of data types together then we need help of “structure”. A structure is a container provide user to put together there data, type of data maybe int, flot or char. For example you have a bookstore. In that bookstore you have many books. Every book have different price, different author, different name and different number of pages. So here we find that
Price is “float”
Author is “string”
Name is “string”
Number of Page is “int”
You might think of using array. But array is more complex than structure. In a single structure we can put all type of data together.
Let’s write a program on structure
#include<stdio.h>
void main()
{
    struct bookshop
    {
        char name[20];
        char author[20];
        float price;
        int pages;
    };
    struct bookshop b1,b2;
    printf("Enter name,author,price and pages for first books?\n");
    scanf("%s %s %f %d",&b1.name,&b1.author,&b1.price,&b1.pages);
    printf("Enter name,author,price and pages for second books?\n");
    scanf("%s %s %f %d",&b2.name,&b2.author,&b2.price,&b2.pages);
    printf("Your entered data is\n");
    printf("%s %s %.2f %d\n",b1.name,b1.author,b1.price,b1.pages);
    printf("%s %s %.2f %d\n",b2.name,b2.author,b2.price,b2.pages);
}
In above program we write a simple “structure” program. First we declare a structure named “bookshop”. Syntax of declaring a “structure” is
struct structure_name
{
data_type variable_name;
data_type variable_name;
.
.
data_type variable_name;
};
There is no limits how many element you want to put inside a structure. Once your structure created then you need to declare some variable for your structure that can access structure element. In this program we use b1 and b2 as a variable. b1 and b2 can have access to all element in structure. In above program we declare structure and structure variable differently. We can declare theme together. Then the syntax became
struct bookshop
    {
        char name[20];
        char author[20];
        float price;
        int pages;
    }b1,b2;
We can set variables data directly in declaration by this syntax
struct bookshop b1={“code”,”Henry”,300,120);
Now you need to access the data from structured variable. For that “b1.name” can access the “name” in structure for b1. Same way you can access “b1.author” for “author” and so on. In this way you can put and retrieve data in structure.
Now let’s see the output for this program
Enter name,author,price and pages for first books?
Mask Json 300 230
Enter name,author,price and pages for second books?
Sky Mike 100 200
Your entered data is
Mask Json 300.00 230
Sky Mike 100.00 200

Friday, 1 August 2014

Use string library functions in c programming

In programming you may need to do some operations on strings. In C programming “string.h” is a hader file which contain many library function by which we can do operations on string. There are so many string library functions. Let’s try some important of  them one by one.
“strlrn”
 We use strlen to find the length of a string.
#include<stdio.h>
#include<string.h>
void main()
{
    char name[]="Programs Page";
    int length=strlen(name);
    printf("Length of string=%d",length);
}
The output of this program is
Length of string 13
“strlwr” and “strupr”
Convert a string to lower case using “strlwr” and convert a string to upper case using “strupr”.
#include<stdio.h>
#include<string.h>
void main()
{
    char name[]="Programs Page\n";
    printf("here is lower case %s\n",strlwr(name));
    printf("here is upper case %s\n",strupr(name));
}
The output of this program is
here is lower case programs page
here is upper case PROGRAMS PAGE
“strcat” and “strncat”
“strcat” is used for append one string at the end of another. “strcncat” do append first n number of character of a string to another string.
Code for “strcat”
#include<stdio.h>
#include<string.h>
void main()
{
    char source[]="page";
    char target[30]="programs";
    strcat(target,source);
    printf("source = %s\n",source);
    printf("target = %s\n",target);
}
Output of this program
source = page
target = programspage
Code for “strncat”
#include<stdio.h>
#include<string.h>
void main()
{
    char source[]="page";
    char target[30]="programs";
    strncat(target,source,2);
    printf("source = %s\n",source);
    printf("target = %s\n",target);
}
Output of this program
source = page
target = programspa
Note: here first 2 character are added to target string from source string.
“strcpy” and “strncpy”
“strcpy” copies a string into another string and “strncpy” copies first n characters of string into another.
Code for “strcpy”
#include<stdio.h>
#include<string.h>
void main()
{
    char source[]="programspage";
    char target[30];
    strcpy(target,source);
    printf("source = %s\n",source);
    printf("target = %s\n",target);
}
Output of this program is
source = programspage
target = programspage
Code for “strncpy”
#include<stdio.h>
#include<string.h>
void main()
{
    char source[]="programspage";
    char target[30]="addhere";
    strncpy(target,source,8);
    printf("source = %s\n",source);
    printf("target = %s\n",target);
}
Output of this program is
source = programspage
target = programs
Note: if your target string is empty then it will return garbage value
“strcmp” and “strncmp”
Here “strcmp” compare two strings and “strncmp” compare first n number of two string
Code for “strcmp”
#include<stdio.h>
#include<string.h>
void main()
{
    char source[]="programspage";
    char targetX[]="code";
    char targetY[]="programspage";
    int i=strcmp(targetX,source);
    int j=strcmp(targetY,source);
    printf("value of i = %d\n",i);
    printf("value of j = %d\n",j);
}
Output of this program is
value of i = -1
value of j= 0
Note: when two string does not match then it will return -1 and when they match return 0.
Code for “strncmp”
#include<stdio.h>
#include<string.h>
void main()
{
    char source[]="programspage";
    char targetX[]="programs";
    char targetY[]="programs";
    int i=strcmp(targetX,source);
    int j=strncmp(targetY,source,7);
    printf("value of i = %d\n",i);
    printf("value of j = %d\n",j);
}
Output of this program
value of i = -1
value of j= 0
Note: in this program we compare “strcmp” and “strncmp”. here "strcmp" return -1 as not match of full string. but "strncmp" return true because first 7 character of both of them are matched.

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