Write a program to print the sum of squares of all the numbers from 1 to 10 using a loop
Answers
Answered by
4
The given problem is solved using language - Java.
public class Brainly{
public static void main(String s[]){
int sum=0,i;
for(i=1;i<=10;i++)
sum+=i*i;
System.out.println("Sum of squares of numbers from 1 to 10 is: "+sum);
}
}
- Line 1: Class Declaration.
- Line 2: Declaration of main() method.
- Line 3: Two variables named 'i' and 'sum' are declared where 'sum' stores the sum of the squares of first ten natural numbers.
- Line 4: A loop iterates in the range 1 to 10.
- Line 5: The square of each number in this range is added and stored in the variable 'sum'.
- Line 6: The sum is now displayed on the screen.
- Line 7: End of main().
- Line 8: End of class.
See the attachment for output.
Attachments:
Similar questions