Printing sum of two square using For Loop in C++


This C++ program calculates the sum of the squares of numbers from 1 to a given limit n and prints the total value. Here is how the program works:

  • The program starts by including the necessary header file iostream and using the namespace std.
  • The program declares four integer variables: i, j, and n. i and j are initialized to 0, and n is input by the user.
  • The program then enters a for loop that runs from i=1 to i=n. In each iteration of the loop, the value of i is squared and added to the current value of j.
  • After the loop finishes, the total value of j is printed as output.
  • The program returns 0, indicating successful execution.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,j=0,n;
    cout<<"\nEnter the Limit:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        j=i*i+j;
    }
    cout<<"\nTotal value is:"<<j;
    return 0;
}
To download raw file Click Here

Output

Enter the Limit:4
Total value is:30

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples