1. Write a complete C++ application to prompt the user for the double voltage and resistance of a circuit, and call function calcurrent to calculate and display the current of the circuit.
Use the following statement to calculate the current:
double current = voltage / resistance
2. Write a C++ program to calculate the product of all even numbers from 1 to 13 using a for loop.
3. Write a C++ Program to generate a random from the following list of numbers
Put the above statements in a C++ program and run the program and print the generated numbers
1
#include <iostream>
using namespace std;
int main() {
double voltage, resistance;
cout << "Enter a voltage (V): ";
cin >> voltage;
cout << "Enter a resistance (Ohm): ";
cin >> resistance;
double current = voltage / resistance;
cout << "Current is " << current << " A";
return 0;
}
2
#include <iostream>
using namespace std;
int main() {
int prod = 1;
for (int i=1; i<=13; i++) {
if (i % 2 == 0) {
prod *= i;
}
}
cout << "The product of all even numbers from 1 to 13 is " << prod << endl;
return 0;
}
3
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int random(int low, int hi) {
return rand() % (hi - low + 1) + low;
}
int main() {
srand(time(nullptr));
int i1 = random(1, 535);
cout << "1: " << i1 << endl;
int i2 = random(-14, 190);
cout << "2: " << i2 << endl;
int i3 = 12 * random(1, 5);
cout << "3: " << i3 << endl;
int i4 = random(0, 241);
cout << "4: " << i4 << endl;
int i5 = random(42, 90);
cout << "5: " << i5 << endl;
return 0;
}
Comments
Leave a comment