Reputation: 11
#include<stdio.h>
int main(){
int i, n;
long sum=0;
puts("Enter the number: ");
scanf("%d", &n);
for(i=1; i<=n; i++){
sum = sum + (i * i);
printf("(%d * %d) = %d\n", i, i, (i*i) );
}
printf("\nThe sum of the series is %ld\n", sum);
return 0;
}
suppose I enter the input as 5, then the operation goes like this 1 * 1 = 1, 2 * 2 = 4, 3 * 3 = 9, 4 * 4 = 16, 5 * 5 = 25..... and then, I want to show the sum of every individual product... I want the output to be like this:
(1 * 1) = 1
(2 * 2) = 4
(3 * 3) = 9
(4 * 4) = 16
(5 * 5) = 25
The sum of the series is 1 + 4 + 9 + 16 + 25 = 55
Upvotes: 0
Views: 104
Reputation: 42
There is a few things you could do just as a suggestion for beginner legibility
sum = sum + (i * i);
i*i
can be pulled out to
int iSquared = i * i;
then store
int nextProduct = sum + iSquared;
and finally simplify the summation with
sum += nextProduct
Bringing it all together you'd have
#include<stdio.h>
int main(){
int i, n;
long sum=0;
puts("Enter the number: ");
scanf("%d", &n);
for(i=1; i<=n; i++){
int iSquared = i * i;
int nextProduct = sum + iSquared;
sum += nextProduct;
printf("(%d * %d) = %d\n", i, i, iSquared);
}
printf("\nThe sum of the series is %ld\n", sum);
return 0;
}
It's highly debatable if this is more user friendly honestly, but I still think it's more clear to have each step broken out logically especially when first trying to learn C++ or programming in general.
Upvotes: -1
Reputation: 108978
Here's my attempt at your requirements.
I saved every product in its own array during the loop, then print them one by one again in the final line.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
unsigned i, n; // no negative numbers used
long unsigned sum = 0;
long unsigned parcel[2000]; // could use VLA or dynamic memory
printf("Enter the number: "); // write the input on the
fflush(stdout); // same line as the prompt
if (scanf("%u", &n) != 1) {
fprintf(stderr, "Input error.\n");
exit(EXIT_FAILURE);
}
if (n > 2000) {
fprintf(stderr, "Sorry, max number is 2000.\n");
exit(EXIT_FAILURE);
}
for (i = 0; i < n; i++) { // I like to start for loops at 0
long unsigned product = (i + 1) * (i + 1);
sum = sum + product;
printf("(%u * %u) = %lu\n", i + 1, i + 1, product);
// save product for final output
parcel[i] = product;
}
printf("\nThe sum of the series is %lu", parcel[0]);
// exception for starting for loops at 0: the 0th element was printed above
for (i = 1; i < n; i++) printf(" + %lu", parcel[i]);
printf(" = %lu\n", sum);
return 0;
}
Upvotes: 1
Reputation: 11496
For a start, you can do it in the loop itself:
printf("\nThe sum of the series is ");
for (i = 1; i <= n; i++){
sum = sum + (i * i);
// Don't print a leading + for the first number to avoid
// "The sum of the series is + 1 + 4 + 9 + 16 + 25 = 55"
if (i > 1) {
printf(" + ");
}
// Print the current term
printf("%d", i * i);
}
printf(" = %ld\n", sum);
This outputs, for
The sum of the series is 1 + 4 = 5
The sum of the series is 1 + 4 + 9 + 16 + 25 = 55
But it also outputs, for
The sum of the series is = 0
The sum of the series is 1 = 1
Handling user input is up to you.
If you also wants to output the middle terms, you can iterate over the terms twice:
for (int i = 1; i <= n; i++){
sum = sum + (i * i);
printf("(%d * %d) = %d\n", i, i, i*i);
}
printf("The sum of the series is ");
for (int i = 1; i <= n; i++){
if (i > 1) printf(" + ");
printf("%d", i*i);
}
printf(" = %ld.\n", sum);
Outputs
(1 * 1) = 1
(2 * 2) = 4
(3 * 3) = 9
(4 * 4) = 16
(5 * 5) = 25
The sum of the series is 1 + 4 + 9 + 16 + 25 = 55.
Upvotes: 2