7

Sum of a series of numbers to the square

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. main() {
  5.     int n, sum = 0;
  6.     int i;
  7.  
  8.     printf("Enter number of squares to add: ");
  9.     scanf("%d", &n);
  10.  
  11.     for ( i = 1; i <= n; i++ )
  12.         sum += i*i;
  13.  
  14.     printf("Sum of the squares is %d\n", sum);
  15.  
  16.     exit(0);
  17. }
$ 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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].