Compute Sum of Squares in C++

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to write a C++ program to find the sum of the series 1^2 + 3^2 + 5^2 + ... + n^2 using two different approaches. We will guide you through step-by-step to help you understand the process.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/AdvancedConceptsGroup(["`Advanced Concepts`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/AdvancedConceptsGroup -.-> cpp/pointers("`Pointers`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/variables -.-> lab-96194{{"`Compute Sum of Squares in C++`"}} cpp/data_types -.-> lab-96194{{"`Compute Sum of Squares in C++`"}} cpp/operators -.-> lab-96194{{"`Compute Sum of Squares in C++`"}} cpp/for_loop -.-> lab-96194{{"`Compute Sum of Squares in C++`"}} cpp/pointers -.-> lab-96194{{"`Compute Sum of Squares in C++`"}} cpp/function_parameters -.-> lab-96194{{"`Compute Sum of Squares in C++`"}} end

Create a new C++ file

First, we need to create a new C++ file. Open the terminal and navigate to the ~/project directory. Here, we will create a new file named main.cpp.

cd ~/project
touch main.cpp

Write code using the first method

Here, we will write the program using the first method. In this method, we will use a for loop to iterate through the odd numbers up to n and add their squares to the sum variable.

#include<iostream>
using namespace std;

int pattern_sum(int n){
    int sum=0;
    for(int i=1;i<=n;i+=2){
      sum+=(i*i);
    }
    return sum;
}

int main(){
    int num;
    cout<<"Enter the number of terms you want: ";
    cin>>num;
    cout<<"The sum of the series is: "<<pattern_sum(num)<<endl;
    return 0;
}

Code explanation:

  • First, we include the necessary header files.
  • Then we write a function named pattern_sum, which takes an integer n as input and returns the sum of the series.
  • Inside the function, we initialize a variable named sum to 0.
  • We use a for loop starting from 1 to n with a step of 2 to get the odd numbers, and add their squares to the sum variable.
  • Finally, we return the sum.
  • In the main() function, we ask the user to input the number of terms they want and store the value in the num variable.
  • We print the sum of the series by calling the pattern_sum() function with the user's input.

Write code using the second method

Here, we will write the program using the second method. In this method, we use the mathematical formula to find the sum of the series.

#include<iostream>
using namespace std;

int pattern_sum(int n){
    int sum;
    sum = ( ((2 * n) - 1) * (((2 * n) - 1)+ 1) * ( ( 2 * ((2 * n) - 1) ) + 1 ) ) / 6;
    return sum;
}

int main(){
    int num;
    cout<<"Enter the number of terms you want: ";
    cin>>num;
    cout<<"The sum of the series is: "<<pattern_sum(num)<<endl;
    return 0;
}

Code explanation:

  • First, we include the necessary header files.
  • Then we write a function named pattern_sum, which takes an integer n as input and returns the sum of the series.
  • Inside the function, we use the mathematical formula to find the sum of the series and store it in the sum variable.
  • Finally, we return the sum.
  • In the main() function, we ask the user to input the number of terms they want and store the value in the num variable.
  • We print the sum of the series by calling the pattern_sum() function with the user's input.

Full code for main.cpp

Here is the full code for main.cpp, using the second method to find the sum of the series:

#include<iostream>
using namespace std;

int pattern_sum(int n){
    int sum;
    sum = ( ((2 * n) - 1) * (((2 * n) - 1)+ 1) * ( ( 2 * ((2 * n) - 1) ) + 1 ) ) / 6;
    return sum;
}

int main(){
    int num;
    cout<<"Enter the number of terms you want: ";
    cin>>num;
    cout<<"The sum of the series is: "<<pattern_sum(num)<<endl;
    return 0;
}

Summary

In this lab, we learned how to find the sum of the series 1^2 + 3^2 + 5^2 + ... + n^2 using two different approaches: using a for loop and using a mathematical formula. We also covered how to write a C++ program to solve this problem. Now you should have a clear understanding of how to find the sum of the series using C++.

Other C++ Tutorials you may like