C++

 Write the following functions and procedures in c ++ code.

The function f1 that receives an integer value greater than or equal to one and using a for loop returns the result of the following summation:

f1.png

For example:

f1example.png

The function f2 that receives an integer value greater than or equal to one and using a for loop returns the result of the following multiplication:

f2.png

 For example:

f2example.png

The function f3 that receives an integer value greater than or equal to one and using a for loop returns the result of the following series:

f3.png

For example:

f3example.png

The increment procedure receives 2 real parameters, the lower limit of the LI series and the upper limit of the LS series. And using a for loop, the procedure should display the sequence of numbers from LI to LS in 0.4 increments.


For example:

increment (3.0, 5.0), it will display on screen: 3.0, 3.4, 3.8, 4.2, 4.6, 5.0

increment (5.1, 7.2), it will display on screen: 5.1, 5.5, 5.9, 6.3, 6.7, 7.1

Answer-The function f1-

#include <iostream>
#include <cmath>
using namespace std;
// function to calculate sum of series
int calculateSum(int n)
{    // initialize sum as 0
    int sum = 0;
    // loop to calculate sum of series
    for (int i = 0; i <= n; i++) {
        sum = sum + pow(2,i);// sum of series
    }
    return sum;
}
int main()
{
    int n;
    cout<<"Please enter the value of term:\n";
    cin>>n;
    cout << "Sum of series of power of 2 is : "
         << calculateSum(n);
    return 0;
}

Output-

Please enter the value of term:
3
Sum of series of power of 2 is : 15

Answer--The function f2-

#include <iostream>
using namespace std;
// function to calculate multiplication  of series
int calculateMul(int n)
{    // initialize mul as 1
    int mul = 1;
    // loop to calculate multiplication of series
    for (int i = 1; i <= n; i++) {
        mul = mul *( 3*i-1);// multiplication of series
    }
    return mul;
}
int main()
{
    int n;
    cout<<"Please enter the value of term:\n";
    cin>>n;
    cout << "Multiplication of series  is : "
         << calculateMul(n);
    return 0;
}

Output-

Please enter the value of term:
4
Multiplication of series  is : 880

Answer--The function f3-

#include <iostream>
using namespace std;
// Function to calculate sum of series
int series_sum(int n)
{
    // when n is odd
    if (n % 2 == 1)
        return (n + 1) / 2;
 
    // when n is not odd
    return -n / 2;
}
 int main()
{
    int n;
    cout<<"Enter value of term:\n";
    cin>>n;
    cout <<"Sum of series is:"<< series_sum(n);
    return 0;
}

Output-

Enter value of term:
4
Sum of series is:-2

 

Answer-increment-

#include <iostream>
using namespace std;
// Function to display series 
int increment(float n, float m)
{
    
    for(float i=n;i<=m;i=i+0.4)
    {
      cout<<i<<"\t";  
    }
}
 int main()
{
    float n,m;
    cout<<"Enter value of function:\n";
    cin>>n>>m;
    increment(n,m);
    return 0;
}

Output-

Enter value of function:
5.1
7.2
5.1 5.5 5.9 6.3 6.7 7.1

 

Comments

Popular posts from this blog

The python program allows the user to enter the Sales amount and Actual cost of a Product. Next, Python calculates the Loss Amount or profit Amount based on those two values using if-elif-else Statement

Mealy and Moore Machine

Question Bank of Theory of Automata & Formal Languages