I am trying to show me "average age","minor","retired(60 years old)","older age" and "younger age.
but the program somo shows you the average age and the others are left blank, could you help me solve this problem please.
#include<stdio.h>
int main() {
int edad, totalmenores=0, totaljubilados=0, edadmenor, edadmayor=0, suma = 0, c = 0;
float media;
printf("\nIngrese la edad de la persona: ");
scanf("%d",&edad);
edadmenor = edad;
while (edad > 0) {
suma = suma + edad;
c ++;
if (edad<=17)
totalmenores=totalmenores+1;
if (edad>=60)
totaljubilados=totaljubilados+1;
if (edad > edadmayor)
edadmayor = edad;
if (edad <edadmenor)
edadmenor = edad;
printf("\nIngrese la edad de la persona: ");
scanf("%d",&edad);
}
media = suma / c;
printf("\nProemdio de edades: %.1f" , media);
printf("\nEdad mayor: %.1f" ,edadmayor);
printf("\nEdad menor: %.1f" ,edadmenor);
printf("\nTotal de jubilados: %.1f" ,totaljubilados);
printf("\nTotal de menores de edad: %.1f" ,totalmenores);
return 0;
}
The program does not display a valid number for those variables because they are of type
int
andprintf
the format is of the wrong type (float
).You can change the format to integer (
%d
) or the type of the variables tofloat
:By the way,
media = suma / c
it's integer division because both operands are integers. In order not to lose the decimals, you need at least one of the two to be of typefloat
. For example:Also mention that it
suma += edad
is equivalent tosuma = suma + edad
and more concise.Similarly, you can use
totalmenores++
instead oftotalmenores=totalmenores+1