-1

I have this code in C, and for some reason instead of properly working with numbers, it outputs zero all the time. Can someone explain me what is going on here? I know C#, but not C.

#include <stdio.h>

int main(void) {
    // I want to express 1/6n*(n + 1)(2n + 1)

    int n = 1;
    while(n != 0){
        scanf("%d", &n);
        printf("%d", 1/6 * n * (n + 1) * (2 * n + 1));
    }

    return 0;
}

Thanks in advance!

I am using Code::Blocks + GCC compiler.

Károly Ozsvárt
  • 1,025
  • 1
  • 12
  • 24

1 Answers1

1

The solution is:

#include <stdio.h>

int main(void) {

    int n = 1;
    while(n != 0){
        scanf("%d", &n);
        printf("%d", n * (n + 1) * (2 * n + 1) / 6);
    }

    return 0;
}
Károly Ozsvárt
  • 1,025
  • 1
  • 12
  • 24