Display N Ugly Numbers

//C Program
//Display N Ugly Numbers
#include <stdio.h>

int factor(int number,int checker)
{
  while(number%checker==0)
  {
    number/=checker;
  }
  return number;
}
int is_ugly(int number)
{
  number=factor(number,2);
  number=factor(number,3);
  number=factor(number,5);
  return number;
}
void display_ugly(int size)
{
  
  for (int i = 1; size > 0; ++i)
  {
    if(is_ugly(i)==1)
    {
      printf("%d\n",i );
      size--;
    }
  }
}

int main()
{

  int n = 20;
  
  display_ugly(n);

  return 0;
}

Output

1
2
3
4
5
6
8
9
10
12
15
16
18
20
24
25
27
30
32
36
/*
  C++ Program
  Display N Ugly Numbers
*/
#include <iostream>

using namespace std;

class Number {
public:
  int factor(int number, int checker) {
    while (number % checker == 0) {
      number /= checker;
    }
    return number;
  }
  int is_ugly(int number) {
    number = this->factor(number, 2);
    number = this->factor(number, 3);
    number = this->factor(number, 5);
    return number;
  }
  int display_ugly(int size) {
    for (int i = 1; size > 0; ++i) {
      if (this->is_ugly(i) == 1) {
        cout << " " << i;
        size--;
      }
    }
  }

};

int main() {
  Number obj;
  int n = 20;
  obj.display_ugly(n);
}

Output

 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36
/*
  Java Program
  Display N Ugly Numbers
*/
public class Number {

  public int factor(int number, int checker) {
    while (number % checker == 0) {
      number /= checker;
    }
    return number;
  }
  int is_ugly(int number) {
    number = factor(number, 2);
    number = factor(number, 3);
    number = factor(number, 5);
    return number;
  }
  public void display_ugly(int size) {

    for (int i = 1; size > 0; ++i) {
      if (is_ugly(i) == 1) {
        System.out.println(" "+ i);
        size--;
      }
    }
  }
  public static void main(String[] args) {
    Number obj = new Number();

    int n = 20;

    obj.display_ugly(n);

  }
}

Output

 1
 2
 3
 4
 5
 6
 8
 9
 10
 12
 15
 16
 18
 20
 24
 25
 27
 30
 32
 36
#Python3 Program
#Display N Ugly Numbers
class Number :
  def factor(self, number, checker) :
    while (number % checker == 0) :
      number /= checker
    
    return number
  
  def is_ugly(self, number) :
    number = self.factor(number, 2)
    number = self.factor(number, 3)
    number = self.factor(number, 5)
    return number
  
  def display_ugly(self, size) :
    i = 1
    while (size > 0) :
      if (self.is_ugly(i) == 1) :
        print(" ", i)
        size -= 1

      i+=1
      
    
  
def main() :
  obj = Number()
  n = 20
  obj.display_ugly(n)
  

if __name__ == "__main__":
  main()

Output

  1
  2
  3
  4
  5
  6
  8
  9
  10
  12
  15
  16
  18
  20
  24
  25
  27
  30
  32
  36
/*
  Node Js Program
  Display N Ugly Numbers
*/
class Number {
  factor(number, checker) {
    while (number % checker == 0) {
      number /= checker;
    }
    return number;
  }
  is_ugly(number) {
    number = this.factor(number, 2);
    number = this.factor(number, 3);
    number = this.factor(number, 5);
    return number;
  }
  display_ugly(size) {
    for (var i = 1; size > 0; ++i) {
      if (this.is_ugly(i) == 1) {
        console.log(" " + i);
        size--;
      }
    }
  }
}

function main() {
  var obj = new Number();
  var n = 20;
  obj.display_ugly(n);
}
main();

Output

 1
 2
 3
 4
 5
 6
 8
 9
 10
 12
 15
 16
 18
 20
 24
 25
 27
 30
 32
 36
<?php
/*
  Php Program
  Display N Ugly Numbers
*/
class Number {

  public function factor($number, $checker) {
    while ($number % $checker == 0) {
      $number /= $checker;
    }
    return $number;
  }

  public function is_ugly($number) {
    $number = $this->factor($number, 2);
    $number = $this->factor($number, 3);
    $number = $this->factor($number, 5);
    return $number;
  }
  public function display_ugly($size) {
    for ($i = 1; $size > 0; ++$i) {
      if ($this->is_ugly($i) == 1) {
        echo ("\n". $i);
        $size--;
      }
    }
  }
}
function main() {
  $obj = new Number();
  $n = 20;
  $obj->display_ugly($n);
}
main();

Output

1
2
3
4
5
6
8
9
10
12
15
16
18
20
24
25
27
30
32
36
/*
  C# Program
  Display N Ugly Numbers
*/
using System;
public class Number {

  public int factor(int number, int checker) {
    while (number % checker == 0) {
      number /= checker;
    }
    return number;
  }
  int is_ugly(int number) {
    number = factor(number, 2);
    number = factor(number, 3);
    number = factor(number, 5);
    return number;
  }
  public void display_ugly(int size) {

    for (int i = 1; size > 0; ++i) {
      if (is_ugly(i) == 1) {
        Console.WriteLine(" " + i);
        size--;
      }
    }
  }
  public static void Main(String[] args) {
    Number obj = new Number();

    int n = 20;

    obj.display_ugly(n);

  }
}

Output

 1
 2
 3
 4
 5
 6
 8
 9
 10
 12
 15
 16
 18
 20
 24
 25
 27
 30
 32
 36
#Ruby Program
#Display N Ugly Numbers
class Number 
  def factor(number, checker) 
    while (number % checker == 0) 
      number /= checker
    end
    return number
  end
  def is_ugly(number) 
    number = self.factor(number, 2)
    number = self.factor(number, 3)
    number = self.factor(number, 5)
    return number
  end
  def display_ugly(size) 
    i = 1 
    while (size > 0 ) 
      if (self.is_ugly(i) == 1) 
        print(i,"\n")
        size -= 1
      end
      i+=1
    end
  end
end
def main() 
  obj = Number.new()
  n = 20
  obj.display_ugly(n)
end
main()

Output

1
2
3
4
5
6
8
9
10
12
15
16
18
20
24
25
27
30
32
36
/*
  Swift 4
  Display N Ugly Numbers
*/
class Number {
  func factor(_ number: inout Int, _ checker: Int) {
    while (number % checker == 0) {
      number /= checker;
    }
    
  }
  func is_ugly(_ value: Int) -> Int {
    var number = value;
    self.factor(&number, 2);
    self.factor(&number, 3);
    self.factor(&number, 5);
    return number;
  }
  func display_ugly(_ size: Int) {
    var i: Int = 1;
    var num = size;
    while ( num > 0) {
      if (self.is_ugly(i) == 1) {
        print(" ", i);
        num -= 1;
      }
      i += 1;
    }
  }

}
func main() {
  let obj: Number = Number();
  let n: Int = 20;
  obj.display_ugly(n);
}
main();

Output

  1
  2
  3
  4
  5
  6
  8
  9
  10
  12
  15
  16
  18
  20
  24
  25
  27
  30
  32
  36


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved