Roxana Dumitrescu

C++ Lecture

Here you can find small and easy exercices in order to help you before going to the Praticals. You can fastly compile your code online on this website : write your code, use the compile button and then use the execute button.

Preliminaries

Problem 1

Write a C++ program to print the following lines:
You are a student.
You are learning C++.

#include<iostream>
	
using namespace std;

int main()
{
	cout<<"You are a student"<<endl;
	cout<<"You are learning C++."<<endl;
	
	return 0;
}

Problem 2

Write a C++ program to declare two integers and one float variables; assign 10, 15 and 12.6 to them respectively. Print the values on the screen.

#include<iostream>
	
using namespace std;

int main()
{
	int x=10;
	int y=15;
	float z=12.6;
	cout<<"The value of x is "<<x<<endl;
	cout<<"The value of y is "<<y<<endl;
	cout<<"The value of z is "<<z<<endl;

	return 0;
}

Problem 3

Write a C++ program to prompt the user to input 3 integer values and print these values in forward and reversed order, as shown below.

Please enter your 3 numbers: 12 45 78

Your numbers forward:
12, 45, 78.

Your numbers reversed:
78, 45, 12.
#include<iostream>
	
using namespace std;

int main()
{
	int x,y,z;
    
	cout<<"Please give the fist number "<<endl;
	cin>>x;
	
	cout<<"Please give the second number "<<endl;
	cin>>y;

	cout<<"Please give the third number "<<endl;
	cin>>z;

	cout<<"Print the values in forward order "<<x<<", "<<y<<", "<<z<<"."<<endl;
	cout<<"Print the values in reversed order "<<z<<", "<<y<<", "<<x<<"."<<endl;

	return 0;
}

Problem 4

Write a C++ program to declare 3 variables of type int. Assign the values 10 and 15 to the first two of the variables and assign their product to the third variable. Print on the screen the 3 values.

#include<iostream>
	
using namespace std;

int main()
{
	int x,y,z;

	x=10;
	y=15;
	z=x+y;
	cout<<"The sum of "<<x<<" and "<<y<<" is "<<z;

	return 0;
}

Problem 5

Write a C++ program to compute the maximum of two real numbers.

#include<iostream>
	
using namespace std;

int main()
{
	int a,b,max;

	a=10;
	b=15;

	if (a>=b) max=a;
	else max=b;

	cout<<"The maximum of "<<a<<" and "<<b<<" is "<<max;

	return 0;
}

Problem 6

Write a C++ program to compute the absolute value of a real number.

#include<iostream>
	
using namespace std;

int main()
{
	int a, absolute_value;

	a=-10;

	if (a>=0) absolute_value=a;
		else absolute_value=-a;

	cout<<"The absolute value of "<<a<<" is "<<absolute_value<<endl;

	return 0;
}