Vous êtes sur la page 1sur 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 Zeezou_tevez@yahoo.com

Vous aimerez peut-être aussi