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

printf values inside single bracket

I was just practicing with beginner code printing odds and evens and I could never figure out how to print them inside a simple for loop such that the values stay inside 1 single bracket { } like this:

The·even·number·in·range·are·{2,·4,·6,·8,·10}.

And not like this:

The even number in range are {2} The even number in range are {4}

Edit: oops forgot to place code fragment.

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

for(i=1; i<=n; i++){
    r=i%2;
    if(r==0){
      printf("The even number in range are {%d} ",i);
    }

>Solution :

#include <stdio.h>

int main(void) {
    int n=21;
    printf("The even number in range are {");
    for(int i=1; i<=n; i++){
        (i%2)? 0:printf("%d ", i);
    }
    printf("}\n");
    return 0;
}

Output

Success #stdin #stdout 0s 5436KB
The even number in range are {2 4 6 8 10 12 14 16 18 20 }
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