#First Ever Program: Feedback desired :)

8 messages · Page 1 of 1 (latest)

zealous pine
#

hi, i wrote Fizzbuzz (could've gone with FizzBuzz to make it to easier, but didn't know if that would have been cheating)
what r ur thoughts?

#include<stdio.h>

int main() {
    int LIMIT = 15;
    int State = 0;
    /*
    0 none, 
    1 factor three: Fizz,  
    2 factor five: Buzz, 
    3 both: Fizzbuzz 
    */ 
    
    for (int i=1; i<=LIMIT; i++) {
        if (i%3==0) {
            State++;
        } 
        if (i%5==0) {
            State = State + 2;
        } 
        
        switch (State) {
            case 0:
                printf("%i\n", i);
                break;
            case 1:
                printf("Fizz\n");
                break;
            case 2: 
                printf("Buzz\n");
                break;
            case 3:
                printf("Fizzbuzz\n");
                break;
        }
        State = 0;
    }
}
sudden vale
#

State should probably be declared in the loop, so it resets itself on every iteration

#

LIMIT should be const int, and probably shouldn't be named in all caps. That's usually only for macros

zealous pine
sudden vale
zealous pine
#
int BiggestKnownProductOfThree = 3;
    int BiggestKnownProductOfFive = 5;
    
    for (int i=1; i<=LIMIT; i++) {
            int State = 0
        if (BiggestKnownProductOfThree+3==i) {
            BiggestKnownProductOfThree = i;
            State++;
        } 
        if (BiggestKnownProductOfFive+5==i) {
            BiggestKnownProductOfFive = i;
            State = State + 2;
        } 

check this out, I tried this instead of the modulo operator and it is evidently way faster, or am I having illusions? :D
@sudden vale