write a program to input 3 numbers and print sum of squares of the number?
Answers
Answer:
The sum of squares of the first n natural numbers is found by adding up all the squares.
Input - 5
Output - 55
Explanation - 12 + 22 + 32 + 42 + 52
There are two methods to find the Sum of squares of first n natural numbers −
Using Loops − the code loops through the digits until n and find their square, then add this to a sum variable that outputs the sum.
Example
#include <iostream> using namespace std; int main() { int n = 5; int sum = 0; for (int i = 1; i >= n; i++) sum += (i * i); cout <<"The sum of squares of first "<<n<<" natural numbers is "<<sum; return 0;
Explanation:
HOPE MY ANSWERS HELPS U IF U HAD PLZZZ BRAINLIST ME AND FOLLOW ME
★
import java.util.Scanner; // Line 1
public class Main { // Line 2
public static void main(String [] args) { // Line 3
Scanner sc = new Scanner(System.in); // Line 4
System.out.print("Enter the three numbers"); // Line 5
int num1 = sc.nextInt(); // Line 6
int num2 = sc.nextInt(); // Line 7
int num3 = sc.nextInt(); // Line 8
int sum = Math.pow(num1, 2) + Math.pow(num2, 2) + Math.pow(num3, 2); // Line 9
System.out.println("The sum is " + sum); // Line 10
} // Line 11
} // Line 12
★
❖ Line 1: This line imports the Scanner class from the java.util package, which is used to read input from the user.
❖ Line 2: This line defines a public class called Main.
❖ Line 3: This line defines the main method of the program, which is the entry point for the program's execution.
❖ Line 4: This line creates a new Scanner object called sc that reads input from the standard input stream.
❖ Line 5: This line prints the message "Enter the three numbers" to the console.
❖ Line 6: This line reads an integer input from the user using the nextInt() method of the Scanner object and assigns it to the variable num1.
❖ Line 7: This line reads another integer input from the user using the nextInt() method of the Scanner object and assigns it to the variable num2.
❖ Line 8: This line reads a third integer input from the user using the nextInt() method of the Scanner object and assigns it to the variable num3.
❖ Line 9: This line calculates the sum of the squares of the three numbers using the Math.pow() method, which is used to raise each number to the power of 2 (i.e. to square each number), and then adds them together. The result is stored in the variable sum.
❖ Line 10: This line prints the message "The sum is " followed by the value of the sum variable to the console.
❖ Line 11: This line marks the end of the main method.
❖ Line 12: This line marks the end of the class.