7
Sum of a series of numbers to the square
- #include <stdlib.h>
- #include <stdio.h>
- main() {
- int n, sum = 0;
- int i;
- printf("Enter number of squares to add: ");
- scanf("%d", &n);
- for ( i = 1; i <= n; i++ )
- sum += i*i;
- printf("Sum of the squares is %d\n", sum);
- exit(0);
- }
$ gcc -o sum2 sum2.c
$ ./sum2
Enter number of squares to add: 1
Sum of the squares is 1
$ ./sum2
Enter number of squares to add: 2
Sum of the squares is 5
$ ./sum2
Enter number of squares to add: 3
Sum of the squares is 14
$ ./sum2
Enter number of squares to add: 4
Sum of the squares is 30
$ ./sum2
Enter number of squares to add: 0
Sum of the squares is 0
$ ./sum2
Enter number of squares to add: -1
Sum of the squares is 0
Comments