This program is calculating average in whole number without printing decimal values. It prints the result as a whole number: N.000000. Why?
//Store name, roll no, marks of 4 students using structure
//and calculating average
struct student
{
char name[10];
int roll_no;
int marks;
};
void main()
{
struct student s1,s2,s3,s4;
float avg;
clrscr();
printf("\nEnter name, rollnumber and marks of student 1 : \n");
scanf("%s%d%d",&s1. name,&s1. roll_no,&s1. marks);
printf("\nEnter name, rollnumber and marks of student 2 : \n");
scanf("%s%d%d",&s2. name,&s2. roll_no,&s2. marks);
printf("\nEnter name, rollnumber and marks of student 3 : \n");
scanf("%s%d%d",&s3. name,&s3. roll_no,&s3. marks);
printf("\nEnter name, rollnumber and marks of student 4 : \n");
scanf("%s%d%d",&s4. name,&s4. roll_no,&s4. marks);
avg = (s1. marks + s2. marks + s3. marks + s4. marks) / 4;
printf("\nAverage : %f",avg);
getch();
}
means that each mark is an integer. Then you sum them, that’s also an integer. Then you divide them by 4, that’s also an integer, so this operation will be an integer division – an operation that throws away the fractional part of the result. Divide it by
4.0instead and you’ll be fine to go.