[ create a new paste ] login | about

Link: http://codepad.org/ilubfEfd    [ raw code | output | fork ]

supriya.k - C, pasted on Sep 30:
#include <stdio.h>
#include <string.h>

int ham_num(int x) {
    int h = x;
    while (h && (h%2 == 0)) 
        h = h/2;

    while (h && (h%3 == 0)) 
        h = h/3;
    
    while (h && (h%5 == 0)) 
        h = h/5;
   /* 
    * h will be 1, when it is ham num, else it will be  
    * something else - example h =10  
    */
   return h;
}

int main(int argc, char *argv[])
{
    int x = 1, cnt = 20;
    printf("Hamming Numbers : \n");
    if (argc > 1)  
        cnt = atoi(argv[1]); 

    while (cnt) {
        if (ham_num(x) == 1) {
            printf("%d ",x);
            cnt--;
        }   
        x++;
    }   
    printf("\n");
}


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

Exited: ExitFailure 10


Create a new paste based on this one


Comments: