C Important Question

Q)Write a program which asks the user to enter the name and age of persons in his group. The numbers of persons is not known to the user in the beginning of the program. The user keeps on entering the data till the user enters the age as zero. The program finally prints the average age.

#include<stdio.h>
#include<conio.h>
typedef struct stu
{
      char name[20];
      int age;
      struct stu * next;

}EMP;
EMP *start=NULL;
void adddata(char*,int);
int average();
void main()
{
      char name[20];
      int age;
      clrscr();
      while(1)
      {
            printf("Enter Age>> ");
            scanf("%d",&age);
            fflush(stdin);
            if(age==0)
                  break;
            printf("Enter name>> ");
            gets(name);
            adddata(name,age);
      }
      printf("\n\tAverage age is %d",average());

}
void adddata(char *nm,int ag)
{
      EMP *nw,*pt;
      nw=(EMP*) malloc(sizeof(EMP));
      strcpy(nw->name,nm);
      nw->age=ag;
      if(start==NULL)
            start=nw;
      else
      {
            for(pt=start;pt->next!=NULL;pt=pt->next)
            {
            }
            pt->next=nw;
      }
}
int average()
{
      EMP *pt;
      int i,total;
      for(i=0,pt=start,total=0;pt!=NULL;pt=pt->next,i++)
            total+=pt->age;
      return(total/i);
}







Q) Write a ‘C’ Program to create a file of numbers and copy odd number into second file and even number into third file.



#include<stdio.h>
#include<conio.h>
void main()
{
     FILE *source,*even_file,*odd_file;
     char ch;
     clrscr();
     source=fopen("test.txt","r");
     even_file=fopen("even.txt","w");
     odd_file=fopen("o
dd.txt","w");
     if(source==NULL)
          printf("\n\tUnable to open source file");
     else if(even_file==NULL)
          printf("\n\tEven file creation error");
     else if(odd_file==NULL)
          printf("\n\tOdd file creation error");
     else
     {
          while(1)
          {
              ch=fgetc(source);
              if(ch==EOF)
                   break;
              if(ch%2==0)
                   fputc(ch,even_file);
              else
                   fputc(ch,odd_file);
          }
     }
     printf("\n\tSource file content\n");
     fseek(source,0,SEEK_SET);
     while(1)
     {
          ch=fgetc(source);
          if(ch==EOF)
              break;
          printf("%c",ch);
     }
     printf("\n\tEven file content\n");
     fclose(source);
     fclose(even_file);
     fclose(odd_file);
     even_file=fopen("even.txt","r");
     odd_file=fopen("odd.txt","r");
     while(1)
     {
          ch=fgetc(even_file);
          if(ch==EOF)
              break;
          printf("%c",ch);
     }
     fclose(even_file);
     printf("\n\tODD file content\n");
     while(1)
     {
          ch=fgetc(odd_file);
          if(ch==EOF)
              break;
          printf("%c",ch);
     }
     fclose(odd_file);
     getch();

}



Q).Write a program to calculate number of vowels (a, e, i, o, u) separately in the entered string.

#include<stdio.h>
#include<conio.h>
void main()
{
     char ch[200];
     int a,e,i,o,u,index;
     a=i=o=u=e=0;
     clrscr();
     printf("Enter your string>> ");
     gets(ch);
     for(index=0;ch[index]!='\0';index++)
     {
           switch(ch[index])
           {
                case 'a' :
                     a++;
                     break;
                case 'A' :

                     a++;
                     break;

                case 'e':
                     e++;
                     break;
                case 'E':
                     e++;
                     break;

                case 'o':
                     o++;
                     break;
                case 'O':
                     o++;
                     break;

                case 'i':
                     i++;
                     break;
                case 'I':
                     i++;
                     break;

                case 'u':
                     u++;
                     break;
                case 'U':
                     u++;
                     break;

           }
     }
     printf("\nVowel frequency\nA=%d\nE=%d\nI=%d\nO=%d\nU=%d",a,e,i,o,u);
     getch();


}



Q)Write a C program to modify the constant variable.

#include<stdio.h>
void main()
{
const int a=1;
int *p;
p=&a;
printf("\n%d",a);
*p=2;
printf("\n%d",a);
}



Q) Write a C program to write a line of string in a text file.

#include<stdio.h>
void main()
{
     FILE *pt;
     char ch[100],c;
     int i;
     pt=fopen("test.txt","w");

     if(pt==NULL)
          puts("File Opening error");
     else
     {
          printf("Enter a line of text>> ");
          gets(ch);
          for(i=0;ch[i]!='\0';i++)
               fputc(ch[i],pt);
     }
     fclose(pt);
}

No comments: