updated program with loop