C++ Basics - Practice
Muhammad Magdi
What is an Online Judge?
Is an online system that tests the correctness of the solutions of programming problems.
Square Area
Write a C++ Program such that, given an integer number 's' representing the side length of a square prints its area.
S
Square Area
[Solution]
#include <iostream>
using namespace std;
int main() {
int s;
cin >> s;
cout << s*s << endl;
return 0;
}
URI 1001 - Extremely Basic [Solution]
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int x = a + b;
cout << "X = " << x << endl;
return 0;
}
URI 1001 - Extremely Basic [Notes]
- Make sure to follow the problem description!
- You must print the message "X = "
- Before the sum answer.
- With capital x.
- With a space before and after the equal sign.
- Followed by an end of line ( endl ).
- Otherwise your solution won't be correct.
URI 1004 - Simple Product
[Solution]
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << "PROD = " << a*b << endl;
return 0;
}
URI 1004 - Simple Product
[Notes]
- We could save the value a*b in a variable before printing it, both ways are correct.
- You must print the message "PROD = "
- Before the product answer.
- With all capital letters.
- With a space before and after the equal sign.
- Followed by an end of line ( endl ).
- Otherwise your solution won't be correct.
int c = a*b;
cout << "PROD = " << c << endl;
URI 1013 - The Greatest
[Incomplete Solution]
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int max = (a + b + abs(a - b)) / 2;
cout << max << endl;
return 0;
}
URI 1013 - The Greatest
[Notes]
- Note that we added a new library in our code called cmath using #include.
- cmath is used here to tell the compiler how abs works.
- abs(5) = 5, abs(-5) = 5, abs(3) = 3, abs(-3) = 3, ...
#include <cmath>
Assignment
Thanks
Content
- URI Online Judge
- Square Area
- Extremely Basic
- Simple Product
- The Greatest
C++ Basics - Practice
By Muhammad Magdi
C++ Basics - Practice
- 147