Is this code correct

is this code correct for approach 2.