I am trying to write a program in C to get the percent of even numbers in an array. I am thinking of writing it using int datatype. But some one mentioned to me using double will be easier. I don't understand that. Can anyone guide me with it?
What does double datatype return? Can the return statement be given as return (double)? What will that give? Can double convert a real number to a percent? Eg: 0.5 to 50.0
The
int
datatype is, as the name would suggest, integers (or whole numbers). So you cannot represent a decimal like0.5
. Adouble
is decimal number. So you can hold numbers like0.5
. Common practice is to store your percentage as a simple decimal number like0.5
(using thedouble
type). Then when you need to display nicely as50.0%
, just multiply by100
before displaying.Here's a useful link on C's basic datatypes: http://www.tutorialspoint.com/ansi_c/c_basic_datatypes.htm