I'm a beginner, this is my current code so far. What I'm confused with is on on how to implement "working out the sum of the squares of the numbers".
#include <stdio.h>
int main()
{
int i,n,Sum=0,numbers;
float Average;
printf("\nPlease Enter How many Numbers you want?\n");
scanf("%d",&n);
printf("\nPlease Enter the Number(s) one by one\n");
for(i=0;i<n; i)
{
scanf("%d", &numbers);
Sum = Sum numbers;
}
Average = Sum/n;
printf("\nSum of the %d Numbers = %d",n,Sum);
printf("\nAverage of the %d Numbers = %.2f",n, Average);
// printf("\nSum of the Square of %d Numbers = %d",n, );
return 0;
}
CodePudding user response:
This is a solution to your problem : ( Changes are marked with //******** )
#include <stdio.h>
int main()
{
int i,n,Sum=0,numbers, square = 0;//***********************
float Average;
printf("\nPlease Enter How many Numbers you want?\n");
scanf("%d",&n);
printf("\nPlease Enter the Number(s) one by one\n");
for(i=0;i<n; i)
{
scanf("%d", &numbers);
Sum = Sum numbers;
square = square (numbers*numbers); //***************
}
Average = Sum/n;
printf("\nSum of the %d Numbers = %d",n,Sum);
printf("\nAverage of the %d Numbers = %.2f",n, Average);
printf("\nSum of the Square of %d Numbers = %d",n, square);//****************
return 0;
}
Although other methods could be used to solve your question.
CodePudding user response:
Sum of squares of numbers has a mathematical formula:
1^2 2^2.... n^2 = (n(n 1)(2n 1))/6
You can use that to directly get the solution.
CodePudding user response:
For the sum of the squares of the numbers as:
#include<stdio.h>
int main()
{
int n, sum=0;
printf("Enter n value: ");
scanf("%d", &n);
for(int i=0; i<=n; i )
{
sum = (i*i);
}
printf("Sum of squares of %d numbers = %d", n, sum);
return 0;
}
CodePudding user response:
You can create a variable and do exactly as you do with Sum
, except you are now adding the squares.
int sumOfSqaure = 0;
// code
for(i = 0; i < n; i)
{
scanf("%d", &numbers);
// a = a b is equal to a = b
sumOfSqaure = numbers;
sumOfSqaure = numbers * numbers; // n * n == n ^ 2
}