google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: February 2016

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();
}

C Program to find matrix addition, subtraction, multiplication, transpose and symmetric operations in C Programming

//Code for Program to find matrix addition, subtraction, multiplication, //transpose and symmetric operations in C Programming
 # include<stdio.h>
       void display(int [][3]);
       void main()
       {
           int c;
           void func1();
           void func2();
           void func3();
           void func4();
           void func5();
       
             printf("\n- : Matrix Manipulation Functions (for 3 X 3 Matrix) : -");
           printf("\n-------------------------------------");
           printf("\n Matrix Addition            : 1");
           printf("\n Matrix Subtraction         : 2");
           printf("\n Matrix Multiplication      : 3");
           printf("\n Find Transpose Matrix      : 4");
           printf("\n Matrix is Symmetric or not : 5");
           printf("\n Enter Your Choice          : ");
           scanf("%d",&c);
           switch(c)
           {
               case 1:
                   func1();
                   break;
               case 2:
                   func2();
                   break;
               case 3:
                   func3();
                   break;
               case 4:
                   func4();
                   break;
               case 5:
                   func5();
                   break;
               default:
                   printf("\nInvalid Choice");
           }
      
       }




       void func1()
       {
           int x[3][3],y[3][3],z[3][3];
           void getmatrix(int [][3]);
           void addition(int [][3],int [][3],int [][3]);
  
           getmatrix(x);
           getmatrix(y);
           addition(x,y,z);
           printf("\n - : Matrix 1: - \n");
           display(x);
           printf("\n - : Matrix 2: - \n");
           display(y);
           printf("\n - : Matrix Addition (Result): - \n");
           display(z);
       }
       void getmatrix(int t[][3])
       {
           int i,j;
           for(i=0;i<3;i++)
           {
               for(j=0;j<3;j++)
               {
                   printf("Enter element [%d][%d] : ",i,j);
                   scanf("%d",&t[i][j]);
               }
           }
       }
       void addition(int p[][3],int q[][3],int r[][3])
       {     int i,j;
           for(i=0;i<3;i++)
           {      for(j=0;j<3;j++)
                   r[i][j]=p[i][j]+q[i][j];
           }
       }
       void func2()
       {
           int x[3][3],y[3][3],z[3][3];
           void getmatrix(int [][3]);
           void subtraction(int [][3],int [][3],int [][3]);
       
           getmatrix(x);
           getmatrix(y);
           subtraction(x,y,z);
           printf("\n - : Matrix 1: - \n");
           display(x);
           printf("\n - : Matrix 2: - \n");
           display(y);
           printf("\n - : Matrix Subtraction (Result): - \n");
           display(z);
       }
       void subtraction(int p[3][3],int q[3][3],int r[3][3])
       {
           int i,j;
           for(i=0;i<3;i++)
           {
               for(j=0;j<3;j++)
                   r[i][j]=p[i][j]-q[i][j];
           }
       }
       void func3()
       {
           int x[3][3],y[3][3],z[3][3];
           void getmatrix(int [][3]);
           void multiplication(int [][3],int [][3],int [][3]);
     
           getmatrix(x);
           getmatrix(y);
           multiplication(x,y,z);
           printf("\n - : Matrix 1: - \n");
           display(x);
           printf("\n - : Matrix 2: - \n");
           display(y);
           printf("\n - : Matrix Multiplication (Result): - \n");
           display(z);
       }
       void multiplication(int p[][3],int q[3][3],int r[3][3])
       {
           int i,j,k;
           for(i=0;i<3;i++)                
//condition i< total row of matrix1
           {
               for(j=0;j<3;j++)        
//condition i< total col of matrix1 or//condition i< total row of matrix2
{
                   r[i][j]=0;
                   for(k=0;k<3;k++) //condition i< total col of matrix2
                       r[i][j]=r[i][j]+(p[i][j]*q[j][k]);
               }
           }
       }
       void func4()
       {
           int x[3][3],y[3][3];
           void getmatrix(int [][3]);
           void transpose(int [][3],int [][3]);
     
           getmatrix(x);
           transpose(x,y);
           printf("\n - : Matrix 1: - \n");
           display(x);
           printf("\n - : Transpose Matrix : - \n");
           display(y);
       }
       void transpose(int p[][3],int q[][3])
       {
           int i,j;
           for(i=0;i<3;i++)
           {
               for(j=0;j<3;j++)
                   q[i][j]=p[j][i];
           }
       }
       void func5()
       {
           int x[3][3],y[3][3];
           void getmatrix(int [][3]);
           void transpose(int [][3],int [][3]);
           int symmetric(int [][3],int [][3]);
      
           getmatrix(x);
           transpose(x,y);
           if(symmetric(x,y)==1)
               printf("\nMatrix is Symmetric");
           else
               printf("\nMatrix is Not Symmetric");
       }
       int symmetric(int p[][3],int q[][3])
       {
           int i,j;
           for(i=0;i<3;i++)
           {
               for(j=0;j<3;j++)
               {
                   if(q[i][j]!=p[i][j])
                       return 0;
               }
           }
           return 1;
       }
       void display(int m[][3])
       {
           int i,j;
           printf("\n\n");
           for(i=0;i<3;i++)
           {
               for(j=0;j<3;j++)
                   printf("%d  ",m[i][j]);
               printf("\n");
           }
       }

PROGRAM TO PRINT LOWER TRIANGULAR MATRIX

//PROGRAM TO PRINT LOWER TRIANGULAR MATRIX
#include<stdio.h>

void main()
{
int m,n,i,j,a[10][10];

printf("Enter the order of a square matrix: ");
scanf("%d%d",&m,&n);
if(m==n)
{
printf("Enter %d elements in the matrix:\n",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("You have entered the following matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%4d",a[i][j]);
printf("\n");
}
printf("Lower triangular matrix for the given matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
a[i][j]=0;
printf("%4d",a[i][j]);
}
printf("\n");
}
}
else
printf("Matrix should be a square matrix,So enter a valid order");

}

रघुपति राघव राजाराम पतित पावन सीताराम ॥

 रघुपति राघव राजाराम पतित पावन सीताराम ॥ सुंदर विग्रह मेघश्याम गंगा तुलसी शालग्राम ॥ भद्रगिरीश्वर सीताराम भगत-जनप्रिय सीताराम ॥  जानकीरमणा स...