Thursday 11 January 2018

Average in dynamic structure

Q :  Write a program which asks the user to enter the name and age of persons in his group.The number 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.

Solution :


/*==============================
     Girfa Student Help
     Program : Age calculation in dynamic structure
     More Program : http://girfahelp.blogspot.in/p/c-language-structure-programming.html
================================*/
#include<stdio.h>
#include<conio.h>
typedef struct stu
{
     char name[20];
     int age;

}student;
void main()
{
     student pt;
     int sum=0,count=0;
     clrscr();

     while(1)
     {
          printf("\nEnter Age  ");
          scanf("%d",&pt.age);
          if(pt.age==0)
              break;
          sum+=pt.age;
          printf("Enter Name  ");
          fflush(stdin);
          gets(pt.name);
          count++;

     }


     printf("\n\n\tAverage=%d",sum/count);
     getch();

}


No comments:

Post a Comment