0% found this document useful (0 votes)
429 views

C++ Exercises I

The document contains solutions to 5 programming exercises: 1. A program that asks for the width and length of a rectangle and calculates the area and perimeter. 2. A program that asks the user to input 5 integers and calculates the average using only 2 variables. 3. A program that exchanges the values of two variables A and B entered by the user. 4. A program that asks for the price of tomatoes without tax, quantity, and tax percentage and calculates the total price. 5. A program that asks for the coordinates of two points and calculates the distance between them.

Uploaded by

Zaid Al-Ali
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
429 views

C++ Exercises I

The document contains solutions to 5 programming exercises: 1. A program that asks for the width and length of a rectangle and calculates the area and perimeter. 2. A program that asks the user to input 5 integers and calculates the average using only 2 variables. 3. A program that exchanges the values of two variables A and B entered by the user. 4. A program that asks for the price of tomatoes without tax, quantity, and tax percentage and calculates the total price. 5. A program that asks for the coordinates of two points and calculates the distance between them.

Uploaded by

Zaid Al-Ali
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXERCISE 1

Write a program that asks the user to type the width and the length of a rectangle and then outputs to the screen the area and the perimeter of that rectangle. Solution #1
#include <iostream> using namespace std; int main() { double area ,width,longer, perimeter; cout<<"Type the width : ";cin>>width; cout<<"Type the longer : ";cin>>longer; area=width*longer; perimeter=2*(width+longer); cout<<"The area is : "<<area<<endl; cout<<"The perimeter is : "<<perimeter<<endl; return 0; }

EXERCISE 2
Write a program that asks the user to type 5 integers and writes the average of the 5 integers. This program can use only 2 variables. Solution #1
#include<iostream> using namespace std; int main() { int a;double s=0; cout<<"Type cout<<"Type cout<<"Type cout<<"Type cout<<"Type integer integer integer integer integer 1 2 3 4 5 : : : : : ";cin>>a;s=s+a; ";cin>>a;s=s+a; ";cin>>a;s=s+a; ";cin>>a;s=s+a; ";cin>>a;s=s+a;

s=s/5.0; cout<<"The average is : "<<s<<endl; return 0; }

EXERCISE 3
Write a program that asks the user to type 2 integers A and B and exchange the value of A and B. Solution Solution #
#include<iostream> using namespace std; int main() { double a,b,temp; cout<<"Type the value of a : "; cin>>a; cout<<"Type the value of b : "; cin>>b; temp=a; a=b; b=temp; cout<<"The value of a is "<<a<<endl; cout<<"The value of b is "<<b<<endl; return 0; }

EXERCISE 4
Write a program that asks the user to type the price without tax of one kilogram of tomatoes,the number of kilograms you want to buy and the tax in percent units. The program must write the total price including taxes. Solution #
#include<iostream> using namespace std; int main() { double price_without_taxes,weight,taxes,total; cout<<"Type the price of 1 kg of tomatoes without taxes : "; cin>>price_without_taxes; cout<<"How much tomatoes do you want (in kilogram) : "; cin>>weight; cout<<"What is the tax in percent : "; cin>>taxes; total= ((((price_without_taxes*taxes)/100)+ price_without_taxes)*weight); cout<<"The total price is : "<<total<<endl;

return 0; }

EXERCISE 5
Write a program that asks the user to type the coordinate of 2 points, A and B (in a plane), and then writes the distance between A and B. Solution Solution #1:
#include<iostream> using namespace std; #include<cmath> int main() { double XA,YA,XB,YB,dx,dy,distance; cout<<"Type cin>>XA; cout<<"Type cin>>YA; cout<<"Type cin>>XB; cout<<"Type cin>>YB; the abscissa of A : "; the ordinate of A : "; the abscissa of B : "; the ordinate of B : ";

dx=XA-XB; dy=YA-YB; distance=sqrt(dx*dx+dy*dy); cout<<"The distance AB is : "<<distance<<endl; return 0; }

Made by : Zaid Al-Ali [email protected]

You might also like