Program 4
Ugly Number
You need to fix the code syntactically and if there are any logical errors fix them too.
Statement : An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.
Given an integer n, return true if n is an ugly number.
Incorrect Code
Correct Code
Incorrect Code
bool isUgly(int n) {
if(n==0)return false;
While n/=2;(n%2==0)
While n/=3;(n%3==0)
whilen/=5;(n%5==0)
return n==1;
}
Correct Code
bool isUgly(int n) {
if(n==0)return false;
while(n%2==0)n/=2;
while(n%3==0)n/=3;
while(n%5==0)n/=5;
return n==1;
}

Login/Signup to comment