12 Jan 2015


WELCOME 

TO Reader . Here this article to how learn C program in cprogrammebook.blogspot.com




About C:

‘c’ seems a name for a programming language. This language is one of the most popular computer language today because it is a structured, high-level, machine independent language. It allow software developer to develop programs knowing about hardware platforms where they will be implemented.At the Bell laboratories in 1972 , C was evolved from ALGOL, BCPL and B by Dennis Ritchie.

Get Start :
If you already install Turbo C (tc) in your computer, it is much better. If you not done it and hesitates how to download or install Turbo C. I tell you  that, its also easy. Just log on: www.turboexplorer.com and download  a version and install it.

10 Jan 2015

Program to accept name , class and marks of 10 students with the use of structure with array of pointer and also print them

#include<stdio.h>
#include<conio.h>
main ( )
{
    struct {
        char name[20];
         int class;
        int marks;
         }*rec[10];
    int i;
    for ( i=0;i< 10;i++ )
    {
        printf ( "Name:" );
        scanf ( " %s", rec[i]->name );
        printf ( "Class:" );
        scanf ( " %d", &rec[i]->class );
        printf ( " Marks:" );
        scanf ( " %d", &rec[i]->marks );
    }
    printf ( "Name\tClass\tMarks\n" );
    for ( i=0 ; i<10 ;i++ )
    {
        printf ( " %s\t" , rec[i]->name );
        printf (" %d\t" , rec[i]->class );
        printf ( " %d\n" , rec[i]->marks );
    }
getch();
}

Program to accept name , class and marks with the use of structure with pointer. Also print the grade which is based on the marks- 1. marks > = 75 grade-A 2. marks >=50 and < 75 grade-B 3. marks < 50 grade-C

#include<stdio.h>
#include<conio.h>

main ( )
{
struct {
         char name[20];
         int class;
        int marks;
                 }*ptr;
    printf ( "Name:" );
    scanf ( " %s", ptr->name );
    printf ( "Class:" );
    scanf ( " %d",&ptr->class );
    printf ( " Marks:" );
    scanf ( " %d", &ptr->marks );
    printf ( "Name\tClass\tMarks\tGrade\n" );
    printf ( " %s\t" , ptr->name );
    printf (" %d\t" , ptr->class );
    printf ( " %d\t" , ptr->marks );
    if ( ptr->marks  < 50 )
        printf ( " F\n" );
    else
    if ( ptr->marks >=50 && ptr->marks < 75 )
        printf ( " B\n" );
    else
        printf ( " A\n" );
getch();
}

program to pass the address of the structure variable

#include<stdio.h>
struct rec{
      char name[10];
      int age;
      int sal;
    };
main ( )
{
    static struct rec data = { "sachin" , 26 , 25000 };
    function ( &data );
}
function ( ptr )
struct rec *ptr;
{
    printf ( "Name:%s\n" ,ptr->name );
    printf ( "Age:%d\n" , ptr->age );
    printf ( "Salary:%d\n" , ptr->sal );
}

Program to assign the value to member of structure with the use of structure pointer

#include<stdio.h>
#include<stdio.h>
main ( )
{
    struct rec{
         char name[10];
          int age;
          int sal;
        }*ptr;
    strcpy(ptr->name, "suresh");
    ptr->age= 24;
    ptr->sal = 5000;
    printf ( "Name:%s\n" , ptr->name );
    printf ( "Age:%d\n" , ptr->age );
    printf ( "Salary:%d ", ptr->sal );
}

Program to understand pointer to structure

#include<stdio.h>
main ( )
{
    struct rec{
          char name[10];
          int age;
          int sal;
        }*ptr;
    printf ( " Enter the name:" );
    scanf ( "%s" , ptràname );
    printf ( " Enter the age:");
    scanf ( "%d" , &ptràage );
    printf ( " Enter the salary:" );
    scanf ( "%d" , &ptràsal );
    printf ( "Name:%s\n" , ptràname);
    printf ( " Age:%d\n" , ptràage );
    printf ( " Salary:%d ", ptràsal );
}

program to understand the use of realloc function

#include<stdio.h>
#include<alloc.h>
#include<string.h>
main ( )
{
    char *ptr;
    ptr = ( char * ) malloc ( 6 );
    ptr = "ankit";
    printf ("%s is in memory block\n",ptr );
    ptr = ( char * ) realloc ( ptr,8);
    printf ("%s is in memory block\n",ptr );
    strcpy ( ptr , "rishabh" );
    printf ("Now %s is in memory block\n",ptr );
}

program to enter the 5 numbers and print them using malloc( )

#include<stdio.h>
main ( )
{
    int i;
    int *a;
    a = ( int * ) malloc ( 5 * sizeof ( int ) );
    for ( i=0;i<5;i++)
    {
        printf ( " number %d=", i+1);
        scanf ( "%d",( a+i) );
    }
    for ( i =0;i<5;i++)
    printf ( " %d\n",*(a+i) );
}

program to understand the sizeof operator

#include<stdio.h>
main ( )
{
    struct
    {
        char name[10];
        int age;
        float sal;
    }rec;
    int arr[10];
    printf ( " size of structure = %d",sizeof ( rec ) );
    printf ( " size of int = %d",sizeof ( int ) );
    printf ( " size of array = %d",sizeof ( arr ) );
}

program to print the elements of 3-D array

#include<stdio.h>
main ( )
{
int arr[2][3][2] = {
            {
                { 5 , 10 },
                { 6 , 11 },
                { 7 , 12 },
            },
            {
                { 20 , 30 },
                { 21 , 31 },
                { 22 , 32 },
             }
            };
    int i,j,k;
    for ( i=0;i< 2;i++)
    for ( j=0;j < 3 ; j++)
    {
        printf ( " \n");
        for ( k =0;k<2;k++)
            printf ( " %d\t",*(*(*(arr+i)+j)+k));
    }
}

program to accept 10 numbers and sort them with use of pointer

#include<stdio.h>
main ( )
{
    int i,j;
    int arr[10];
    int *a;
    a=arr;
    for ( i=0;i<10;i++)
    {
        printf ( "Enter the number %d : ",i+1);
        scanf ( "%d",&arr[i]);
    }
    printf ( "Before sorting:\n" );
    for ( i=0; i<10 ; i++)
        printf ( " %d    " , arr[i] );
    printf ( "\n" );
    a=arr;

    for ( i=0;i<10;i++)
       for ( j=0;j<10-i-1;j++)
        if ( *(arr+j) > *(arr+j+1))
            swap(arr+j , arr+j+1);

    printf ( " After sorting :\n" );

    for ( i =0 ; i < 10 ; i++)
        printf ( "%d  ",arr[i] );
    printf ( "\n" );
}
swap ( int *b , int *c )
{
    int temp;
    temp = *b;
    *b = *c;
    *c = temp;
}

program to accept 10 numbers and print the total with use of pointer

#include<stdio.h>
main ( )
{
    int i,total;
    int arr[10];
    int *a;
    a = arr;
    for ( i=0;i<10;i++)
    {
        printf ( " Enter the number %d : ",i+1);
        scanf ( "%d",&arr[i]);
    }
    for ( i=0;i < 10 ; i++)
    {
        printf ( "%d---",*a);
        total = total + *a;
        a=a+1;
    }
    printf ( "\n Total = %d\n",total);
}

Use pointer to print the value and address of array elements

#include<stdio.h>
main ( )
{
    int arr[4] = { 5 , 10 , 15 , 20 };
    int i = 0;
    int *b;
    b = arr;    /*we can also write b = &arr[0] */
    for ( i = 0;i<=4;i++)
    {
        printf ( "value of arr[%d]=%d\n",i,*b);
        printf ( "address of arr[%d]=%u\n",i,b);
        b=b+1;
    }
}

program to interchange the value of variables from call by reference

#include<stdio.h>
main ( )
{
    int a = 5;
    int b = 8;
    printf ( " Before swapping  a = %d , b = %d\n",a,b);
    swap ( &a,&b);
    printf ( "After calling swap function  a = %d , b = %d\n",a,b);
}
swap ( p , q)
int *p,*q;
{
    int temp;
    temp = *p;
    *p = *q;
    *q = temp;
    printf ( " In swapping function  p = %d , q = %d\n",*p,*q);
}

Use the pointer with function to print the value and address of the array element

#include< stdio.h>
main ( )
{
    int arr[4]={ 5 , 10 , 15 , 20 };
    fun ( arr);
}

fun ( int *a)
{
    int i ;
    for ( i = 0; i <=4;i++)
    {
        printf ( " value of arr[%d]=%d\n",i,*a);
        printf ( "address of arr[%d]=%u\n",i,a);
        a=a+1;
    }
}

Program for binary search

#include <stdio.h>
    #include <conio.h>
    void main()
    {
    int arr[20],start,end,middle,n,i,item;

    printf("How many elements you want to enter in the array : ");
    scanf("%d",&n);
    for(i=0; i < n; i++)
    {
        printf("Enter element %d : ",i+1);
        scanf("%d",&arr[i]);
    }
    printf("Enter the element to be searched : ");
    scanf("%d",&item);
    start=0;
    end=n-1;
    middle=(start+end)/2;
    while(item != arr[middle] && start <= end)
    {
        if(item > arr[middle])
            start=middle+1;
        else
            end=middle-1;
        middle=(start+end)/2;
    }
    if(item==arr[middle])
        printf("%d found at position %d\n",item,middle+1);
    if(start>end)
        printf("%d not found in array\n",item);
getch();
}      /*End of main()*/

Write a program for sequential searching

# include<stdio.h>
# include<conio.h>

    void main()
    {
    int arr[20],n,i,item;
    printf("How many elements you want to enter in the array : ");
    scanf("%d",&n);

    for(i=0; i < n;i++)
    {
        printf("Enter element %d : ",i+1);
        scanf("%d", &arr[i]);
    }
    printf("Enter the element to be searched : ");
    scanf("%d",&item);
    for(i=0;i < n;i++)
    {
        if(item == arr[i])
        {
            printf("%d found at position %d\n",item,i+1);
            break;
        }
    }    /*End of for*/
         if(i == n)
        printf("Item %d not found in array\n",item);
     getch();

}

PROGRAM FOR SORTING A GROUP OF NUMBERS IN ASCENDING ORDER



#include<stdio.h>
#include<conio.h>
void main()
{
  clrscr();
  int a[25],i,n,j,temp;
  printf("Enter the size of array a:-\n");
  scanf("%d",&n);
  printf("Enter element into array a:-\n");
  for(i=0;i<n;i++)
  {
   scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++)
   {
    for(j=0;j<n;j++)
    {
     if(a[j]>a[j+1])
     {
      temp=a[j];
      a[j]=a[j+1];
      a[j+1]=temp;
     }
   }
  }
 printf("The ascending order is :-\n");
  for(i=0;i<n;i++)
  {
   printf("%d\t",a[i]);
  }
 getch();
}

PROGRAM FOR MULTIPLY TWO MATRIX



#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],b[3][3],c[3][3],i,j,k;
printf("enter the element into a:-\n");
 for(i=0;i<3;i++)
 {
 for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
printf("nter element into b:-\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
   {
    scanf("%d",&b[i][j]);
   }
 }
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
 {
 c[i][j]=0;
  for(k=0;k<3;k++)
  {
   c[i][j]=c[i][j]+a[i][k]*b[k][j];
  }
 }
}
printf("the result is \n");
for(i=0;i<3;i++)
{


for(j=0;j<3;j++)

{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}