An interesting question came across my mind: If it is possible to convert a percentage to a Grade(A,B,C,D, or an F). Firstly, there is an easy method to convert a percentage to a Grade; by using an If and Else loop. However, are there also other methods with which this can be achieved? Arrays, shiftin bits, etc... It would be very interesting to see if there are any non-trivial methods to this.

Recommended Answers

All 5 Replies

Multiply 100 times your percentage which should be a decimal number? If you elaborate on the purpose of this people be able to think of more ways. There's also a for loop and while loop. Doing an array would also be possible if you gave a list of numbers.

Switch statements?

They would be the best way to do this, IMO, since you're just considering *one* variable (i.e. the %) and outputting different values (i.e. the grade) depending on that % variable.

If you elaborate on the purpose of this people be able to think of more ways.

The grade boundaries are as follows:
>=95 -->1,0 >=70 -->2,7 < 50 -->Fail (6,0)
>=90 -->1,3 >=65 -->3,0
>=85 -->1,7 >=60 -->3,3
>=80 -->2,0 >=55 -->3,7
>=75 -->2,3 >=50 -->4,0

One possible direction would be to subtract 50 and then divide by 5.

Use the ? : operator.

No, I don't think there are non-trivial ways to do it with a standard grading system, other than the obvious ways which have been listed in this thread already. I don't understand the system you listed, but consider that since 90 and 100 are both usually considered A's, you can't really index an array to get the letter 'A' because what indexes would you use? 9 and 10, after division to get the tens digit, are both A's. So your array would not work without a lot of extra work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.