Printing formula examples using For Loop in C++


Example Program 1

This program calculates the sum of the series 1/1! + 2/2! + 3/3! + ... + n/n!. The program starts by initializing four variables: i, j, n, and fact. j and fact are initialized to 0 and 1 respectively. The program then prompts the user to enter the value of n.

In the for loop, the program calculates the factorial of each number from 1 to n and stores it in fact. It then calculates the value of i/fact and adds it to j. This is repeated for each value of i from 1 to n. Finally, the program prints out the value of j, which is the sum of the series.

Source Code

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

Output

Enter the limit:5
Total value:2.70833

Example Program 2

This program calculates the sum of the series 1 + 1/2 + 1/3 + ... + 1/n. The program starts by initializing two variables: i and j. j is initialized to 0. The program then prompts the user to enter the value of n.

In the for loop, the program calculates the value of 1/i for each value of i from 1 to n and adds it to j. This is repeated for each value of i. Finally, the program prints out the value of j, which is the sum of the series.

Source Code

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

Output

Enter the limit:5
Total value:2.28333

Example Program 3

This program calculates the sum of the series 1/1 + 1/3 + 1/5 + ... + 1/n. The program starts by initializing two variables: i and j. j is initialized to 0. The program then prompts the user to enter the value of n.

In the for loop, the program calculates the value of 1/i for each odd value of i from 1 to n and adds it to j. This is repeated for each odd value of i. Finally, the program prints out the value of j, which is the sum of the series.

Source Code

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

Output

Enter the limit:5
Total value:1.53333

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