calculating average

hi. Im new to c programming. i try to write a C program to print the letter-based grade when a number-based grade is entered from the keyboard.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{char c; int exam;int Final;float avr;
printf("input exam=\n");
scanf("%d", &exam);
printf("input final=\n");
scanf("%d", &Final);
avr=(exam*40/100)+(Final*60/100); 
if(100>avr>94)
{
printf("average=%3.2f AA\n ", avr);
}
if(avr==94)
{
printf("average=%3.2f AA\n ", avr);
}
if(94>avr>87)
{
printf("average=%3.2f BA\n ", avr);
}
if(avr==87)
{
printf("average=%3.2f BA\n ", avr);
}
//......             it continues like this 
c=getch(); 
}

but printf doesnt work. I didnt understand it. where s my fail if you tell me.
Ty so much.
Last edited on
Your conditions are wrong. a>b return 1 or 0. You then take that 1 or 0 and compare it to another number. What you need is 100 > avr && avr > 94. The rest seems ok. If you're still having problems, be more specific about what they are.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
main()
{char c; int exam;int Final;float avr;
printf("input exam=\n");
scanf("%d", &exam);
printf("input final=\n");
scanf("%d", &Final);
avr=(exam*40/100)+(Final*60/100); 
if (100>avr&&avr>94); 
{
printf("average=%3.2f AA\n ", avr);
}
if(avr==94)
{
printf("average=%3.2f AA\n ", avr);
}
if(94>avr&&avr>87);
{
printf("average=%3.2f BA\n ", avr);
}
if(avr==87)
{
printf("average=%3.2f BA\n ", avr);
}
//......             it continues like this 
c=getch(); 
}

it works wrongly. when i said exam=88 final=88 it says
avrage=88 AA
avrage=88 BA
avrage=88 BA

it gives all answers.
Last edited on
no comments? i think semi-pro's can make it easily. :S
I would do the avr checking a little differently. Try
1
2
3
4
5
if (avr >=94 && avr <=100)
printf("average =%3.2f AA\n",avr;
if (avr>=87 && avr<94)
printf("average =%3.2f BA\n",avr;
//... it continues like this 


Only one line will print since only one will be true..

EDIT... Also I see you have a semicolon after your first and third if's. Take them off and see if things work properly..
(Re-EDIT: Noticed semicolon also on third if. Changed original EDIT to include it. )
Last edited on
whitenite1 is right. sorry. it was a typo.
Topic archived. No new replies allowed.