Code improved and outputs added