Home > Software engineering >  Reverse the output of the numbers without touching the for loop
Reverse the output of the numbers without touching the for loop

Time:09-30

How to print out the numbers and words from 100 -> 0 rather than 9 -> without touching the for loop? Making another variable is allowed

#include <stdlib.h>
#include <stdio.h>

int main ()
{
    int     idx;
    

    for (idx=0; idx<=100; idx  )        // Do not alter this line
    {
        // Add your code within this curly braces
        
        if((idx%3) == 0)
            printf("Fizz\n");
        else if((idx%5) == 0)
            printf("Buzz\n");
        else if((idx%3 && idx%5) == 0)
            printf("FizzBuzz\n");
        else
            printf("%d\n", idx);
       
    }
}

CodePudding user response:

Idk why you would want to do this but just do 100-idx

#include <stdlib.h>
#include <stdio.h>

int main ()
{
    int     idx;
    

    for (idx=0; idx<=100; idx  )        // Do not alter this line
    {
        // Add your code within this curly braces
        
        if(((100-idx)%3) == 0)
            printf("Fizz\n");
        else if(((100-idx)%5) == 0)
            printf("Buzz\n");
        else if(((100-idx)%3 && idx%5) == 0)
            printf("FizzBuzz\n");
        else
            printf("%d\n", (100-idx));
       
    }
}

CodePudding user response:

The weird exercise.

#include <stdio.h>
int main ()
{
    int idx;
    for (idx=0; idx<=100; idx  )        
    {
        int temp = idx;
        idx = 100 - idx;
        
        if((idx%3) == 0)
            printf("Fizz\n");
        else if((idx%5) == 0)
            printf("Buzz\n");
        else if((idx%3 && idx%5) == 0)
            printf("FizzBuzz\n");
        else
            printf("%d\n", idx);
       
       idx = temp;
    }
}
  •  Tags:  
  • c
  • Related