Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why my ft_print_comb function does not give any output?

#include<stdio.h>   
#include<unistd.h>

void ft_putchar(char x){
    write(1, &x, 1);
}
void ft_print_comb()
{
    char i, j, k;
    i = '0';
    while(i <= 7){
        i++;
        j = i+1;
        while(j <= 8){
            j++;
        
            k = j+1;
            while(k <= 9){
                k++;
                    ft_putchar(i);
                    ft_putchar(j);
                    ft_putchar(k);
                    ft_putchar(',');
                    ft_putchar(' ');
            }
            }
        }
}
int main(){
    ft_print_comb();
    return 0;
}

I have tried to do couple changes but it either broke the code or kept giving me no output. What I am trying to do is create a function that displays all different combinations of three different digits in
ascending order, listed by ascending order. for loop and printf functions are not allowed.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Since you are using chars, you should compare with character literals instead of integers. For instance, the while loop is never entered because the ASCII code for ‘0’ is 48, which is greater than 7.

while (i <= '7') {
    j = i + 1;
    while (j <= '8') {
        k = j + 1;
        while (k <= '9') {
            ft_putchar(i);
            ft_putchar(j);
            ft_putchar(k);
            ft_putchar(',');
            ft_putchar(' ');
            k++;
        }
        j++;
    }
    i++;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading