Home > Software design >  How can I display odd numbers 1 to 100 with 5 numbers per line in C?
How can I display odd numbers 1 to 100 with 5 numbers per line in C?

Time:01-30

I'm trying to display odd numbers from 1 to 100 as 5 numbers per line but I couldn't.

int main(void)
{
    int i,j;
    
    for(i=1;i<=5;i  )
    {
        for(j=0;j<100;j  )
        {
            if(j%2==1)
            {
                printf("%d ",j);
            }
        }
    }
    return 0;
}

CodePudding user response:

Your problem is not a C problem. You even should not approach a keyboard until you can make clear in your brain what you have to do.

Let us write it down in plain English:

  • you want to print numbers from 1 to 100
  • you want only odd numbers (so even ones are to be ignored)
  • you want a new line after every fifth number

Now in pseudo code:

loop for i from 0 to 100
    if i % 2 == 0 continue loop
    print number
    increment counter
    if counter == 5
        print newline
        reset counter to 0
    end of if block
end of loop block

For the above algo to work you must just initialize the counter variable to 0 before starting the main loop.

And now it is time to go back to your keyboard and translate that in C language, but it is not the hard part:

#include <stdio.h>

int main() {
    int i, counter = 0;

    for (i = 0; i < 100; i  ) {
        if (i % 2 == 0) continue;
        // as highest number will be 99 "=" would ensure at least 1 space
        // but " -" makes the intention more evident
        printf("% 2d", i);
        if (  counter >= 5) {
            printf("\n");
            counter = 0;
        }
    }
    return 0;
}

Other implementations are possible, this one is just a working one. What you should have learned here: a programmer must be sure of its algo before thinking about how to code it...

CodePudding user response:

Try this (the shortest code)

for(int i=1;i<=100;i =2){
    printf(" -",i);
    if(i==9) printf("\n");
}

CodePudding user response:


int main()
{
    int k=5;
  for(int i=0;i<100;i  ){
      if(i%2!=0){
      printf("%d," ,i) ;
      if(k==1){
      printf ("\n");
        k=6;
       }
       k--;
      }
   }

    return 0;
} 

please have a look

CodePudding user response:

Maybe it's not the best solution, but it's readable for beginners.

int main() {
    int j;
    int k;
    for(j=0;j<100;j  ) {
        if(j%2==1) {   
            k  ;
            printf("%d ",j);
            if (k == 5) {
                printf("\n");
                k = 0;
            }
        }
    }
return 0;

}

or this

int main() {
    int j;
    int k;
    for(j=1;j<100;j =2) {   
            k  ;
            printf("%d ",j);
            if (k == 5) {
                printf("\n");
                k = 0;
            }
    }
return 0;
}

CodePudding user response:

#include <iostream>

int main(){

    // Loop through numbers 1 to 100
    for (int i = 1; i <= 100; i  )
    {
        // Output the current value of i
        std::cout << i << " ";

        // If there is no remainder when dividing by 5...
        if (!(i % 5))
            // ...make a new line
            std::cout << std::endl;
    }

    // Wait until a key is pressed before closing
    std::cin.get();

    return 0;
}
  •  Tags:  
  • Related