hopingsteam

Untitled

Oct 25th, 2020
973
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include    <stdio.h>
  2.  
  3. int main() {
  4.     int nr;
  5.     scanf("%d", &nr);
  6.  
  7.     // Varianta cu For
  8.     int sumaFor = 0;
  9.     for(int i = 1; i <= nr; i++) {
  10.         if(i % 2 == 0) {
  11.             sumaFor = sumaFor - (1 / (i * i));
  12.         } else {
  13.             sumaFor = sumaFor + (1 / (i * i));
  14.         }
  15.     }
  16.  
  17.     // Varianta cu While
  18.     int sumaWhile = 0;
  19.     int i = 1;
  20.     while(i <= nr) {
  21.         if(i % 2 == 0) {
  22.             sumaWhile = sumaWhile - (1 / (i * i));
  23.         } else {
  24.             sumaWhile = sumaWhile + (1 / (i * i));
  25.         }
  26.         i = i + 1;
  27.     }
  28.  
  29.     // Varianta cu While
  30.     int sumaDoWhile = 0;
  31.     i = 1;
  32.     do {
  33.         if(i % 2 == 0) {
  34.             sumaDoWhile = sumaDoWhile - (1 / (i * i));
  35.         } else {
  36.             sumaDoWhile = sumaDoWhile + (1 / (i * i));
  37.         }
  38.         i = i + 1;
  39.     } while (i < nr);
  40.  
  41.     printf("For: %d\nWhile: %d\nDo While: %d", sumaFor, sumaWhile, sumaDoWhile);
  42.     return 0;
  43. }
RAW Paste Data