Tuesday, February 9, 2016

C Program to Create Employee Record and Update it

    /*

     * C Program to Create Employee Record and Update it

     */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define size 200

     struct emp
    {

        int id;
        char *name;
    }*emp1, *emp3;

    
    void display();
    void create();
    void update();

    
    FILE *fp, *fp1;
    int count = 0;
    

    void main(int argc, char **argv)

    {
        int i, n, ch;
   

        printf("1] Create a Record\n");
        printf("2] Display Records\n");
        printf("3] Update Records\n");
        printf("4] Exit");
        while (1)

        {
            printf("\nEnter your choice : ");
            scanf("%d", &ch);
            switch (ch)
            {
            case 1:   

                fp = fopen(argv[1], "a");
                create();
                break;

            case 2:   

                fp1 = fopen(argv[1],"rb");
                display();
                break;

            case 3:   

                fp1 = fopen(argv[1], "r+");
                update();
                break;

            case 4:
                exit(0);

            }

        }

    }

    

    /* To create an employee record */

    void create()

    {
        int i;

        char *p;

        emp1 = (struct emp *)malloc(sizeof(struct emp));
        emp1->name = (char *)malloc((size)*(sizeof(char)));
        printf("Enter name of employee : ");
        scanf(" %[^\n]s", emp1->name);
        printf("Enter emp id : ");
        scanf(" %d", &emp1->id);
        fwrite(&emp1->id, sizeof(emp1->id), 1, fp);
        fwrite(emp1->name, size, 1, fp);
        count++;   // count to number of entries of records
        fclose(fp);
    }

    
    /* Display the records in the file */

    void display()

    {   

        emp3=(struct emp *)malloc(1*sizeof(struct emp));   
        emp3->name=(char *)malloc(size*sizeof(char));
        int i = 1;
  

        if (fp1 == NULL)   
            printf("\nFile not opened for reading");
        while (i <= count)

        {
            fread(&emp3->id, sizeof(emp3->id), 1, fp1);
            fread(emp3->name, size, 1, fp1);
            printf("\n%d %s",emp3->id,emp3->name);
            i++;

        }

        fclose(fp1);
        free(emp3->name);
        free(emp3);

    }
   

    void update()

    {
        int id, flag = 0, i = 1;

        char s[size];

      if (fp1 == NULL)

        {
            printf("File cant be opened");
            return;

        }

        printf("Enter employee id to update : ");
        scanf("%d", &id);
        emp3 = (struct emp *)malloc(1*sizeof(struct emp));
            emp3->name=(char *)malloc(size*sizeof(char));
        while(i<=count)

        {   

            fread(&emp3->id, sizeof(emp3->id), 1, fp1);

            fread(emp3->name,size,1,fp1);

            if (id == emp3->id)

            {

                printf("Enter new name of emplyee to update : ");   

                scanf(" %[^\n]s", s);

                fseek(fp1, -204L, SEEK_CUR);

                fwrite(&emp3->id, sizeof(emp3->id), 1, fp1);

                fwrite(s, size, 1, fp1);

                flag = 1;

                break;

            }

            i++;

        }

        if (flag != 1)

        {

            printf("No employee record found");

            flag = 0;

        }

        fclose(fp1);

        free(emp3->name);        /* to free allocated memory */

        free(emp3);

    }

C Program to sort numbers using counting sort and save the o/p in a file using FILE handling

/*********************************************************************
* Program to sort numbers using counting sort and save
* the o/p in a file using FILE handling
**********************************************************************/
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 500
void selection(int elements[], int maxsize);
int elements[MAXSIZE],maxsize;
void main()
{
int i;
FILE *f;
printf("\nHow many elements you want to sort: ");
scanf("%d",&maxsize);
printf("\nEnter the values one by one: ");
for (i = 0; i < maxsize; i++)
{
printf ("\nEnter element %i :",i);
scanf("%d",&elements[i]);
}
f=fopen("a.txt","w");
if(f==NULL)
{
printf("\nUnable to write in to the file\n");
exit(1);
}
//fprintf(f,"\nArray before sorting:\n");
//for (i = 0; i < maxsize; i++)
//fprintf(f,"[%i], ",elements[i]);
//fprintf (f,"\n");
selection(elements,maxsize);
//fprintf(f,"\nArray after sorting:\n");
fprintf(f,"\nSorted order is:\n");
for (i = 0; i < maxsize; i++)
fprintf(f,"[%i], ", elements[i]);
}


void selection(int elements[], int array_size)
{
int i, j, k;
int min, temp;
for (i = 0; i < maxsize-1; i++)
{
min = i;
for (j = i+1; j < maxsize; j++)
{
if (elements[j] < elements[min])
min = j;
}
temp = elements[i];
elements[i] = elements[min];
elements[min] = temp;
}
}

Saturday, February 6, 2016

C program to Add Two Complex Number

//C program to Add Two Complex Number using structures

#include <stdio.h>
typedef struct complex{
    float real;
    float imag;
}complex;
complex add(complex n1,complex n2);
int main(){
    complex n1,n2,temp;
    printf("For 1st complex number \n");
    printf("Enter real and imaginary respectively:\n");
    scanf("%f%f",&n1.real,&n1.imag);
    printf("\nFor 2nd complex number \n");
    printf("Enter real and imaginary respectively:\n");
    scanf("%f%f",&n2.real,&n2.imag);
    temp=add(n1,n2);
    printf("Sum=%.1f+%.1fi",temp.real,temp.imag);
    return 0;
}
complex add(complex n1,complex n2){
      complex temp;
      temp.real=n1.real+n2.real;
      temp.imag=n1.imag+n2.imag;
      return(temp);
}

Write a C program to write all the members of an array of strcures to a file using fwrite(). Read the array from the file and display on the screen.

//Write a C program to write all the members of an array of strcures to a file //using fwrite(). Read the array from the file and display on the screen.

#include <stdio.h>
struct s
{
char name[50];
int height;
};
int main(){
    struct s a[5],b[5];  
    FILE *fptr;
    int i;
    fptr=fopen("file.txt","wb");
    for(i=0;i<5;++i)
    {
        fflush(stdin);
        printf("Enter name: ");
        gets(a[i].name);
        printf("Enter height: ");
        scanf("%d",&a[i].height);
    }
    fwrite(a,sizeof(a),1,fptr);
    fclose(fptr);
    fptr=fopen("file.txt","rb");
    fread(b,sizeof(b),1,fptr);
    for(i=0;i<5;++i)
    {
        printf("Name: %s\nHeight: %d",b[i].name,b[i].height);
    }
    fclose(fptr);
}

C program to read name and marks of n number of students from user and store them in a file. If the file previously exits, add the information of n students.

//Write a C program to read name and marks of n number of students from //user and store them in a file. If the file previously exits, add the
//information of n students.


#include <stdio.h>
int main(){
   char name[50];
   int marks,i,n;
   printf("Enter number of students: ");
   scanf("%d",&n);
   FILE *fptr;
   fptr=(fopen("C:\\student.txt","a"));
   if(fptr==NULL){
       printf("Error!");
      // exit(1);
   }
   for(i=0;i<n;++i)
   {
      printf("For student%d\nEnter name: ",i+1);
      scanf("%s",name);
      printf("Enter marks: ");
      scanf("%d",&marks);
      fprintf(fptr,"\nName: %s \nMarks=%d \n",name,marks);
   }
   fclose(fptr);
   return 0;
}

Write a C program to read name and marks of n number of students from user and store them in a file.

//Write a C program to read name and marks of n number of students from //user and store them in a file


#include <stdio.h>
int main(){
   char name[50];
   int marks,i,n;
   printf("Enter number of students: ");
   scanf("%d",&n);
   FILE *fptr;
   fptr=(fopen("C:\\student.txt","w"));
   if(fptr==NULL){
       printf("Error!");
       //exit(1);
   }
   for(i=0;i<n;++i)
   {
      printf("For student%d\nEnter name: ",i+1);
      scanf("%s",name);
      printf("Enter marks: ");
      scanf("%d",&marks);
      fprintf(fptr,"\nName: %s \nMarks=%d \n",name,marks);
   }
   fclose(fptr);
   return 0;
}

Write the c program to calculate the sum of element of lower triangle of nXn matrix

//Write the c program to calculate the sum of element of lower triangle of //nXn matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],r,c;
int i=0,j=0,sum=0;
clrscr();
printf("\n enter the r and c");
scanf("%d%d",&r,&c);
printf("\n enter the array\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter the array\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
printf("\n sum of lower triangle\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i>=j)
sum=sum+a[i][j];
}
}
printf("\n sum of lower tringle=%d",sum);
getch();
}

List of bank names in India

 Here is a comprehensive list of banks in India, categorized by type: