In: Computer Science
Write a C++ program that asks the user for the values of three resistances. We suppose that the user will not enter a negative value. The program then asks the user whether he has a series or parallel montage. The user will answer with S or s for series and P and p for parallel. (If the user enters a wrong answer, he will see a message on the screen alerting him, and the program will end.) Finally, the program displays the total resistance value. (Note that for a parallel circuit, if one of the resistances is zero, the total resistance is zero)
C++ CODE :
#include <iostream>
using namespace std;
int main()
{
float a, b, c;
char ch;
cout << "Please enter the three resistors positives values :
";
cin >> a >> b >> c; // read the three
resistors
cout << "Please enter if it is series or parallel : ";
cin >> ch; // read if it is series or parallel
if(ch == 's' || ch == 'S') // if it is series
{
cout << "Total Resistance : " << (a + b + c);
}
else if(ch == 'p' || ch == 'P') // if it is parallel
{
cout << "Total Resistance : " << 1 / ((1 / a) + (1 / b)
+ (1 / c));
}
else // else print the alert message
{
cout << "Entered input is wrong, Please run the program
again";
}
return 0;
}
OUTPUT :