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 like 0.5
. A double
is decimal number. So you can hold numbers like 0.5
. Common practice is to store your percentage as a simple decimal number like 0.5
(using the double
type). Then when you need to display nicely as 50.0%
, just multiply by 100
before displaying.
Here's a useful link on C's basic datatypes: http://www.tutorialspoint.com/ansi_c/c_basic_datatypes.htm