Advertisement
Dani_info

calcul de suma_for

Oct 31st, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.39 KB | None | 0 0
  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
Add Comment
Please, Sign In to add comment
Advertisement