CPlus Plus Book 1
CPlus Plus Book 1
SAMPLE OUTPUT
Voltage supply = 15V
Resistor R = 270K and capacitor C = 100microF
NOTE: The capacitor cannot be 100% charged, the output only estimates for 99%
charged and the charging time may be from 127 to around 190 seconds.
SAMPLE PROGRAM
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main(){
int input;
int dc, m, rm, i=1, sum=0;
int bin, dec = 0, rem, num, base = 1;
cout<<"Pls choose the conversion:-"<<endl;
cout<<"1. Decimal to Binary conversion."<<endl;
cout<<"2. Binary Decimal to conversion."<<" Choice (1/2): ";
cin>>input;
switch(input){
case 1: cout<<"\nEnter the decimal to be converted: ";
cin>>dc;
m=dc;
while (dc>0) { rm=dc%2;
sum=sum + (i*rm);
dc=dc/2;
i=i*10; }
cout<<"The binary equivalent of "<<m<<" is: "<<sum<<endl;
break;
case 2: cout << "\nEnter the binary number (1s and 0s): ";
cin >> num;
bin = num;
while (num > 0) {
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10; }
cout << "The decimal equivalent of " << bin << " is: " << dec << endl;
break;
default: cout<<"\nInvalid choice."<<endl; }
return 0; }
SAMPLE OUTPUTS
Pls choose the conversion:-
1. Decimal to Binary conversion.
2. Binary Decimal to conversion. Choice (1/2): 1
NOTE:
The decimal and binary number entered by user is not checked for validity.
Choice other than 1 or 2 will result in ‘Invalid choice’ printed out.
3. Based on Newton’s method, write a program to calculate the root(s) of the equation:-
2x3 - 3x2 + 2x - 6 = 0. Newton’s formula for finding the roots of an equation is:-
f ( xi )
xi 1 xi
f '( xí )
where f’(xi) is the derivative of function f(x) evaluated at x=xi. The program display the
equation and ask the user to enter a guess i.e. the starting value of x. The program
then plots the roots from the guessed value until it converges in table form.
Use while…do loop for the iteration process.
https://www.youtube.com/watch?v=1uN8cBGVpfs
SAMPLE PROGRAM
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{ float x, xn, f, df; int y=0;
cout<<fixed<<setprecision(3);
cout << "Function: 2x^3 - 3x^2 + 2x - 6 = 0" <<endl;
cout << "\nEnter guess value of x: "; //prompts user to input starting value
cin >> x; //assigns inputted starting value to x
cout<<"\nx\txn"<<endl; //header
while(y<15)
{ f = ((2*x*x*x)-(3*x*x)+(2*x)-6); //function to solve
df = ((6*x*x)-(6*x)+2); //derivative of function
xn=x-(f/df); //Newton's method
cout<<x<<"\t"<<xn<<endl;
if(xn==x)break; //check if solution has arrived
x=xn; //use new x value for next iteration
y++; }
cout<<"\nThe root equals to "<<x<<" and is found after "<<y<<" iterations."<<endl;
return 0;}
x xn
1.000 3.500
3.500 2.583
2.583 2.070
2.070 1.872
1.872 1.842
1.842 1.842
1.842 1.842
1.842 1.842
------------------------------------------------------------------------------------------
Function: 2x^3 - 3x^2 + 2x - 6 = 0
x xn
25.000 16.833
16.833 11.388
11.388 7.762
7.762 5.351
5.351 3.761
3.761 2.743
2.743 2.150
2.150 1.894
1.894 1.843
1.843 1.842
1.842 1.842
1.842 1.842