Problem follows:
Write a program that outputs the squares of the given numbers. The program stops when 0 is entered. Can't use nested loops.
I cannot think of a solution.
#include <iostream>
int main()
{
int number ;
while( std::cout << "number? " && std::cin >> number && number != 0 )
{
// we use long long to avoid a potential signed integer overflow
constlonglong n = number ;
std::cout << n*n << '\n' ;
}
}
Thanks for the reply,
The code you wrote gives the individual squared value of a single entered value, the problem was asking for the sum of the individual squares of multiple inputted numbers. Ex. (Input: 4 3 6 7; Output: 110)
Thanks
You should have been able to make the required modification to the earlier code on your own.
Consider going back and revising what you have learnt so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
int main()
{
longlong sum_of_squares = 0 ;
int number ;
while( std::cout << "number? " && std::cin >> number && number != 0 )
{
// we use long long to avoid a potential signed integer overflow
constlonglong n = number ;
sum_of_squares += n*n ;
}
std::cout << sum_of_squares << '\n' ;
}