Skip to content
Snippets Groups Projects
Commit 138f3778 authored by orestis.malaspin's avatar orestis.malaspin
Browse files

added example

parent 8405fb8f
Branches
No related tags found
No related merge requests found
sum_n
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Enter n: "); // affichage chaine de caractères
int n = 0; // déclaration et initialisation de n
scanf("%d", &n); // entrée au clavier
int sum = 0; // déclaration et initialisation de sum
for (int i = 0; i <= n; ++i) { // boucle for
sum += i;
}
printf("The sum of the %d first integers is: %d\n", n, sum); // affichage de n et sum
printf("The analytical formula is %d * (%d + 1) / 2 = %d.\n", n, n, n*(n+1)/2); // on peut mettre n'importe quelle expression
if (sum != n * (n+1) / 2) { // branchement conditionnel
printf("Error: The answer we computed is wrong.\n");
return EXIT_FAILURE; // code d'erreur
}
return EXIT_SUCCESS; // code de réussite
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment