Home > Mobile >  Checking whether space should be printed in the middle or at the end in for loop?
Checking whether space should be printed in the middle or at the end in for loop?

Time:05-07

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

0
1
2               2 
3               3 
4               2 2// I dont wanna print space at the end
5               5 
6               2 3 //but between two nums or more I want space to be added
7               7 
8               2 2 2 
9               3 3 
10              2 5 
11              11 
12              2 2 3 
13              13 

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.

CodePudding user response:

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
$    
  • Related