Advertisement
Dani_info

calcul de suma_for

Oct 31st, 2017
99
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int n, i, suma=0, p;
  9. cout<<"Acesst program calculeaza suma de tipul:1^2-2^2+4^2-4^2+...+(-1)^(n+1)n^2"<<endl;
  10. cout<<"Introduceti n=";
  11. cin>>n;
  12. for(i=1; i<=n; i++)
  13. { if(i%2==0)
  14. p=i*(-i);
  15. else
  16. p=i*i;
  17.  
  18. suma=suma+p;
  19. }
  20. cout<<"Suma este:"<<suma;
  21. return 0;
  22.  
  23. }
Advertisement
RAW Paste Data Copied
Advertisement