Thursday, 23 May 2013

Credit inquiry program, which tell about bank balance of customer and diiferent functions


/* Fig. 11.8: fig11_08.c
  Credit inquiry program */
#include <stdio.h>

/* function main begins program execution */
int main( void )
{
  int request;     /* request number */
  int account;     /* account number */
  double balance;  /* account balance */
  char name[ 30 ]; /* account name */
  FILE *cfPtr;     /* clients.dat file pointer */

  /* fopen opens the file; exits program if file cannot be opened */
  if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {
     printf( "File could not be opened\n" );
  } /* end if */
  else {
   
     /* display request options */
     printf( "Enter request\n"
        " 1 - List accounts with zero balances\n"
        " 2 - List accounts with credit balances\n"
        " 3 - List accounts with debit balances\n"
        " 4 - End of run\n? " );
     scanf( "%d", &request );
     /* process user's request */
     while ( request != 4 ) {

        /* read account, name and balance from file */      
        fscanf( cfPtr, "%d%s%lf", &account, name, &balance );

        switch ( request ) {

           case 1:
              printf( "\nAccounts with zero balances:\n" );

              /* read file contents (until eof) */
              while ( !feof( cfPtr ) ) {

                 if ( balance == 0 ) {
                    printf( "%-10d%-13s%7.2f\n",
                       account, name, balance );
                 } /* end if */

                 /* read account, name and balance from file */
                 fscanf( cfPtr, "%d%s%lf",                    
                    &account, name, &balance );              
              } /* end while */

              break;
              printf( "\nAccounts with credit balances:\n" );

              /* read file contents (until eof) */
              while ( !feof( cfPtr ) ) {

                 if ( balance < 0 ) {
                    printf( "%-10d%-13s%7.2f\n",
                       account, name, balance );
                 } /* end if */

                 /* read account, name and balance from file */
                 fscanf( cfPtr, "%d%s%lf",                    
                    &account, name, &balance );              
              } /* end while */

              break;

           case 3:
              printf( "\nAccounts with debit balances:\n" );

              /* read file contents (until eof) */
              while ( !feof( cfPtr ) ) {

                 if ( balance > 0 ) {
                    printf( "%-10d%-13s%7.2f\n",
                       account, name, balance );
                 } /* end if */
                 /* read account, name and balance from file */
                 fscanf( cfPtr, "%d%s%lf",                    
                    &account, name, &balance );              
              } /* end while */

              break;          
         
        } /* end switch */

        rewind( cfPtr ); /* return cfPtr to beginning of file */

        printf( "\n? " );
        scanf( "%d", &request );
     } /* end while */

     printf( "End of run.\n" );
     fclose( cfPtr ); /* fclose closes the file */
  } /* end else */

  return 0; /* indicates successful termination */

 } /* end main */

Sunday, 12 May 2013

Wrrite a program which event become earlier


You are to write a program to process the timings of events. The program should have following structures.

Date is defined as a structure with three fields of type integer i.e. day, month and year.
Time is defined as a structure with three fields of type integer i.e. hour, minute and second.
Event is also defined as a structure with three fields. One of the fields is venue of type integer. Whereas other two fields are the variables of type DateType and TimeType. 







#include<stdio.h>
#include<stdlib.h>
struct time
{
int hour;
int min;
int sec;
};
struct date
{
int day;
int month;
int year;
};
struct even
{
int venue;
struct time t;
struct date d;
};
void inputtime(struct time *t);
void inputdate(struct date *d);
void inputeven( struct even *e1);
int Earlierevevt(struct even *e1 , struct even *e2);
void print(even *e1);
void main()
{
struct even s[10] , temp;
int i,j;
for(i=0;i<3;i++)
{
inputeven(&s[i]);
}
for(i=0;i<2;i++)
{
for(j=i+1;j<3;j++)
if(Earlierevevt(&s[i],&s[j])==1)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
system("cls");
for(i=0;i<3;i++)
print(&s[i]);
system("pause");
}
void inputtime(struct time *t)
{
printf("enter hour\n");
scanf("%d",&t->hour);
printf("enter min\n");
scanf("%d",&t->min);
printf("enter second\n");
scanf("%d",&t->sec);
}
void inputdate(struct date *d)
{
printf("enter date\n");
scanf("%d",&d->day);
printf("enter month\n");
scanf("%d",&d->month);
printf("enter year\n");
scanf("%d",&d->year);
}
void inputeven( struct even *e1)
{
printf("enter the venue of event\n");
scanf("%d",&e1->venue);
printf("enter the date of event\n");
inputdate(&(e1->d));
printf("enter the time of event\n");
inputtime(&(e1->t));
}
int Earlierevevt(struct even *e1 , struct even *e2)
{
if(e1->d.year != e2->d.year)
{
if(e1->d.year > e2->d.year)
return 1;
else{}
}
if(e1->d.month != e2->d.month)
{
if(e1->d.month > e2->d.month)
return 1;
else{}
}
if(e1->d.day != e2->d.day)
{
if(e1->d.day > e2->d.day)
return 1;
else{}
}
if(e1->t.hour != e2->t.hour)
{
if(e1->t.hour > e2->t.hour)
return 1;
else{}
}
if(e1->t.min != e2->t.min)
{
if(e1->t.min > e2->t.min)
return 1;
else{}
}
if(e1->t.sec != e2->t.sec)
{
if(e1->t.sec > e2->t.sec)
return 1;
else{}
}
return 0;
}
void print(even *e1)
{
printf("Venue %d\n",e1->venue);
printf("Date= %d/%d/%d\n",e1->d.day,e1->d.month,e1->d.year);
printf("Time= %d:%d:%d\n",e1->t.hour,e1->t.min,e1->t.sec);
}


Add time t1+t2 in c code


time AddTime(time t1, time t2)
this function takes two variables of type time and add those times and return the resultant time.
e.g if t1 = 23:45:50 and t2 = 04:20: 25 then resultant time should be 04:06:15. Note that seconds and minutes are rounded to 60 whereas hour is rounded to 24.






#include<stdio.h>
#include<stdlib.h>

struct time {
    int hour;
    int minute;
    int sec;
};
struct time t1,t2;
struct time add_time(struct time t1,struct time t2){
    struct time add;

    add.hour   = t1.hour + t2.hour;
    add.sec    = t1.sec + t2.sec;
    add.minute = t1.minute + t2.minute;
    if(add.hour>23)
    {
        add.hour=add.hour-24;
    }
     if(add.minute>59)
    {
        add.minute=add.minute-60;
    }
     if(add.sec>59)
    {
        add.sec=add.sec-60;
    }
    return add;
}
void main()
{
    struct time sum;
    printf("enter the the time 1:\n");
    printf("Enter hours, minutes and seconds respectively:\n ");
    scanf("%d%d%d",&t1.hour,&t1.minute,&t1.sec);
    printf("enter the the time 2\n\n");
    printf("Enter hours, minutes and seconds respectively:\n ");
    scanf("%d%d%d",&t2.hour,&t2.minute,&t2.sec);
    sum=add_time(t1,t2);
    printf("t1+t2  =   %d : %d : %d\n\n\n",sum.hour,sum.minute,sum.sec);
system("pause");
}


Saturday, 4 May 2013

Add complex numbers,suntract complex numbers and multiply complex numbers and return the resultant complex number


A complex number is represented by two parts, real and imaginary part. Consider the complex number ‘ 
a + bi’ where ‘a’ is the real part and ‘b’ is the imaginary part. Also  i = 1 Consider the following struct:




#include<stdio.h>
//#include<math.h>
struct comp {
    float real;
    float img;
};
struct comp a1,a2;
struct comp sum_complex(struct comp a1,struct comp a2)
{
    struct comp sum;
    sum.real=a1.real+a2.real;
    sum.img=a1.img+a2.img;

return sum;
}
struct comp a1,a2;
struct comp multiply_complex(struct comp a1,struct comp a2)
{
    struct comp multiply;
    multiply.real=a1.real*a2.real;
    multiply.img=a1.img*a2.img;

return multiply;
}
struct comp a1,a2;
struct comp subtraction_complex(struct comp a1,struct comp a2)
{
    struct comp subtraction;
    subtraction.real=a1.real - a2.real;
    subtraction.img=a1.img - a2.img;

return subtraction;
}

void main(){
    struct comp add;
    struct comp multi;
    struct comp sub;
    printf("enter the complex no 1\n");
    scanf("%f",&a1.real);
    scanf("%f",&a1.img);
    printf("enter the complex no 2\n");
    scanf("%f",&a2.real);
    scanf("%f",&a2.img);
    add = sum_complex(a1,a2);
    sub =subtraction_complex(a1,a2);
    multi= multiply_complex(a1,a2);

    printf("The sum is            =   (%.2f) +(%.2f)i\n\n\n",add.real,add.img);
    printf("The subtraction  is   =   (%.2f) +(%.2f)i\n\n\n",sub.real,sub.img);
    printf("The multiplication is =   (%.2f) + (%.2f)i\n\n\n",multi.real,multi.img);

}





Input
This function should take a pointer of type complex as parameter and take input for real and imaginary parts of the complex number from the user.

Add
This function takes two parameters of type complex, perform complex number addition and return the resultant complex number. If we have two complex numbers (a+bi) and (c+di) then
(a+ib) + (c+id) = (a+c) + (b+d)i
e.g (2 + (-6)i) + (-4+2i) = -2 – 4i

            Subtract
This function takes two parameters of type complex, perform complex number subtraction and return the resultant complex number. If we have two complex numbers (a+ib) and (c+id) then
(a+ib) - (c+id) = (a-c) + (b-d)i
e.g (2 + (-6)i) - (-4+2i) = 6 – 8i

            Multiply
This function takes two parameters of type complex, perform complex number multiplication and return the resultant complex number. If we have two complex numbers (a+ib) and (c+id) then
(a+ib)*(c+id) = (ac-bd) + (bc+ad)i
e.g (2+6i) + (-4+2i) = (-8-12) + (-24+4)i
                                = -20 + (-20)i


Friday, 3 May 2013

Simple Adding two matrixes by for loop in C


//simple adding two matrixes

#include<stdio.h>
void main()
{
int a[3][3],r[3][3],c[3][3],i,j,temp=0;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
        printf("enter the values for A matrics\n");
    scanf("%d",&a[i][j]);
        printf("enter the values for B matrics\n");
    scanf("%d",&r[i][j]);
        }
    }
    printf("\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            printf("%d\t",a[i][j]);
            printf("+\t");
        for(j=0;j<3;j++)
            printf("%d\t",r[i][j]);
    printf("\n");
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
    c[i][j]=a[i][j]+r[i][j];
    }
    printf("sum of above matices\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
    printf("%d\t",c[i][j]);
    printf("\n");
    }



}

Thursday, 2 May 2013

Sort the numbers in ascending order




#include<stdio.h>
void main()
{
    int a[10],i,j,temp;
    printf("Enter the marks of 10 students!!");
    for(i=0;i<10;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<10;i++)
    {
        for(j=0;j<9;j++)
        {
            if(a[i]>a[j]){
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;}
        }

    }
    for(i=0;i<10;i++)
    printf(" %d   ",a[i]);

}