14

Somme d'une suite de nombres au carré

  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

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[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]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].