Question

In: Computer Science

Using C++, find the sum of the squares of the integers from 1 to MySquare, where...

Using C++, find the sum of the squares of the integers from 1 to MySquare, where MySquare is input by the user. Be sure to check that the user enters a positive integer.

Solutions

Expert Solution

Code with explanation in comments:

#include<iostream>

using namespace std;

int main()
{
//unsigned long variable will let
//user to enter only positive value only.
   unsigned long MySquare,i,sum=0,dummy;
   cout<<"Enter a positive number:";
   cin>>MySquare;
  
   //find the sum of the squares of the +ve integers
   //from 1 to MySquare
   for(i=1;i<=MySquare;++i)
   {
   //store the sqaure of i in dummy variable
   dummy=i*i;
   //store the sum of square of +ve integers from 1 to MySquare in sum variable.
       sum+=dummy;
   }
  
   //print the sum of squares of the +ve integers
   //from 1 to MySquare
   cout<<"\nSum = "<<sum;
  
   return 0;
}

-------------------------------------------------------------------------------------

Snapshot of Code and the output:

Output:

----------------------------------------------------------------------------------------

It will let the user to enter only positive value else it will throw error:


Related Solutions

Find the change of variable that reduce the following quadratic from to a sum of squares...
Find the change of variable that reduce the following quadratic from to a sum of squares and express the quadratic from in terms of the new variables. 8x^2+7y^2+3z^2-12xy-8yz+4zx
1)With​ two-way ANOVA, the total sum of squares is portioned in the sum of squares for​...
1)With​ two-way ANOVA, the total sum of squares is portioned in the sum of squares for​ _______. 2) A​ _______ represents the number of data values assigned to each cell in a​ two-way ANOVA table. a)cell b) Block c)replication D)level 3.) True or false: In a​ two-way ANOVA​ procedure, the results of the hypothesis test for Factor A and Factor B are only reliable when the hypothesis test for the interaction of Factors A and B is statistically insignificant. 4.)Randomized...
The sum of two integers is 54 and their different is 10. Find the integers
The sum of two integers is 54 and their different is 10. Find the integers
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
1) Find the sum of vectors A + B in cartesian component from using the unit...
1) Find the sum of vectors A + B in cartesian component from using the unit vector X and Y http://tinypic.com/r/5v6bdy/8 <- is where the graph 2) then find the sum of the vectors in polar form 3) if A+b+c=0 find C? Note in case the graph isnt showing: In a X and Y planar: Vector A is in the positve quadrant of X and positive of Y, A= 145.0m and the angle= 25.8 Vector B is in the negative...
Statement: For a given integer N, print all the squares of positive integers where the square...
Statement: For a given integer N, print all the squares of positive integers where the square is less than or equal to N, in ascending order. Programming Tasks: Prompt the user to input the value of N Output to the screen all squares of positive integers <= N Tests: Item Test 1 Test 2 Test 3 Inputs: 50 9 100 Outputs: 1 4 9 16 25 36 49 1 4 9 1 4 9 16 25 36 49 64 81...
Write a function sum_int( n ) to compute the sum of the integers from 1 up...
Write a function sum_int( n ) to compute the sum of the integers from 1 up to and including n.
Sum the odd integers between 1 and 99 using a for structure. Assume the integer variables...
Sum the odd integers between 1 and 99 using a for structure. Assume the integer variables sum and count have been declared. Calculate the value of 2.5 raised to the power of 3 using pow method. Print the integers from 1 to 20 using a while loop and the counter variable x. Assume that the variable x has been declared but not initialized. Print only five integers per line.[ Hint: Use calculation x % 5. When the value of this...
Three positive integers (a, b, c) with a<b<c are called a Pythagorean triple if the sum...
Three positive integers (a, b, c) with a<b<c are called a Pythagorean triple if the sum of the square of a and the square of b is equal to the square of c. Write a program that prints all Pythagorean triples (one in a line) with a, b, and c all smaller than 1000, as well the total number of such triples in the end. Arrays are not allowed to appear in your code. Hint: user nested loops (Can you...
Find two positive integers such that the sum of the first number and four times the...
Find two positive integers such that the sum of the first number and four times the second number is 100 and the product of the numbers is as large as possible. please double check answer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT