Advertisement
lewapkon

Suma kolejnych liczb

Nov 18th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(){
  6.     long long a,b,w = 0;
  7.  
  8.     cout << "Podaj pierwsza liczbe: ";
  9.     while(!(cin >> a)){
  10.           cin.clear();
  11.           string str;
  12.           cin >> str;
  13.           cout << "Blad: \"" << str << "\" nie jest liczba" << endl;
  14.         }
  15.  
  16.     cout << "\nPodaj druga liczbe: ";
  17.     while(!(cin >> b)){
  18.           cin.clear();
  19.           string str;
  20.           cin >> str;
  21.           cout << "Blad: \"" << str << "\" nie jest liczba" << endl;
  22.         }
  23.  
  24.     //Sprawdzenie czy "a" jest większe niż "b"
  25.     if(a>b){
  26.         long long a0 = a;
  27.         a = b;
  28.         b = a0;
  29.     }
  30.  
  31.     while(a < b+1)w += a++;                                     //Główna operacja programu, czyli sumowanie liczb
  32.  
  33.     cout << "\nSuma liczb z tego zakresu wynosi: " << w << endl;
  34.     system("pause");
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement