In: Computer Science
1. Write pseudocode for the algorithm using iteration (looping).
Envision an algorithm that when given any positive integer n, it will print out the sum of the squares from 1 to n. For example given 3 the algorithm will print 14 (because 1 + 4 + 9 =14) You can use multiplication denoted as * in your solution and you do not have to define it (For example. 3*3=9) For example if n is 6 it will print: The sum of squares from 1 to 6 = 91 Again, to get marks, the algorithm needs to work for any n given.
Pseudocode
Function SumOFSquare (n)
1. Declare integers variables n, sum, iteration variable(i)
2. IF n larger than 0:
FOR ( i equals to 1 [initialization statement]
i less than or equal to n [ condition statement]
incrementation of i) [increment statement]
sum equals to sum added with ( i*i)
3. ELSE
PRINT "Input number is non negative integer"
4. RETURN sum
*Providing a C code for the above program for reference
#include <stdio.h>
int main()
{
int sum=0, n, i;
printf("Input number n:");
scanf("%d", &n);
if (n>0)
{
for (i=1;i<=n;i++)
{
sum=sum+(i*i);
}
printf("sum=%d", sum);
}
else
printf("Input number is non
positive integer!!");
}