'Checking whether space should be printed in the middle or at the end in for loop?

I am trying to give info to my program whether it should print space or not.

My code looks something like this, and its printing spaces at the end (which is not what I want)

void function(int given_limit)
{
    int current_num = 2;
    while (given_limit > 1)
    {
        if (given_limit % current_num == 0)
        {
            if (current_num == 2)
            {
                printf("%d ", current_num);
            }
            else if (current_num == 3)
            {
                printf("%d ", current_num);
            }
            else if (current_num == 5)
            {
                printf("%d ", current_num);
            }
            else if (current_num == 7)
            {
                printf("%d ", current_num);
            }
            else
            {
                printf("%d ", current_num);
            }

            given_limit /= current_num;
        }
        else
            current_num++;
    }
    printf("\n");
}

In main() I am calling it something like this:

int main()
{
    int given_limit = 13;
    for (int i = 0; i <= given_limit; i++)
    {
        printf("%d\t\t", i);
        function(i);
    }
}

I would appreciate any tips and help. One of the ideas is maybe to store it in an array.



Solution 1:[1]

I replaced spaces with asterisks for better visibility and removed the redundant if-elements. Then I introduced a flag which indicates whether it is the output of the first factor or a later one. In front of each later one we put the space (or asterisk).

#include <stdio.h>
#include <stdbool.h>
void function(int given_limit)
{
    bool is_first_factor = true;

    int current_num = 2;
    while (given_limit > 1)
    {
        if (given_limit % current_num == 0)
        {
            if (is_first_factor) {
                is_first_factor = false; // not first anymore
                // print nothing
            } else {
                printf("*"); // between two factors
            }            
            printf("%d", current_num);

            given_limit /= current_num;
        }
        else
            current_num++;
    }
    printf("\n");
}

int main(int argc, char **argv)
{
    int given_limit = 13;
    for (int i = 0; i <= given_limit; i++)
    {
        printf("%d\t\t", i);
        function(i);
    }
}
$ gcc spacing.c
$ ./a.out      
0
1
2               2
3               3
4               2*2
5               5
6               2*3
7               7
8               2*2*2
9               3*3
10              2*5
11              11
12              2*2*3
13              13
$    

Solution 2:[2]

As mentioned above move the space character to in front of each prime factor and then align the output to take the initial starting space character into account.

This example also skips unnecessary factors.

/* primefactors.c

*/

#include <stdio.h>

void primeFactors(int number)
{
    printf("  %2d   ", number);

    // only test factors <= sqrt(number) 
    // skip even factors > 2
    
    int factor = 2;
    while (factor <= number / factor) {
        if (number % factor == 0) {
            printf(" %d", factor);
            number /= factor;
        }
        else if (factor == 2){
            factor = 3;
        }
        else {
            factor += 2;
        }
    }

    // at this point number equals the greatest prime factor
    printf(" %d\n", number);
}

int main (void)
{
    int max = 45;

    printf("\nnumber  prime factors\n");
    printf("------  -------------\n");

    // skip 0 and 1 which have no prime factors
    printf("  %2d\n", 0);
    printf("  %2d\n", 1);

    for (int i = 2; i <= max; ++i) {
        primeFactors(i);
    }
      
    return 0;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Have Compiler -- Will Travel