Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a program I need in C++. I will need to eventually code this down to Ass

ID: 3605714 • Letter: T

Question

This is a program I need in C++. I will need to eventually code this down to Assembly programming so I would like to use while loops primarily.

This program needs to take a positive integer input from the user, and give the sum of all of those numbers up to the input number. For example, if the user enters a 5, it will calculate that 1 + 2 + 3 + 4 + 5 = 15.

I have got that part of it working; but, I also need it to calculate the sum of any perfect squares in the same number set.

So for example with the same number set, 1, 2, 3, 4, 5... The sum of the perfect squares would be 5 because 1 and 4 are perfect squares.

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


#include <cstdlib>
#include <iostream>
using namespace std;


int main(int argc, char** argv) {

int input, temp;
int sum = 0;
int sqrt = 0;
printf("Enter a positive integer value: ");
scanf("%d", &input);
temp = input;
while(input > 0)
{
sum += input;
for(int i = 1; i <= input; i++)
{
if(i*i == input)
{
sqrt += i;
}
}
input--;
}
printf("The sum of all the numbers up to %d is %d", temp, sum);
cout << endl;
printf("The sum of all the perfect squares up to %d is %d", temp, sqrt);
  
return 0;
}

Explanation / Answer

Here is simpler logic

#include <cstdlib>
#include <iostream>
using namespace std;

int main(int argc, char** argv) {
int input, temp;
int sum = 0;
int sqrt = 0;
printf("Enter a positive integer value: ");
scanf("%d", &input);
  
temp = input;
while(input > 0)
{
sum += input;
if (input*input <= temp) {
sqrt += input*input;
}
input--;
}
printf("The sum of all the numbers up to %d is %d", temp, sum);
cout << endl;
printf("The sum of all the perfect squares up to %d is %d", temp, sqrt);
  
return 0;
}

Sample output

Here is your logic fixed

#include <cstdlib>
#include <iostream>
using namespace std;

int main(int argc, char** argv) {
int input, temp;
int sum = 0;
int sqrt = 0;
printf("Enter a positive integer value: ");
scanf("%d", &input);
temp = input;
while(input > 0)
{
sum += input;
for(int i = 1; i <= input; i++)
{
if(i*i == input)
{
sqrt += i*i;
}
}
input--;
}
printf("The sum of all the numbers up to %d is %d", temp, sum);
cout << endl;
printf("The sum of all the perfect squares up to %d is %d", temp, sqrt);
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote