Regarding typecasting

These are two type-casting ways for all datatypes , right?

int(variable)

(int) variable

hello @prerak_semwal

mostly we use this only but there are other ways as well.
pls check this->link

@aman212yadav

why there is typecasting used (long long) in the isPrime function (inside loop)

https://drive.google.com/file/d/1FBd_EoCIb5hpW7wwBPb-sv_LXYF4_F5c/view?usp=sharing

https://drive.google.com/file/d/118paW9_HIiIdRc_hIYusMFawkiC0mkYb/view?usp=sharing

https://drive.google.com/file/d/1FBd_EoCIb5hpW7wwBPb-sv_LXYF4_F5c/view?usp=sharing

that is done to avoid overflow.

the product may exceed the integer range hence we need to typecast to long long

@aman212yadav
so can i say, if I have a variable -> int var = 10^6;

and I want to perform - > i * i > num ,
then I should do -> i * (long long) i > num (irrespective of num’s datatype i.e int or long long)

yeah …

@aman212yadav
i made an edit !

if datatype is already long long then no need to write long long exxplicitly

@aman212yadav
so if
int var=10^6; long long num= 10^13;

now I should do: i * i > num
and NOT i * (long long) i > num

right ?

what is datatype of i?

@aman212yadav
sorry…var is i

i is int then obviously u need typecasting

@aman212yadav

then what did you meant by above ??

Please can you share both examples (typecasting used or not) by altering the same code I sent

all i am saying is
if
long long x=something ;

then while computing x*x no need of time casting.

but if it is
int x=something
then while computing x*x use typecasting i.e x *(long long) x

1 Like