0% found this document useful (0 votes)
5 views66 pages

24cse0104 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views66 pages

24cse0104 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 66

24CSE0104 - Programming for Engineering Problem Solving

Practical File

Bachelor of Engineering
Electrical Engineering

SUBMITTED BY
Vardaan Palta
2410996645

SUBMITTED TO
Dr. Abhishlipa Nandini
Assistant Professor

July – December 2024

DEPARTMENT OF ELECTRICAL ENGINEERING, CUIET-AE


CHITKARA UNIVERSITY, PUNJAB

1 | Page
INDEX
S.No. Title Page Date Signature
1. Exercise 1: Write a C++ program to print 5-6
“Ohm’s Law states that, At constant
temperature, the electrical current(I)
flowing through a fixed linear resistance(R)
is directly proportional to the voltage(V)
applied across it.”
2. Exercise 2: Write a C++ program to print 7-8
Wire size selection table.
3. Exercise 3: Write a program that calculates 9-10
and displays the total resistance of a series
and parallel circuit.
4. Exercise 4: Write a program to calculate 11-
voltage across a resistor using Ohm’s law. 12
5. Exercise 5: Write a program for calculating 13-
power in an electrical circuit. 14
6. Exercise 6: Write a program to calculate 15-
RMS (Root Mean Square) voltage from 16
peak voltage using the square root function
(sqrt).
7. Exercise 7: Write a program to calculates 17-
the frequency of an RC circuit. 18
8. Exercise 8: Write a program to calculate 19-
the resonant frequency of an LC circuit 20
(inductor-capacitor circuit).
9. Exercise 9: Write a program to calculate 21-
the frequency of an AC voltage signal. 22
10. Exercise 10: Write a program to calculate 23-
the voltage across an inductor based on its 24
inductance and the rate of change of
current.
11. Exercise 11: Write a program to calculate 25-
the roots of a quadratic equation using 26
decision control statements in perspective
to work with the all the cases whether the

2 | Page
equation gives a real, imaginary or equal
roots.
12. Exercise 12: Simulate the control of an 27-
LED (ON/OFF) using if-else statements. 28
13. Exercise 13: Write a program to simulate 29-
the charging process of a capacitor and 30
uses an if statement to check whether the
capacitor is fully charged or still charging
based on the calculated voltage.
14. Exercise 14: Write a program which uses 31-
cascading if and else if statements to 32
determine the warning level based on the
entered temperature.
15. Exercise 15: Write a program using a 33-
switch statement to control the direction of 34
a motor based on the user's input.
16. Exercise 16: Write a program using 35-
a switch statement to control a 36
voltage regulator based on a control
signal.
17. Exercise 17: Write a program using 37-
cascading if and else if statements to 38
determine the speed level of a motor based
on RPM.
18. Exercise 18: Write a program for Metric 39-
Conversions of Various Properties: 40
Temperature, Length, Energy and Power
using switch statement.
19. Exercise 19- Write program showcases the 41-
use of iterative statements (for loop), to 43
create a basic circuit analysis tool.
20. Exercise 20: Given a resistor with a 44-
constant resistance, calculate the voltage 45
across the resistor for a range of currents.

21. Exercise 21: Write a program to calculate 46-


the charging of a capacitor in an RC circuit 47
at different time intervals of 10
3 | Page
milliseconds.
22. Exercise 22: Write a program for voltage 48-
across the capacitor at any given time can 49
be calculated using the formula:
V(t)=Vmax × (1-e-t/RC)
23. Exercise 23: Create a C++ program to store 50-
and manipulate an array of voltages in a 51
circuit. The program should calculate the
total, average, minimum and maximum
voltage values using 1D arrays, allowing
for effective analysis and management of
voltage data within the circuit.
24. Exercise 24: Calculate Total Resistance of 52-
Resistors in Series Using Dynamic 53
Memory Allocation
25. Exercise 25: Calculate Power Dissipated by 54-
a Resistor Using Call by Reference 55
26. Exercise 26: Write a C++ program that 56-
accepts a string input representing the type 57
of circuit (e.g., "Series", "Parallel") and
calculate total resistance.
27. Exercise 27: Write a C++ program to 58-
convert lowercase letters to uppercase 59
letters in a given input string.
28. Exercise 28: Write a C++ program to use 60-
String functions length, at, append, 63
compare, substr, find, replace using switch.
29. Exercise 29: Write a C++ program to create 64-
a file that stores the resistance, current, and 66
voltage values of a circuit.

4 | Page
Exercise 1:

Aim:

Write a C++ program to print “Ohm’s Law states that, At constant


temperature, the electrical current(I) flowing through a fixed linear
resistance(R) is directly proportional to the voltage(V) applied across
it.”

Program:
#include
<iostream> using
namespace std;

int main() {
cout << "Ohm's Law states that, at constant temperature, the
electrical current (I) flowing through a fixed linear resistance (R) is
directly
proportional to the voltage (V) applied across it." <<
endl; return 0;

5 | Page
Ouput:

6 | Page
Exercise 2:
Aim:
Write a C++ program to print Wire size selection table.

Program:

#include
<iostream> using
namespace std; int
main() {
cout << "Wire Size Selection Table" << endl;
cout << "----------------------------" << endl;
cout << "Gauge\tAmpacity (A)" << endl;
cout << "----------------------------" << endl;
cout << "14 AWG\t15" <<
endl; cout << "12 AWG\t20"
<< endl; cout << "10 AWG\
t30" << endl; cout << "8
AWG\t40" << endl; cout << "6
AWG\t55" << endl; cout << "4
AWG\t70" << endl; cout << "2
AWG\t95" << endl; cout << "1
AWG\t110" << endl;
cout << "1/0 AWG\t125" <<
endl; cout << "2/0 AWG\t145"
<< endl; return 0;
}

7 | Page
Output:

8 | Page
Exercise 3:

Aim:
Write a program that calculates and displays the total resistance of a
series and parallel circuit.

Program:
#include
<iostream> using
namespace std; int
main() {
double R1, R2, R3;
cout << "Enter resistance of R1 (in
ohms): "; cin >> R1;
cout << "Enter resistance of R2 (in
ohms): "; cin >> R2;
cout << "Enter resistance of R3 (in
ohms): "; cin >> R3;
double totalSeriesResistance = R1 + R2 + R3;
double totalParallelResistance = 1 / (1 / R1 + 1 / R2 + 1 / R3);
cout << "\nTotal Resistance in Series: " << totalSeriesResistance
<< " ohms" << endl;
cout << "Total Resistance in Parallel: " << totalParallelResistance
<< " ohms" << endl;
return 0;
}

9 | Page
Output:

10 | Page
Exercise 4.
Aim: Write a program that converts temperature from Celsius to
Fahrenheit and vice versa.
Program:

#include <iostream>
using namespace std;
int main()
{
int a;
cout << "1. For Celsius To Fahrenheit. \n";
cout << "2. For Fahrenheit To Celsius. \n";
cout << "3. For Exit\n\n";
cout << "Enter Your Choice \n ";
cin >> a;
switch (a)
{ double cel, feh;
case 1:
cout << "Enter The Temperature In Celsius\n";
cin >> cel;
feh = (cel *9 / 5) + 32;
cout << "\nTemperature In Fahrenheit Is = " << feh;
break;
case 2:
cout << "Enter The Temperature In Fahrenheit\n";
cin >> feh;
cel = (feh - 32) *5 / 9;
cout << "\nTemperature In Celsius Is = " << cel;
break;
case 3:
exit(0);
default:
cout << "\nEnter The Right Choice \n";
break;
}
}

11 | Page
OUTPUT-

12 | Page
Exercise 5.
Aim :Write a program to calculate voltage across a resistor using Ohm’s law.
Program :

#include<iostream>
using namespace std;
int main(){
float Resistance,Current,Voltage;
cout<<"Enter the resistance in ohms: ";
cin>>Resistance;
cout<<"Enter the current in amperes: ";
cin>>Current;
Voltage=Resistance*Current;
cout<<"The voltage is: "<<Voltage<<" volts";
return 0;
}

13 | Page
OUTPUT-

14 | Page
Exercise 6.
Aim :Write a program for calculating power in an electrical circuit.
Program :

#include<iostream>
using namespace std;
int main(){
float Current,Resistance,Power;
cout<<"Enter the current in amperes: ";
cin>>Current;
cout<<"Enter the resistance in ohms: ";
cin>>Resistance;
Power=Current*Current*Resistance;
cout<<"The power is: "<<Power<<"watts"<<endl;
return 0;
}

15 | Page
OUTPUT-

16 | Page
Exercise 7.
Aim :Write a program to calculate RMS (Root Mean Square) voltage from
peak voltage using the square root function (sqrt).
Write a program to calculate the frequency of an RC circuit.
Program :

#include<iostream>
#include <cmath>
using namespace std;
int main() {
float V_peak;
cout << "Enter the peak voltage (in volts): ";
cin >> V_peak;
float V_RMS = V_peak / sqrt(2);
cout << "The RMS voltage is: " << V_RMS << " volts" << endl;
return 0;
}

17 | Page
OUTPUT-

18 | Page
Exercise 8.
Aim :Write a program to calculate the frequency of an RC circuit.
Program :

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
float r,c,frc;
const float PI = 3.14159f;
cout<< "Enter the Resistance (in ohms): ";
cin >> r;
cout<< "Enter the Capacitance (in farads): ";
cin >> c;
frc = 1/(2*PI*r*c);
cout << "The Frequency of RC is:" <<frc <<"Hz" <<endl;
return 0;
}

19 | Page
OUTPUT-

20 | Page
Exercise 9.
Aim : Write a program to calculate the resonant frequency of an LC
circuit (inductor-capacitor circuit).
Program :

#include <iostream>
#include <cmath>
using namespace std;
int main() {
float L, C, frequency;
const float PI=3.14;
cout << "Enter the inductance (in henries): ";
cin >> L;
cout << "Enter the capacitance (in farads): ";
cin >> C;
frequency = 1 / (2*PI*sqrt(L * C));
cout << "The resonant frequency of the LC circuit is: " <<
frequency << " Hz" << endl;
return 0;
}

21 | Page
OUTPUT-

22 | Page
Exercise 10.
Aim :Write a program to calculate the frequency of an AC voltage signal.
Program :

#include <iostream>
#include <cmath>
using namespace std;
int main() {
float omega, frequency;
const float PI=3.14;
cout << "Enter the angular frequency (in radians per second): ";
cin >> omega;
frequency = omega / (2 * PI);
cout << "The frequency of the AC voltage signal is: " << frequency << " Hz" << endl;
return 0;
}

23 | Page
OUTPUT-

24 | Page
Exercise 11.
Aim : Write a program to calculate the voltage across an inductor based on
its inductance and the rate of change of current.
Program :

#include <iostream>
#include<cmath>
using namespace std;
int main() {
float inductance, rateOfChangeOfCurrent,Voltage;
cout << "Enter the inductance (L) in henries: ";
cin >> inductance;
cout << "Enter the rate of change of current (dI/dt) in amperes per second: ";
cin >> rateOfChangeOfCurrent;
Voltage = rateOfChangeOfCurrent*inductance;
cout<<"The voltage across the inductor is: " << Voltage << " volts." << endl;
return 0;
}

25 | Page
OUTPUT-

26 | Page
Exercise 12.
Aim :Write a program to calculate the roots of a quadratic equation using
decision control statements in perspective to work with the all the cases
whether the equation gives a real, imaginary or equal
Program :

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
float a,b,c,d;
cout <<"Enter the value of a ." <<endl;
cin >> a;
cout <<"Enter the value of b ." <<endl;
cin >> b;
cout <<"Enter the value of c ." <<endl;
cin >> c;
d = pow(b,2)- (4*a*c);
cout << "d= " <<d <<endl;
if (d>0) {
cout << "The roots are real and distinct." <<endl;
}
else if (d==0) {
cout << "The roots are real and equal." <<endl;
}
else {
cout << "The roots are imaginary." <<endl;
}
return 0;
}

27 | Page
OUTPUT-

28 | Page
Exercise 13.
Aim :Simulate the control of an LED (ON/OFF) using if-else statements.
Program :

#include <iostream>
int main() {
bool button1State = false; // initial state of button 1
bool button2State = true; // initial state of button 2
bool button3State = false; // initial state of button 3
if (button1State) {
std::cout << "LED1 is ON" << std::endl;
} else {
std::cout << "LED1 is OFF" << std::endl;
}
if (button2State) {
std::cout << "LED2 is ON" << std::endl;
} else {
std::cout << "LED2 is OFF" << std::endl;
}
if (button3State) {
std::cout << "LED3 is ON" << std::endl;
} else {
std::cout << "LED3 is OFF" << std::endl;
}
return 0;
}

29 | Page
OUTPUT-

30 | Page
Exercise 14.
Aim: Write a program to simulate the charging process of a capacitor and
use an if statement to check whether the capacitor is fully charged or still
charging based on the calculated voltage.
Program:
#include <iostream>
#include <cmath>
const double VIN = 3.3; // supply voltage
const double R = 20000; // resistance in ohms
const double C = 0.0002; // capacitance in farads
const double THRESHOLD = 0.99; // 99% of maximum voltage
int main() {
double t = 0; // time in seconds
double Vc = 0; // initial voltage across capacitor
double dt = 1; // time step in seconds
while (Vc < VIN * THRESHOLD) {
// Calculate the voltage across the capacitor using the RC circuit model
Vc = VIN * (1 - exp(-t / (R * C)));
// Increment time
t += dt;
// Print the current voltage and time
std::cout << "Time: " << t << " s, Voltage: " << Vc << " V" << std::endl;
// Check if the capacitor is fully charged (within the specified threshold)
if (Vc >= VIN * THRESHOLD) {
std::cout << "Capacitor fully charged." << std::endl;
break;
}
}
return 0;
}

31 | Page
OUTPUT-

32 | Page
Exercise 15.
Aim: Write a program which uses cascading if and else if statements
to determine the warning level based on the entered temperature.
Program:

#include <iostream>
using namespace std;
int main() {
double temperature;
cout << "Enter the temperature: ";
cin >> temperature;
if (temperature < 0) {
cout << "Warning Level: Low" << endl;
} else if (temperature >= 0 && temperature < 20) {
cout << "Warning Level: Normal" << endl;
} else if (temperature >= 20 && temperature < 35) {
cout << "Warning Level: High" << endl;
} else {
cout << "Warning Level: Extreme" << endl;
}
return 0;
}

33 | Page
OUTPUT-

34 | Page
Exercise 16.
Aim: Write a program using a switch statement to control the
direction of a motor based on the user’s input.
Program:

#include <iostream>
using namespace std;
int main() {
int direction;
cout << "Motor Control Menu:" << endl;
cout << "1 - Forward" << endl;
cout << "2 - Backward" << endl;
cout << "3 - Left" << endl;
cout << "4 - Right" << endl;
cout << "0 - Stop" << endl;
cout << "Enter the direction (0-4): ";
cin >> direction;
switch (direction) {
case 1:
cout << "Motor is moving Forward." << endl;
break;
case 2:
cout << "Motor is moving Backward." << endl;
break;
case 3:
cout << "Motor is turning Left." << endl;
break;
case 4:
cout << "Motor is turning Right." << endl;
break;
case 0:
cout << "Motor has stopped." << endl;
break;
default:
cout << "Invalid input. Please enter a number between 0 and 4." << endl;
break; }
return 0; }

35 | Page
OUTPUT-

36 | Page
Exercise 17.
Aim: Write a program using a switch statement to control a voltage
regulator based on a control signal.
Program:

#include <iostream>
using namespace std;
int main() {
int controlSignal;
cout << "Voltage Regulator Control Menu:" << endl;
cout << "1 - Set Voltage to 3.3V" << endl;
cout << "2 - Set Voltage to 5V" << endl;
cout << "3 - Set Voltage to 12V" << endl;
cout << "4 - Set Voltage to 24V" << endl;
cout << "0 - Turn Off Voltage Regulator" <<
endl; cout << "Enter the control signal (0-4): ";
cin >> controlSignal;
switch (controlSignal) {
case 1:2
cout << "Voltage regulator set to 3.3V." << endl;
break;
case 2:
cout << "Voltage regulator set to 5V." << endl;
break;
case 3:
cout << "Voltage regulator set to 12V." << endl;
break;
case 4:
cout << "Voltage regulator set to 24V." << endl;
break;
case 0:
cout << "Voltage regulator is turned off." << endl;
break;
default:
cout << "Invalid input. Please enter a number between 0 and 4." << endl;
break; }
return 0; }

37 | Page
OUTPUT-

38 | Page
Exercise 18.
Aim: Write a program using cascading if and else if statements to
determine the speed level of a motor based on RPM.
Program:

#include <iostream>
using namespace std;
int main() {
int rpm;
cout << "Enter the RPM of the motor: ";
cin >> rpm;
if (rpm < 1000) {
cout << "Speed Level: Low" << endl;
} else if (rpm >= 1000 && rpm < 3000) {
cout << "Speed Level: Medium" << endl;
} else if (rpm >= 3000 && rpm < 5000) {
cout << "Speed Level: High" << endl;
} else {
cout << "Speed Level: Very High" << endl;
}
return 0;
}

39 | Page
OUTPUT-

40 | Page
Exercise 19.
Aim: Write a program for Metric Conversions of Various Properties:
Temperature, Length, Energy and Power using switch statement.
Program:

#include <iostream>
using namespace std;
void convertTemperature();
void convertLength();
void convertEnergy();
void convertPower();
int main() {
int choice;
cout << "Metric Conversion Menu:" << endl;
cout << "1 - Temperature Conversion (Celsius to Fahrenheit)" << endl;
cout << "2 - Length Conversion (Meters to Feet)" << endl;
cout << "3 - Energy Conversion (Joules to Calories)" << endl;
cout << "4 - Power Conversion (Watts to Horsepower)" << endl;
cout << "0 - Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
convertTemperature();
break;
case 2:
convertLength();
break;
case 3:
convertEnergy();
break;
case 4:
convertPower();
break;
case 0:
cout << "Exiting the program." << endl;
break;
default:
cout << "Invalid choice. Please enter a number between 0 and 4." <<
endl;
break;
}
return 0;
}

41 | Page
void convertTemperature() {
double celsius, fahrenheit;
cout << "Enter temperature in Celsius: ";
cin >> celsius;
fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
cout << celsius << "°C is equal to " << fahrenheit << "°F." << endl;
}
void convertLength() {
double meters, feet;
cout << "Enter length in meters: ";
cin >> meters;
feet = meters * 3.28084; // 1 meter = 3.28084 feet
cout << meters << " meters is equal to " << feet << " feet." << endl;
}
void convertEnergy() {
double joules, calories;
cout << "Enter energy in Joules: ";
cin >> joules;
calories = joules / 4.184; // 1 calorie = 4.184 joules
cout << joules << " Joules is equal to " << calories << " calories." << endl;
}
void convertPower() {
double watts, horsepower;
cout << "Enter power in Watts: ";
cin >> watts;
horsepower = watts / 745.7; // 1 horsepower = 745.7 watts
cout << watts << " Watts is equal to " << horsepower << " horsepower." << endl;
}

42 | Page
OUTPUT-

43 | Page
Exercise 20.
Aim: Write program showcases the use of iterative statements (for
loop), arrays, and an array of strings to create a basic circuit analysis
tool.
Program:

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const int MAX_RESISTORS = 5; // Maximum number of resistors
double voltages[MAX_RESISTORS]; // Array to hold voltage values
double resistances[MAX_RESISTORS]; // Array to hold resistance values
double currents[MAX_RESISTORS]; // Array to hold calculated current values
string resistorNames[MAX_RESISTORS]; // Array of resistor names
cout << "Basic Circuit Analysis Tool" << endl;
// Input voltage
double voltage;
cout << "Enter the total voltage (in Volts): ";
cin >> voltage;
// Input resistor values
for (int i = 0; i < MAX_RESISTORS; i++) {
cout << "Enter the name of Resistor " << (i + 1) << ": ";
cin >> resistorNames[i];
cout << "Enter the resistance of " << resistorNames[i] << " (in Ohms): ";
cin >> resistances[i];
// Calculate current using Ohm's Law: I = V / R
if (resistances[i] != 0) {
currents[i] = voltage / resistances[i];
} else {
currents[i] = 0; // Avoid division by zero
cout << "Resistance cannot be zero for " << resistorNames[i] << ".
Current is set to 0." << endl;
}
}
// Display results
cout << fixed << setprecision(2); // Set precision for output
cout << "\nCircuit Analysis Results:" << endl;
cout << setw(20) << "Resistor Name" << setw(20) << "Resistance (Ohms)" <<
setw(20) << "Current (Amps)" << endl;
cout << string(60, '-') << endl;
for (int i = 0; i < MAX_RESISTORS; i++) {

44 | Page
cout << setw(20) << resistorNames[i]
<< setw(20) << resistances[i]
<< setw(20) << currents[i] << endl;
}
return 0;
}

OUTPUT-

45 | Page
Exercise 21.
Aim: Given a resistor with a constant resistance, calculate the voltage
across the resistor for a range of currents.
Program:

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double resistance; // Resistance in Ohms
double currentStart, currentEnd, currentStep; // Current range parameters
const int MAX_CURRENTS = 100; // Maximum number of current values
double currents[MAX_CURRENTS]; // Array to hold current values
double voltages[MAX_CURRENTS]; // Array to hold calculated voltage values
// Input resistance
cout << "Enter the resistance of the resistor (in Ohms): ";
cin >> resistance;
// Input current range
cout << "Enter the starting current (in Amps): ";
cin >> currentStart;
cout << "Enter the ending current (in Amps): ";
cin >> currentEnd;
cout << "Enter the step size for current (in Amps): ";
cin >> currentStep;
// Validate input range
if (currentStart > currentEnd || currentStep <= 0) {
cout << "Invalid current range or step size." <<
endl; return 1; // Exit with an error code
}
// Calculate currents and voltages
int index = 0;
for (double current = currentStart; current <= currentEnd; current += currentStep) {
if (index < MAX_CURRENTS) {
currents[index] = current;
voltages[index] = current * resistance; // V = I * R
index++;
} else {
cout << "Maximum current values exceeded." << endl;
break;
}
}
// Display results
cout << fixed << setprecision(2); // Set precision for output
cout << "\nCurrent to Voltage Calculation:" << endl;
cout << setw(20) << "Current (Amps)" << setw(20) << "Voltage (Volts)" << endl;
cout << string(40, '-') << endl;
for (int i = 0; i < index; i++) {
cout << setw(20) << currents[i] << setw(20) << voltages[i] << endl;
}
return 0;
}

46 | Page
OUTPUT-

47 | Page
Exercise 22.
Aim:
Write a program to calculate the charging of a capacitor in an RC
circuit at different time intervals of 10 milliseconds.
Program:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
double V0;
double R;
double C;
const int TIME_INTERVAL_MS =
10; const int MAX_TIME = 100;
cout << "Enter the supply voltage (V0) in Volts: ";
cin >> V0;
cout << "Enter the resistance (R) in Ohms: ";
cin >> R;
cout << "Enter the capacitance (C) in Farads: ";
cin >> C;
double timeConstant = R * C;
cout << fixed << setprecision(4);
cout << "\nCharging of Capacitor in RC Circuit:" << endl;
cout << setw(15) << "Time (ms)" << setw(20) << "Voltage (V)" << endl;
cout << string(35, '-') << endl;
for (int t = 0; t <= MAX_TIME; t += TIME_INTERVAL_MS) {
double voltage = V0 * (1 - exp(-t / timeConstant));
cout << setw(15) << t << setw(20) << voltage <<
endl;
}
return 0;
}

48 | Page
OUTPUT-

49 | Page
Exercise 23.
Aim: Write a program for voltage across the capacitor at any given
time can be calculated using the formula:
V(t)=Vmax × (1- e^(-t/RC) )
Program:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
double Vmax;
double R;
double C;
double t;
cout << "Enter the maximum voltage (Vmax) in Volts: ";
cin >> Vmax;
cout << "Enter the resistance (R) in Ohms: ";
cin >> R;
cout << "Enter the capacitance (C) in Farads: ";
cin >> C;
cout << "Enter the time (t) in seconds: ";
cin >> t;
double timeConstant = R * C;
double voltage = Vmax * (1 - exp(-t / timeConstant));
cout << fixed << setprecision(4); // Set precision for output
cout << "Voltage across the capacitor at time " << t << " seconds is: " << voltage << " Volts" << endl;
return 0;
}

50 | Page
OUTPUT-

51 | Page
Exercise 24.
Aim: Create a C++ program to store and manipulate an array of
voltages in a circuit. The program should calculate the total, average,
minimum and maximum voltage values using 1D arrays, allowing for
effective analysis and management of voltage data within the circuit.
Program:

#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
const int MAX_VOLTAGES = 100;
double voltages[MAX_VOLTAGES];
int numVoltages;
cout << "Enter the number of voltage values (up to " << MAX_VOLTAGES << "): ";
cin >> numVoltages;
if (numVoltages <= 0 || numVoltages > MAX_VOLTAGES)
{ cout << "Invalid number of voltage values." << endl;
return 1;
}
for (int i = 0; i < numVoltages; ++i) {
cout << "Enter voltage value " << (i + 1) << ": ";
cin >> voltages[i];
}
double total = 0.0;
double minVoltage = numeric_limits<double>::max();
double maxVoltage =
numeric_limits<double>::lowest(); for (int i = 0; i <
numVoltages; ++i) {
total += voltages[i];
if (voltages[i] < minVoltage)
{ minVoltage =
voltages[i];
}
if (voltages[i] > maxVoltage) {
maxVoltage = voltages[i];
}
}
double average = total / numVoltages;
cout << fixed << setprecision(4);
cout << "\nVoltage Analysis Results:" << endl;
cout << "Total Voltage: " << total << " Volts" << endl;
cout << "Average Voltage: " << average << " Volts" << endl;
cout << "Minimum Voltage: " << minVoltage << " Volts" << endl;
cout << "Maximum Voltage: " << maxVoltage << " Volts" << endl;
return 0;

52 | Page
OUTPUT-

53 | Page
Exercise 25.
Aim: Calculate Total Resistance of Resistors in Series Using Dynamic
Memory Allocation.
Program:

#include <iostream>
using namespace std;
int main() {
int numResistors;
double* resistors;
cout << "Enter the number of resistors in series: ";
cin >> numResistors;
if (numResistors <= 0) {
cout << "Invalid number of resistors." << endl;
return 1;
}
resistors = new double[numResistors];
for (int i = 0; i < numResistors; ++i) {
cout << "Enter resistance value for resistor " << (i + 1) << " (in Ohms): ";
cin >> resistors[i];
}
double totalResistance = 0.0;
for (int i = 0; i < numResistors; ++i) {
totalResistance += resistors[i];
}
cout << "Total Resistance in Series: " << totalResistance << " Ohms" << endl;
delete[] resistors;
return 0;
}

54 | Page
OUTPUT-

55 | Page
Exercise 26.
Aim: Calculate Power Dissipated by a Resistor Using Call by
Reference
Program:

#include <iostream>
using namespace std;
void calculatePower(double voltage, double resistance, double &power)
{ if (resistance != 0) {
power = (voltage * voltage) / resistance;
} else {
cout << "Resistance cannot be zero." << endl;
power = 0;
}
}
int main() {
double voltage;
double resistance;
double power;
cout << "Enter the voltage across the resistor (in Volts): ";
cin >> voltage;
cout << "Enter the resistance (in Ohms): ";
cin >> resistance;
calculatePower(voltage, resistance, power);
cout << "Power dissipated by the resistor: " << power << " Watts" << endl;
return 0;
}

56 | Page
OUTPUT-

57 | Page
Exercise 27.
Aim: Write a C++ program that accepts a string input representing
the type of circuit (e.g., “Series", "Parallel") and calculate total
resistance.
Program:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
double calculateSeriesResistance(const vector<double>& resistances) {
double totalResistance = 0;
for (double resistance : resistances) {
totalResistance += resistance;
}
return totalResistance;
}
double calculateParallelResistance(const vector<double>& resistances) {
double totalResistance = 0;
for (double resistance : resistances) {
totalResistance += 1.0 / resistance;
}
return 1.0 / totalResistance;
}
int main() {
string circuitType;
int numResistors;
vector<double> resistances;
cout << "Enter the type of circuit (Series/Parallel): ";
cin >> circuitType;
if (circuitType != "Series" && circuitType != "Parallel") {
cout << "Invalid circuit type. Please enter either 'Series' or 'Parallel'." << endl;
return 1;
}
cout << "Enter the number of resistors: ";
cin >> numResistors;
for (int i = 0; i < numResistors; i++) {
double resistance;
cout << "Enter the resistance of resistor " << i + 1 << " (in ohms): ";
cin >> resistance;
resistances.push_back(resistance);
}
if (circuitType == "Series") {
double totalResistance = calculateSeriesResistance(resistances);
cout << "Total Resistance in Series Circuit: " << totalResistance << " ohms" << endl;
} else if (circuitType == "Parallel") {
double totalResistance = calculateParallelResistance(resistances);
cout << "Total Resistance in Parallel Circuit: " << totalResistance << " ohms" << endl;
}
return 0;
}

58 | Page
OUTPUT-

59 | Page
Exercise 28.
Aim: Write a C++ program to convert lowercase letters to uppercase letters in
a given input string.

Program:

#include <iostream>
#include <string>
using namespace std;
int main () {
string input;
cout << "Enter a string: ";
getline(cin, input);
for (char &c : input){
c = toupper(c);
}
cout << "Converted string: "<< input <<endl;
return 0;
}

60 | Page
OUTPUT-

61 | Page
Exercise 29.
Aim: Write a C++ program to use String functions length, at, append,
compare, substr, find,
replace using
switch.
Program:
#include <iostream>
#include <string>
using namespace std;
int main () {
string str,result;
int choice;
cout << "Enter a string: ";
getline(cin,str);
do {
cout << "\nChoose an option: " <<endl;
cout << "1. Length of the string " <<endl;
cout << "2. Access a character at a specific position (using at) " <<endl;
cout << "3. Append another string " <<endl;
cout << "4. compare two strings " <<endl;
cout << "5. Get a substring " <<endl;
cout << "6. find a character in the string " <<endl;
cout << "7. Replace a part of the string " <<endl;
cout << "0. Exit " <<endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
cout <<"Length of the string: " <<str.length() << endl;
break;
case 2:
int pos;
cout <<"Enter position(0 to " <<str.length() -1 << "): ";
cin >> pos;
if (pos >= 0 && pos < str.length()) {
cout << "Character at position " << pos << ": " << str.at(pos)<<endl;
} else {
cout << "Invalid positional" << endl;
}
break;
case 3:
{ string appendstr;
cout << "Enter the string to append: ";
cin.ignore();
getline(cin,appendstr);
str.append(appendstr);
cout << "Updated string: " << str <<endl;
}
break;
case 4:
{
string comparestr;
cout << "Enter the string to compare with: ";
getline(cin,comparestr);
int result = str.compare(comparestr);
if (result == 0) {
cout << "The strings are equal." <<endl;
} else if (result <0) {
cout << "The original string is less than the compared string." << endl;
} else {
cout << "The original string is greater than the compared string." << endl;
}

62 | Page
} break;
case 5:
{
int start,length;
cout << "Enter starting position of substring: ";
cin >> start;
cout << "Enter the length of substring: ";
result = str.substr(start,length);
cout << "Substring: "<< result << endl;
} break;
case 6:
{ char ch;
cout << "Enter character to find: ";
cin >> ch;
int pos = str.find(ch);
if (pos != string::npos){
cout << "Character " << ch << " found at position " << pos << endl; }
else {
cout <<"Character not found"<<endl;
} break;
}
case 7:
{ string toreplace, replacewith;
cout << "Enter the string to replace: ";
cin.ignore();
getline(cin,toreplace);
cout << "Enter the string to replace with: ";
getline(cin,replacewith);
int pos =
str.find(toreplace); if (pos !
= string::npos) {
str.replace(pos, toreplace.length(), replacewith);
cout << "Updated string: "<< str <<endl;
} else {
cout << "Substring not found!" <<endl;
}} break;
case 0:
cout << "Exiting the program. " <<endl;
break;
default:
cout << "Invalid choice, Please try again." <<endl;
}
} while (choice != 0);
return 0;
}

63 | Page
OUTPUT-

64 | Page
Exercise 30.
Aim: Write a C++ program to create a file that stores the resistance, current,
and voltage values of a circuit.

Program:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
double resistance, current, voltage;
cout << "Enter the resistance (in Ohms): ";
cin >> resistance;
cout << "Enter the current (in Amperes): ";
cin >> current;
voltage = current * resistance;
ofstream outputFile("data.txt");
if (!outputFile) {
cerr << "Error: Could not open the file." << endl;
return 1;
}
outputFile << "Resistance (Ohms): " << resistance << endl;
outputFile << "Current (Amperes): " << current << endl;
outputFile << "Voltage (Volts): " << voltage << endl;
outputFile.close();
cout << "Data has been written to data.txt successfully." << endl;
return 0;
}

65 | Page
OUTPUT-

66 | Page

You might also like