Saturday 1 November 2014

Practical for Advanced Programming Language ‘C’ (27 to 40) for HNGU bca syllabus hngu

1 comment :
27. Create one text file store some information into it and print the same information on
Terminal.
28. A file named data contains series of integer no. Write a c program to read that no. and then
Write all odd no into file named odd no. and write all even no into file named even no.
Display all the contents of these file on screen.
29. Write a c program to read data from keyboard, write it to a file called input and Display data
of input file on the screen.
30. Write a program that counts the number of characters and number of lines in a file.
31. Write a c program to read mark data which contains roll no, name, sub1, sub2, sub3 file and
Generate the annual examination results are tabulated as follows:
Result
-------------------------------------------------------------------
Roll no Name Sub1 Sub2 Sub3 Total per% Class
--------------------------------------------------------------------
32. Write a c program to input employee no, employee name and basic and to
store output into empdata file in following format.
A/c Department
------------------------------------------------------------------------------------------
Emp-No Name Basic DA HRA MA PF GROSS NET-PAY
------------------------------------------------------------------------------------------
1 xyz 5000 2500 500 100 500 8100 7600
2
3
-------------------------------------------------------------------------------------------
DA = 50% of Basic HRA =10% of Basic
MA = 100 PF = 10% of Basic
GROSS = BASIC + DA + HRA + MA NET-PAY = GROSS – PF
33. Write a c program to read empin data file which contains empno, empname and basic. To
create empout data file as per practical no 23 format.
34. Write a program using fseek and ftell functions.
35. Two files DATA1 and DATA2 contain sorted lists of integers. Write a program to produce a
third file DATA which holds a single sorted, merged list of these two lists. Use command
line arguments to specify the file names.
36. Write a program to work as a dos copy con command using command line argument.
37. Write a C program to work as a dos type command using command line argument.
38. Write a C program to work as a dos copy command using command line argument.
39. Write programs which explain the use of memory allocation functions.
40. Write a program which explains the use of macro.


PRO-27:
#include<stdio.h>
#include<conio.h>
#include<dir.h>
void main()
{              FILE *fp;
                char ch;
                clrscr();
                fp=fopen("student.txt","w");
                printf("\nEnter Data :\n\n");
                while((ch=getchar())!=EOF)
                {              putc(ch,fp);
                }
                fclose(fp);
                fp=fopen("student.txt","r");
                printf("\ncontent of file is :\n\n");
                while((ch=getc(fp))!=EOF)
                {              putc(ch,stdout);
                }
                fclose(fp);
                getch();
}
OUTPUT:
Enter Data :
RIDDHI BCA COLLEGE
MOTIPURA
HIMATNAGAR
^Z
content of file is :
       RIDDHI BCA COLLEGE
       MOTIPURA
       HIMATNAGAR
==================================
PRO-28:
#include<stdio.h>
#include<conio.h>
#include<dir.h>
void main()
{              FILE *fd,*fo,*fe;
                int n,i,no;
                clrscr();
                printf("Enter No.of Interger to be insert\n");
                scanf("%d",&n);
                fd=fopen("data.txt","w");
                printf("\nEnter interger No. :\n");
                for(i=0;i<n;i++)
                {              scanf("%d",&no);
                               

              if(no==-1)
                                                break;
                                else
                                                putw(no,fd);
                }
              fclose(fd);
                fd=fopen("data.txt","r");
                fo=fopen("odd.txt","w");
                fe=fopen("even.txt","w");
                while((no=getw(fd))!=EOF)
                {              if(no%2==0)
                                                putw(no,fe);
                                else
                                                putw(no,fo);
                }
                fclose(fd);
                fclose(fo);
                fclose(fe);
                fd=fopen("data.txt","r");
                fo=fopen("odd.txe","r");
                fe=fopen("even.txt","r");
                printf("\nContent of Data file :");
                while((no=getw(fd))!=EOF)
                {              printf(" %d",no);
                }
                printf("\nContent of Odd file :");
                while((no=getw(fo))!=EOF)
                {              printf(" %d",no);
                }
                printf("\nContent of Even file :");
                while((no=getw(fe))!=EOF)
                {              printf(" %d",no);
                }
fclose(fd);
                fclose(fo);
                fclose(fe);
                getch();
}
OUTPUT:
Enter No.of Interger to be insert
5
Enter interger No. :
1
3
4
2
5
Content of Data file : 1 3 4 2 5
Content of Odd file : 1 3 5
Content of Even file : 4 2
                                           



PRO-29:
#include<stdio.h>
#include<conio.h>
#include<dir.h>
void main()
{              FILE *fi;
                char ch;
                clrscr();
                fi=fopen("input.txt","w");
                printf("Enter Data :");
                while((ch=getchar())!=EOF)
                {              putc(ch,fi);
                }
                fclose(fi);
                fi=fopen("input.txt","r");
                printf("Display of Input File :");
                while((ch=getc(fi))!=EOF)
                {              putc(ch,stdout);
                }
                getch();
}
OUTPUT:
Enter Data :
RIDDHI BCA COLLEGE
MOTIPURA
HIMATNAGAR
^Z
Display of Input File :
----------------------
RIDDHI BCA COLLEGE
MOTIPURA
HIMATNAGAR
PRO-30:
#include<stdio.h>
#include<conio.h>
void main()
{              FILE *fc;
                char ch,c[100];
                int i=0,s=0,l=0,j;
                clrscr();
                fc=fopen("char.txt","w");
                printf("Enter Data :\n");
                while((ch=getchar())!=EOF)
                {              putc(ch,fc);
                                c[i]=ch;
                                i++;
                }
                fclose(fc);
                fc=fopen("char.txt","r");
                for(j=0;j<i;j++)
                {              if(c[j]=='  ')
                                                s++;
                                if(c[j]=='\n')
                                                l++;
                }
                fclose(fc);
                printf("\nCharacters are : %d",i);
                printf("\nSpace are : %d",s);
                printf("\nLine : %d",l);
                getch();
}
OUTPUT:
Enter Data :
RIDDHI BCA COLLEGE
MOTIPURA
HIMATNAGAR
^Z
Characters are : 39
Space are : 2
Line : 3









PRO-31:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{              int roll_no;
                char name[20];
                int sub[3],total,gread;
                float per;
};
void main()
{              FILE *fp;
                struct student s[20];
                int n,i,j;
                char res[20];
                clrscr();
                printf("\nHow many record you want to insert:");
                scanf("%d",&n);
                fp=fopen("PROG-31.txt","w");
                printf("\nEnter data : ");
                printf("\n---------------------");
                for(i=0;i<n;i++)
                {              printf("\n\t enter the student roll no:");
                                scanf("%d",&s[i].roll_no);
                                printf("\n\t enter the student name :");
                                scanf("%s",s[i].name);
                                printf("\n\t enter the student marks");
                                for(j=0;j<3;j++)
                                {              printf("\n\t\t subject %d:",j+1);
                                                scanf("%d",&s[i].sub[j]);
                                }
                                s[i].total=s[i].sub[0]+s[i].sub[1]+s[i].sub[2];
                                s[i].per=s[i].total/3;
                }
                fclose(fp);
                fp=fopen("PROG-31.txt","r");
                printf("\n\t\t Results : ");
printf("\n------------------------------------------------------------");
                printf("\nRollNo.\t Name\t sub1\t sub2\t sub3\t total\t per(%)\t
                               class");
                printf("\n----------------------------------------------------------- ");
                for(i=0;i<n;i++)
                {              if(s[i].per>=70)
                                {              strcpy(res,"dist");
                                }                              else
if(s[i].per>=60&&s[i].per<=69)
                                {              strcpy(res,"first");
                                }
                                else if(s[i].per>=50&&s[i].per<=59)
                                {              strcpy(res,"Second");
                                }
                                else
                                {              strcpy(res,"pass");
                                }
                }
                for(i=0;i<n;i++)
                {              printf("\n%d\t %s\t %d\t %d\t %d\t   
                                        %d\t %.2f\t %s\n", s[i].roll_no,      
                                   s[i]. name, s[i].sub[0], s[i].sub[1],
                                   s[i].sub[2], s[i].total, s[i].per,res);
                 }
      fclose(fp);
      getch();
}
OUTPUT:
How many record you want to insert:2
Enter data :
---------------------
         enter the student roll no:1
         enter the student name :vivek
         enter the student marks
                 subject 1:70
                 subject 2:68
                 subject 3:70
         enter the student roll no:2
         enter the student name :patel
         enter the student marks
                 subject 1:55
                 subject 2:60
                 subject 3:65
                 Results :
------------------------------------------------------------
R_No. Name   sub1   sub2   sub3  total   per(%)  class
-----------------------------------------------------------
1        vivek         70      68      70      208     69.00   first
2        ajay           55      60      65      180     60.00   first

PRO-32:
#include<stdio.h>
#include<conio.h>
#include<dir.h>
  struct emp
{  int no,basic,ma;
  char name[20];
  float da,hra,pf,gross,net;
};
   void main()
    {       struct emp s[20];
            FILE *fp;
            int n,i;
            clrscr();
            printf("\nEnter Employee Data :");
            printf("\n How many Record you want to insert:");
            scanf("%d",&n);
            fp=fopen("empdata.txt","w");
      for(i=0;i<n;i++)
       {       printf("\nenter the employee no:");
                 fscanf(stdin,"%d",&s[i].no);
                 printf("\n\t enter the employee name:");
                 fscanf(stdin,"%s",s[i].name);
                 printf("\n\t enter the basic salary:");
                 fscanf(stdin,"%d",&s[i].basic);
               fprintf(fp,"%d  %s  %d",s[i].no,s[i].name,s[i].basic);
       }
                  fclose(fp);
                  fprintf(stdout,"\n");
                  fp=fopen("empdata","r");
                  printf("\t\t\tA/c Department :\n");
                  printf("------------------------------------------------------------------");
                  printf("\nEmp_no\t Name\t  Basic\t  DA\t HRA\t MA   
                                 PF\tGROSS    NET-PAY");
                  printf("\n----------------------------------------------------------------");
                  for(i=0;i<n;i++)
                  {            fscanf(fp,"%d %s %d",s[i].no,s[i].name,s[i].basic);

                             s[i].da=s[i].basic*0.50;
                                s[i].hra=s[i].basic*0.10;
                                s[i].ma=100;                         
                             s[i].pf=s[i].basic*0.10;
s[i].gross=s[i].basic+s[i].da+s[i].hra+s[i].ma;
                                s[i].net=s[i].gross-s[i].pf;
                                fprintf(stdout,"\n%d\t%s\t  %d\t %.2f\t
                %.2f\t%d   %.2f    %.2f   %.2f",s[i].no,s[i].name,
           s[i].basic, s[i].da, s[i].hra, s[i].ma, s[i].pf, s[i].gross,      
                                   s[i].net);
                     }
                      fclose(fp);
                      getch();
}
OUTPUT:
Enter Employee Data :
 How many Record you want to insert:3
enter the employee no:1
         enter the employee name:xyz
         enter the basic salary:5000
enter the employee no:2
         enter the employee name:abc
         enter the basic salary:7000
enter the employee no:3
         enter the employee name:def
         enter the basic salary:8000
                        A/c Department :
----------------------------------------------------------------------
E_no   Name     Basic   DA     HRA     MA    PF       GROSS    NET-PAY
--------------------------------------------------------------------
1       xyz    5000  2500.00 500.00  100   500.00    8100.00     7600.00
2       abc   7000  3500.00 700.00  100   700.00    11300.00   10600.00
3       def    8000  4000.00 800.00  100   800.00   12900.00   12100.00








PRO-33:
#include<stdio.h>
#include<conio.h>
#include<dir.h>
  struct emp
{  int no,basic,ma;
  char name[20];
  float da,hra,pf,gross,net;
};
   void main()
    {       struct emp s[20];
            FILE *fp;
            int n,i;
            clrscr();
            printf("\nEnter Employee Data :");
            printf("\n How many Record you want to 
                        insert:");
            scanf("%d",&n);
            fp=fopen("empdata.txt","w");
      for(i=0;i<n;i++)
       {       printf("\nenter the employee no:");
                 fscanf(stdin,"%d",&s[i].no);
                 printf("\n\t enter the employee name:");
                 fscanf(stdin,"%s",s[i].name);
                 printf("\n\t enter the basic salary:");
                 fscanf(stdin,"%d",&s[i].basic);
               fprintf(fp,"%d  %s  %d",s[i].no,s[i].name,s[i].basic);
       }
                  fclose(fp);
                  fprintf(stdout,"\n");
                  fp=fopen("empdata","r");
                  printf("\t\t\tA/c Department :\n");
                  printf("----------------------------------------------");
                  printf("\nEmp_no\t Name\t  Basic\t  DA\t
                              HRA\t MA    PF\tGROSS    NET-PAY");
                  printf("\n-------------------------------------------");
                  for(i=0;i<n;i++)
         {     fscanf(fp,"%d %s %d", s[i].no, s[i].name,
                              s[i].basic);
                s[i].da=s[i].basic*0.50;
                s[i].hra=s[i].basic*0.10;
                s[i].ma=100;
                s[i].pf=s[i].basic*0.10;
                s[i].gross=s[i].basic+s[i].da+s[i].hra+s[i].ma;
                s[i].net=s[i].gross-s[i].pf;
               

fprintf(stdout,"\n%d\t%s\t  %d\t %.2f\t
                %.2f\t%d   %.2f    %.2f   %.2f",s[i].no,s[i].name,
           s[i].basic, s[i].da, s[i].hra, s[i].ma, s[i].pf, s[i].gross,      
                                   s[i].net);
                     }    
 fclose(fp);
                      getch();
}
OUTPUT:
Enter Employee Data :
 How many Record you want to insert : 1
enter the employee no : 1
         enter the employee name:xyz
         enter the basic salary:5000
                          A/c Department :
----------------------------------------------------------------------
E_no   Name     Basic   DA     HRA     MA    PF       GROSS    NET-PAY
--------------------------------------------------------------------
1    xyz    5000  2500.00 500.00  100   500.00    8100.00     7600.00



PRO-34:
#include<stdio.h>
#include<conio.h>
void main()
{ FILE *fp;
 long n;
 char c;
 clrscr();
 fp=fopen("RANDOM","w");
 printf("Enter Data :\n");
 while((c=getchar())!=EOF)
 {             putc(c,fp);
 }
 printf("No.of character entered = %ld\n",ftell(fp));
 fclose(fp);
 fp=fopen("random","r");
 n=0L;
 while(feof(fp)==0)
  {     fseek(fp,n,0);
     printf("position of %c is %ld\n",getc(fp),ftell(fp));
                n=n+1L;
  }
      putchar('\n');
      fseek(fp,-1L,2);
      printf("Reverse Order is :\n");
      do
                {   putchar(getc(fp));
                }while(!fseek(fp,-2L,1));
                 fclose(fp);
       getch();
}
OUTPUT:
Enter Data :
college^Z
No.of character entered = 7
position of c is 0
position of o is 1
position of l is 2
position of l is 3
position of e is 4
position of g is 5
position of e is 6
position of   is 7
Reverse Order is :
         egelloc
PRO-36:
#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{  FILE *fp;
  char ch;
  clrscr();
  if(argc!=2)
   { printf("\n required parameter massing..");
      printf("\n Help: file1<file-name> \n");
      exit(0);
   }
                fp=fopen(argv[1],"W");
                if(fp==NULL)
                 { printf("\n file not found- %s \n",argv[1]);
                   exit(1);
                 }
                  while ((ch=getchar())!=EOF)
                   {    putc(ch,fp);
                   }
                     printf("\n\n %s is created",argv[1]);
                     fclose(fp);
       getch();
}
OUTPUT:

C\TC\BIN\lab_36 ABC.TXT

Riddhi bca college

Abc.txt is created




PRO -35:
#include<stdio.h>
#include<conio.h>
void main()
{   FILE *f1,*f2,*f3;
   int number,i,j,k,temp,sort[40];
   clrscr();
   printf("  file DATA-1\n");
   printf(" enter-1 for stop for add number into file:- \n");
   printf("enter the number :-");
   f1=fopen("data1.txt","w+t");
   for(i=1;i<30;i++)
    {      scanf("%d",&number);
      if(number==-1)break;
      putw(number,f1);
    }
                fclose(f1);
                printf("\n file DATA-2 :");
                printf("\n enter -1 for stop for add 
                              number into file:-\n\n");
                printf("\n enter the number:-");
                f2=fopen("data2.txt","w+t");
                for(i=1;i<=30;i++)
                 {    scanf("%d",&number);
                    if(number==-1)break;
                    putw(number,f2);
                 }
                   fclose(f2);
                     printf("\nMerjed data of file DATA-
                                 1 and DATA-2 file :");
                     f1=fopen("data1.txt","r+t");
                     f3=fopen("data3.txt","w+t");
                      while((number=getw(f1))!=EOF)
                       {  putw(number,f3);
                       }
                                   fclose(f1);
                                   f2=fopen("data2.txt","r+t");
while((number=getw(f2)) !=EOF)
                                    {      putw(number,f3);
                                    }
                                      fclose(f2);
                                      fclose(f3);
                      f3=fopen("data3.txt","r+t");
                      i=0;
                      while((number=getw(f3)) !=EOF)
                                       {       sort[i]=number;
                                                 i++;
                                       }
                                                fclose(f3);
                                f3=fopen("data3.txt","w+t");
               

                                for(j=0;j<i;j++)
                                                 {     for(k=j+1;k<i;k++)
                                                                {    if(sort[j]>sort[k])
                                                                      {temp=sort[j];
                                                                        sort[j]=sort[k];
                                                                         sort[k]=temp;
                                            }        }
                                                 putw(sort[j],f3);
                                                }
                                                 fclose(f3);
                                                 f3=fopen("data3.txt","r+t");
                                 while((number=getw(f3)) !=EOF)
                                 {      printf("\n %d",number);
                                 }
                                 fclose(f3);
                                getch();
}
OUTPUT:-
  file DATA-1
 enter-1 for stop for add number into file:-
enter the number :-
10
5
4
15
-1
 file DATA-2 :
 enter -1 for stop for add number into file:-
 enter the number:-
12
3
15
8
-1
Merjed data of file DATA-1 and DATA-2 file :
 3
 4
 5
 8
 10
 12
 15
 15

PRO-37:
#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
 {   FILE *fp;
     char ch;
     clrscr();
     if(argc!=2)
       {       printf("\n required parameter massing..");
                 printf("\n help:file1<file-name \n");
                 exit(1);
       }
                   fp=fopen(argv[1],"r");
                   if(fp==NULL)
                     { printf("\n file not found- %s \n",argv[1]);
                        exit(1)
}
while((ch=getc(fp))!=EOF)
                  printf("%c",ch);
fclose(fp);
 getch();
}
OUTPUT:
c\tc\bin> LAB_37 abc.txt

PRO-38:
#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{    FILE *fs,*ft;
     char ch;
     clrscr();
       if(argc !=3)
       {         printf("\n required parameter massing..");
                   printf("\n help : file1<file-name \n");
                   exit(1);
       }
                     fs=fopen(argv[1],"r");
                     if(fs==NULL)
                    {     printf("\n file not found-%s \n",argv[1]);
                          exit(1);
                       }
                  ft=fopen(argv[2],"w");
                while((ch=getc(fs)) !=EOF)
                 {
                       putc(ch,ft);
                 }
                  printf("\n 1 file(s) copied");
                 fclose (fs);
                 fclose(ft);
     getch();
}
OUTPUT:
C\tc\bin> ALB_38 abc.txt xyz.txt
1 file is copied
PRO-40:
#include<stdio.h>
#include<conio.h>
#define CUBE(a) a*a*a
void main()
{  int no;
    clrscr();
    printf("enter no:");
    scanf("%d",&no);
    CUBE(no);
    printf("CUBE=%d",CUBE(no));
    getch();
}
OUTPUT:-
enter no:5
CUBE=125
PRO-39:
#include<stdio.h>
#include<conio.h>
void main()
{  int *p,no;
  clrscr();
  p=(int *)malloc(sizeof(int));
  if(p == 0)
     {            printf("error:out of memory \n");
                   return 1;
      }
                printf("\n\n enter any no:");
                  scanf("%d", &no);
                  *p=no;
                  printf("the value=%d \n",*p);
                  free(p);
                  getch();
}
OUTPUT:-
Enter any no. : 20
The value  :  20











1 comment :

  1. Eighty percent of available jobs are never advertised, and over half of all employees get their jobs. The ranks indicated inclusive of the ranks allotted in respect of Vacancies arose at the last round. Engineering Mock Counseling gives a chance to students select a good college.

    ReplyDelete