C++ Exercise 1

Mannan Ul Haq
0

Objective:

  1. Introduction to C++
  2. How to compile C++ code
  3. Simple arithmetic problems solving


Activity 1:

Take two inputs from user (keyboard) length and breadth of rectangle. Compute the area of rectangle given by: area = length*breadth


Solution:

#include<iostream>
using namespace std;

int main()
{
	int length = 0;
	int breadth = 0;
	int area = 0;

	cout << "Enter Length and Breadth of Rectangle";
	cin >> length;
	cin >> breadth;

	area = length * breadth;

	cout << "The Area is = " << area;

	return 0;
}


Activity 2:

Write C++ code to solve the second-degree equation aX^2 + bX + c= 0 for any real numbers a, b and c. The program takes the values of a, b and c as input from the user, and calculates X.

Note: real numbers are taken in float.

Solution:

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

int main()
{
	float a = 0;
	float b = 0;
	float c = 0;
	float X = 0;

	cout << "Enter three Real Numbers a, b and c: ";
	cin >> a;
	cin >> b;
	cin >> c;

	float eq1;
	float sq;
	float eq2;
	float eq3;

	eq1 = (b * b) - (4 * a * c);
	sq = sqrt(eq1);

	eq2 = (-b - sq) / 2 * a;
	cout << "The First value of X is = " << eq2 << endl;

	eq3 = (-b + sq) / 2 * a;
	cout << "The Second value of X is = " << eq3 << endl;

	return 0;
}


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !