Create variables for the following: a) To hold the name of a person b) To hold the bank balance of an account.
Answers
Question:
Create variables for the following: a) To hold the name of a person b) To hold the bank balance of an account..
Solution:
•To hold the name of person.
So first of all let's discuss which datatype is best to store alphabets . There are two datatype which can store alphabets. 1. char 2. String.
But as it is told to store name of person which will contain more than one alphabet, where as datatype char can only store one alphabet , therefore char can't be use to name.
Now let's talk about datatype string. It is a datatype that contains set of characters/alphabets. Therefore string will be best datatype to hold name of a person.
Now Let's create variable to hold the name of a person:
String name = Radha;
Here string is a datatype and name is variable that is holding value Radha.
- To hold bank balance of an account.
Now in this condition as we know we need datatype that holds numeric type of value. We have three datatypes that hold numeric type of values:-
- int
- float
- double
As we know in bank balance there can be decimal types of values where as datatype int only stores integer(only numbers with out any kind of decimals) types of values,therefore int will be wrong choice to choose for declaring a variable that will hold bank balance of accounts.
Now let's talk about datatype float. Although in some extent it will be ok to choose float as datatype for variable to store bank balance. But it can only store 6-7 decimal digits, where as in some cases people have more amount of money, therefore it will too not be good option.
Let's talk about that why doble is best datatype for declaring a variable that will store bank balance. As we know double is a datatype that stores numeric decimal digits and it can store 15-16 decimal digits. That's why it is best option.
Now Let's create a variable to hold the bank balance of an account:
double balance = 234566.778;
Here double is a datatype and balance is variable that is holding value 234566.778.