Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*The sum of the squares of the first ten natural numbers is,
- 1^2 + 2^2 + ... + 10^2 = 385
- The square of the sum of the first ten natural numbers is,
- (1 + 2 + ... + 10)^2 = 55^2 = 3025
- Hence the difference between the
- sum of the squares of the first ten natural numbers and
- the square of the sum is
- 3025 - 385 = 2640.
- Find the difference between the sum of the squares of the first one hundred(100) natural numbers
- and the square of the sum.
- 0. int n=100, i, sumOfSq=0, sqOfSum, sum=0, difference;
- 1. for(i=1,i<=n,i++) {sumOfSq+=pow(i,2);}
- 2. for(i=1,i<=n,i++) {sum+=i} sqOfSum= pow(sum,2);
- 3. difference = sqOfSum-sumofSq;
- Start 9:48am -> end 10:18am
- Sat 4/12/2010
- Note: it still have my debug part
- */
- #include <iostream>
- #include <math.h>
- #include <iomanip>
- using namespace std;
- const int n = 100;
- int main()
- {
- int i;
- double sumOfSq=0.0, sum=0.0, sqOfSum, difference;
- cout << setiosflags(ios::fixed);
- cout << "When n = "<< n << endl;
- cout << "Sum of Square of the first "<< n<<" natural numbers[sumOfSq] =";
- for (i=1; i<=n; i++)
- {
- cout << i << "^2";
- sumOfSq += pow(i,2);
- if (i!=n) {cout << "+";}
- }
- cout << " = " << sumOfSq << endl;
- cout << "Sum of the first " <<n<<" natural numbers[sum] =";
- for (i=1; i<=n; i++)
- {
- cout << i;
- sum += i;
- if (i!=n) {cout << "+";}
- }
- cout << " = " << sum << endl;
- cout << "Square of the Sum of the first " << n << " natural numbers[sqOfSum] =";
- sqOfSum = pow(sum,2);
- cout << sqOfSum << endl;
- difference = sqOfSum-sumOfSq;
- cout << "Difference between sqOfSum and sumOfSq = "
- << sqOfSum << " - " << sumOfSq << " = "
- << difference << endl;
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement