Basic Programming C# Examples

C# Program to Find Sum of Squares of Elements of Given array

The Program in C# Program to Find Sum of Squares of Elements of Given array is given below:

using System;

namespace SumOfSquares
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family,enter the number of elements in the array here to find the sume of squares of elements in array");
            int n = int.Parse(Console.ReadLine());

            int[] numbers = new int[n];

            Console.WriteLine("Well,enter the elements of the array: ");
            for (int i = 0; i < n; i++)
            {
                numbers[i] = int.Parse(Console.ReadLine());
            }

            int sum = 0;
            foreach (int num in numbers)
            {
                sum += num * num;
            }

            Console.WriteLine("Okay,the sum of squares of the elements in the array is: " + sum);
        }
    }
}

Output:

Hello Codeauri Family,enter the number of elements in the array here to find the sume of squares of elements in array
2

Well,enter the elements of the array:
199
200
Okay,the sum of squares of the elements in the array is: 79601

Pro-Tips💡

Here are the step by step execution of above program:

  1. The first line of code using System; specifies the namespace that contains the classes needed for the program.
  2. Next, the program is defined as belonging to the SumOfSquares namespace.
  3. Within the namespace, a class called Program is defined. The Main method of this class is the starting point of the program.
  4. The Main method first prints a message asking the user to enter the number of elements in the array, and stores the user’s input in the integer variable n.
  5. An array numbers is declared with n elements, using int[] numbers = new int[n];.
  6. A for loop is used to read the elements of the array from the user, using int.Parse(Console.ReadLine()) to parse the input string as an integer. The elements are stored in the numbers array.
  7. A foreach loop is used to iterate through the elements in the numbers array, computing the square of each element using num * num and adding it to the sum variable.
  8. Finally, the result of the computation is printed to the console using Console.WriteLine("Okay,the sum of squares of the elements in the array is: " + sum);. The sum variable is converted to a string using string concatenation with an empty string.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X