In: Computer Science
1.Using C++ code, write a program named q4.cpp to compute the product (multiplication) of 4 integers a, b, c and d, where a is input by a user via the keyboard, b = 2a - 1, c = ab - 2 and d = |a - b - c|.
2.Using C++ code, write a program named q5.cpp to solve the equation 2n = 14.27 for n.
3.Using C++ code, write a program named q3.cpp to divide each of the elements of a 3- element array T of real values by the sum of the elements of T, each of which is input by a user via the keyboard.
Problem 1: The following is q4.cpp
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d,product;
cout<<"Enter the value of a: ";
cin>>a; //take the input a
cout<<endl; //print new line
b=2*a-1; //calculate b
c=a*b-2; //calculate c
d=abs(a-b-c); //calculate d; here abs will give us the modulus of an integer i.e returns positive value
product=a*b*c*d; //calculate product
cout<<"The product a*b*c*d is : "<<product<<endl; //print the product
return 0;
}
Output:
Enter the value of a: 3
The product a*b*c*d is : 2925
Problem 2: The following is q5.cpp
#include <iostream>
using namespace std;
int main()
{
double n; //declare it as either double or float
n=14.27/2; //since 2n=14.27, n will be 14.27/2;
cout<<"The value of n for the equation 2n=14.27 is "<<n; //print the value of n
return 0;
}
Output:
The value of n for the equation 2n=14.27 is 7.135
Problem 3: The following is q6.cpp
#include <iostream>
using namespace std;
int main()
{
int T[3],i,sum=0; //declare an array T with size 3 and sum of type int to 0
double value;
cout<<"Enter the 3 values of an array: "<<endl;
for(i=0;i<3;i++)
{
cin>>T[i]; //input the values entered by user and store in T array
sum=sum+T[i]; //add the values to calculate sum
}
for(i=0;i<3;i++)
{
value=((double)T[i]/(double)sum); //Here type conversion is very important. Since sum and array values are int, we have to convert them to double and apply division. The result will be stored in value.
cout<<"The value when "<<T[i]<<" is divided by sum is "<<value<<endl; //print the required result
}
return 0;
}
Output:
Enter the 3 values of an array:
3
4
5
The value when 3 is divided by sum is 0.25
The value when 4 is divided by sum is 0.333333
The value when 5 is divided by sum is 0.416667
#Please dont forget to upvote if you find the solution helpful. Feel free to ask doubts if any, in the comments section. Thank you.