CPP All : Building Expressions and Formulas
Concept Learning Code Sheets

#### Building speed formula for different units of measurement #1822

Formula building can be done for different units using different divisors.

#include <iostream>
using namespace std;
int main()
{
float d,t,s;
cout<<"Distance travelled (in meters):";
cin>>d;
cout<<"Time taken (in seconds):";
cin>>t;
s=d/t;
cout<<"Speed (in m/s) = "<<s<<endl;
s=(d/1000)/(t/3600);
cout<<"Speed (in km/hr) = "<<s<<endl;
return 0;
}

#### Formulas with powers and roots #1824

Creating formulas that contain powers and square root.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double a,b,c;
a=3.23;
b=4.25;
c=sqrt(pow(a,2)+pow(b,2));
cout<<"c = "<<c<<endl;
return 0;
}

#### Simple interest and amount formulation #1826

Calculating simple interest and amount by using typical formulas.

#include <iostream>
using namespace std;
int main()
{
float p,r,t,si;
cout<<"Principal amount:";
cin>>p;
cout<<"Rate of interest (%):";
cin>>r;
cout<<"Time (years):";
cin>>t;
si=p*r*t/100;
cout<<"Simple Interest = "<<si<<endl;
cout<<"Final Amount = "<<p+si<<endl;
return 0;
}

#### Finding area and circumference of a circle #1828

Area and circumference of circle with use of constant value of PI

#include <iostream>
using namespace std;
int main( )
{
const float PI = 3.14159;
float radius; //float = 4 bytes, 7 digit precision
double area; //double = 8 bytes, 15 digit precision
long double circum; //long double = 10 bytes, 19 digit precision
cout << "Area would be : " << area <<endl;
cout<< "Circumference would be : " << circum <<endl;
return 0 ;
}

#### Swapping two variables using temporary third variable. #2493

Swapping of value between two integer variables using the intermediate third variable.

#include <iostream>
using namespace std;
int main()
{
int a,b,temp;
cout<<"Enter two values a & b:";
cin>>a>>b;
cout<<endl<<"You have entered:"<<endl;
cout<<"a="<<a<<" b="<<b<<endl;
temp=a;
a=b;
b=temp;
cout<<endl<<"After swapping:"<<endl;
cout<<"a="<<a<<" b="<<b<<endl;
return 0;
}

#### Swapping two variables without using third variable. #2497

Swapping of value between two integer variables without using the intermediate third variable.

#include <iostream>
using namespace std;
int main()
{
int a,b,temp;
cout<<"Enter two values a & b:";
cin>>a>>b;
cout<<endl<<"You have entered:"<<endl;
cout<<"a="<<a<<" b="<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<endl<<"After swapping:"<<endl;
cout<<"a="<<a<<" b="<<b<<endl;
return 0;
}

#### Area of Triangle using base and height #1787

Finding the area of a triangle when base and height is given

#include <iostream>
using namespace std;
int main()
{
float a,b,h;
cout<<"Enter base and height of triangle:";
cin>>b>>h;
a=b*h/2; //do not write 1/2*b*h
cout<<"Area of triangle = "<<a<<endl;
return 0;
}

#### Simple expression forming #1820

Forming expressions with simple variables.

#include <iostream>
using namespace std;
int main()
{
float a=1.3,b=2.5,c;
c=a+(a+b)/a*b;
cout<<"c = "<<c<<endl;
return 0;
}

Solved Problems

#### Average of three integer numbers, double output #2331

Find average of three integer numbers given by the user. The output can have

#### Finding Errors #2997

Find errors in the following program and write the program with correct synt

#### multiples of an integer #3070

Write a program to display 2nd, 4th and 6th multiple of the input integer num

Code Sheets:8  Solved Problems:3
×