Table of Contents
Example
Sample code
Output
Home Backend Development C++ Mean square of natural numbers?

Mean square of natural numbers?

Sep 20, 2023 pm 10:29 PM
average value Natural number square

Mean square of natural numbers?

The average of the squares of natural numbers is calculated by adding all the squares of n natural numbers and then dividing by that number.

Example

The first 2 natural numbers are 2.5,

12 22 = 5 => 5/2 = 2.5.

There are two calculation methods in programming -

  • Use loops
  • Use formulas
Use loops to calculate squares of natural numbers The average of

This logic works by finding the square of all natural numbers. Find the square of each by looping from 1 to n and add to the sum variable. Then divide that sum by n.

Program for calculating the sum of squares of natural numbers -

Sample code

Real-time demonstration

#include <stdio.h>
int main() {
   int n = 2;
   float sum = 0;
   for (int i = 1; i <= n; i++) {
      sum = sum + (i * i);
   }
   float average = sum/n;
   printf("The average of the square of %d natural numbers is %f", n,average);
   return 0;
}
Copy after login

Output

The average of the square of 2 natural numbers is 2.500000
Copy after login
Copy after login

Use the formula to calculate the mean of the squares of natural numbers.

There are mathematical formulas to make calculations easy. To calculate the sum of squares of natural numbers, the formula is ' n*(n 1)*((2*n) 1)/6' Divide it by the number n to get the formula: ' (n 1)* ((2*n) 1 )/6'.

Program for finding the sum of squares of natural numbers -

Sample code

Live demonstration< /p>

#include <stdio.h>
int main() {
   int n = 2;
   float average = ((n+1)*((2*n)+1)/6);
   printf("The average of the square of %d natural numbers is %f", n,average);
   return 0;
}
Copy after login

Output

The average of the square of 2 natural numbers is 2.500000
Copy after login
Copy after login

The above is the detailed content of Mean square of natural numbers?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to input square 2m³ on mobile phone 'Detailed introduction: Input method of square and cubic symbols' How to input square 2m³ on mobile phone 'Detailed introduction: Input method of square and cubic symbols' Feb 07, 2024 am 08:31 AM

Can you enter squares and cubes in Excel? Here are several quick input methods for you to choose from. 1. Input method input Nowadays, input methods are very smart. When you type "square" or "cube", [²] and [m³] will automatically appear in the options. The input method demonstrated here is a certain dog, and the specific one is not specified. Chinese input method. You can test your own input method. 2. Set the superscript input. First enter [m3] or [m2] in the cell, then select the number, right-click the mouse and select cell format settings, then set 3 and 2 as [superscript], and then confirm. It will be displayed as cube or square; 3. Alt + small keyboard input. Hold down Alt in the input cell and don’t release it, then press the number on the small keyboard.

What is the word formula for average? What is the word formula for average? Sep 19, 2023 pm 01:48 PM

The average word formula is "=AVERAGE(ABOVE)". Specific steps for calculating the average: 1. Open Word and create a new document; 2. In the document, enter the data you want to calculate the average, with each data occupying one row or column. For example, you can enter data in the first column; 3. In a blank cell below or to the right of the data, open the formula bar and enter the formula "=AVERAGE(ABOVE)"; 4. Press the Enter key and Word will Calculate and display the average value.

How to square the square in Excel How to square the square in Excel Mar 20, 2024 am 11:10 AM

When we use Excel software to create tables, sometimes we need to use the square symbol. So how do we enter the square symbol? Now I will share with you how to enter the square symbol in Excel, which is what many friends want to know recently. How to enter the square symbol in Excel. I hope it can help you! We want to enter the square in an Excel file, first open the Excel file. I will use square meters as an example to demonstrate. First, enter m2 in the Excel table. Then use the mouse to select the number 2 in m2, then right-click and you will see a [Format Cells] function. At this time, select the [Format Cells] button. At this time, the [Format Cells] dialog box will pop up in the Excel file, and then find

Mean square of natural numbers? Mean square of natural numbers? Sep 20, 2023 pm 10:29 PM

The average of the squares of natural numbers is calculated by adding all the squares of n natural numbers and then dividing by that number. The first two natural numbers in the example are 2.5, 12+22=5=>5/2=2.5. There are two methods of calculation in programming - Using loops Using formulas Calculating the average of squares of natural numbers using loops This logic works by finding the squares of all natural numbers. Find the square of each by looping from 1 to n and add to the sum variable. Then divide that sum by n. Program to calculate the sum of squares of natural numbers - sample code real-time demonstration #include<stdio.h>intmain(){ intn=2;

Use C++ to write code to find the Nth non-square number Use C++ to write code to find the Nth non-square number Aug 30, 2023 pm 10:41 PM

We all know numbers that are not the square of any number, such as 2, 3, 5, 7, 8, etc. There are N non-square numbers, and it is impossible to know every number. So, in this article, we will explain everything about squareless or non-square numbers and ways to find the Nth non-square number in C++. Nth non-square number If a number is the square of an integer, then the number is called a perfect square. Some examples of perfect square numbers are -1issquareof14issquareof29issquareof316issquareof425issquareof5 If a number is not the square of any integer, then the number is called non-square. For example, the first 15 non-square numbers are -2,3,5,6,

What are the functions for averaging arrays in php? What are the functions for averaging arrays in php? Jul 17, 2023 pm 04:03 PM

PHP array averaging functions include: 1. array_sum(), which is used to calculate the sum of all values ​​in the array. In order to calculate the average, you can add all the values ​​in the array and then divide by the number of array elements; 2 , array_reduce(), used to iterate the array and calculate each value with an initial value; 3. array_mean(), used to return the average of the array, first calculate the sum of the array, and calculate the number of array elements, then The sum is divided by the number of array elements to get the average.

Given an odd number, find the average of all odd numbers Given an odd number, find the average of all odd numbers Sep 03, 2023 pm 03:49 PM

The average of odd numbers up to a given odd number is a simple concept. You just need to find the odd numbers up to that number, then add them and divide by that number. If you want to find the average of odd numbers up to n. Then we will find the odd numbers from 1 to n and add them together and divide by the number of odd numbers. Example The average of odd numbers up to 9 is 5, i.e. 1+3+5+7+9=25=>25/5=5 There are two ways to calculate the average of odd numbers up to n, where n is an odd number using a loop using the formula Program to find the average of odd numbers up to n, using a loop. To find the average of odd numbers up to n, we will add all the numbers up to n and divide by the number of odd numbers up to n. Program to calculate average of odd natural numbers up to n - example code

How to find the average of a one-dimensional array in php How to find the average of a one-dimensional array in php Dec 26, 2022 am 10:20 AM

How to find the average of a one-dimensional array in PHP: 1. Create a new PHP file; 2. Create an array; 3. Use the array_sum function to sum the elements in the array; 4. Use the count function to calculate the number of array elements. Then divide the two numbers to find the average.

See all articles