Printing cubic numbers using For Loop in C++
This is a C++ program that calculates the sum of cubes of natural numbers up to a given limit entered by the user. Here's how the program works:
- The program begins by declaring and initializing two variables: 'n' and 'tot' to 0, and 'i' to 1.
- The program then prompts the user to enter a limit.
- The user enters the limit, which is stored in the variable 'n'.
- The program then enters a 'for' loop that runs from 'i=1' to 'i<=n'.
- Inside the loop, the program adds the cube of 'i' to the variable 'tot'.
- Once the loop is finished, the program prints out the value of 'tot', which is the sum of cubes of natural numbers up to the given limit entered by the user.
- The program then terminates by returning 0.
Source Code
#include<iostream>
using namespace std;
int main()
{
int n,tot=0,i;
cout<<"\n Enter the Limit:";
cin>>n;
for(i=1;i<=n;i++)
{
tot+=i*i*i;
}
cout<<"\n "<<tot;
return 0;
}
To download raw file
Click Here
Output
Enter the Limit:10
3025