0% found this document useful (0 votes)
5 views16 pages

Determine Relationships and Transformations

Uploaded by

mazenkhattab08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Determine Relationships and Transformations

Uploaded by

mazenkhattab08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problems

1) You are given the names of two people. Each person has a first name
and a second name.

Your task is to determine whether the two people are brothers. Two people
are considered brothers if and only if they share the same second name.

Input
The first line contains two strings: the first name and the second name
The second line contains two strings: the first name and the second name

Output
Print "ARE Brothers" if the two people have the same second name.
Otherwise, print "NOT".

Input Output

Bassam Ramadan Are brothers


Ahmed Ramadan

Ali Salah Are brothers


Ayman Salah

ali kamel Not


ali salah

—-----------------------------------------------------------------------------------------------------------
—-----------------------------------------------------------------------------------------------------------
2) You are given a single letter X. The letter can either be in lowercase or
uppercase.

● If the letter is lowercase, convert it to uppercase and print it.


● If the letter is uppercase, convert it to lowercase and print it.

Input
The input consists of one character, either an uppercase or a lowercase letter.

Output
Output the letter after converting it to the opposite case (lowercase to uppercase
or uppercase to lowercase).

Input Output

a A

A a

Notes :
The difference between the ASCII values of a lowercase letter and its corresponding
uppercase letter is 32.
A == 65 and a == 97
B == 66 and b == 98

—-----------------------------------------------------------------------------------------------------------
—-----------------------------------------------------------------------------------------------------------
3) You are given a person's age in days. Your task is to convert this age into
years, months, and days.

● Consider that a year has exactly 365 days.


● Consider that a month has exactly 30 days.

Input
The input consists of a single integer N (0 ≤ N ≤ 10^6), representing a person's age in days.

Output
The number of years, followed by the word "years".
The number of months, followed by the word "months".
The number of days, followed by the word "days".

Input Output

400 1 years
1 months
5 days

800 2 years
2 months
10 days

30 0 years
1 months
0 days

—-----------------------------------------------------------------------------------------------------------
—-----------------------------------------------------------------------------------------------------------
4) You are given a number N. You need to determine whether it is a float or
an integer.

Input
The input consists of one number N, which can either be a float or an integer.
The value of N will be between 1 and 1000 (inclusive).

Output

● If N is a float, print "float" followed by its integer part and decimal part separated by a
space.
● If N is an integer, print "int" followed by the integer part only.

Input Output

234.000 int 234

534.958 float 534 0.958

—-----------------------------------------------------------------------------------------------------------
—-----------------------------------------------------------------------------------------------------------
5) You are given a symbol and a list of integers. Your task is to print the symbol
repeated a specific number of times based on the integers in the list.

Input
The first line contains the symbol S (either +, -, *, or /).
The second line contains an integer N (1 ≤ N ≤ 50).
The third line contains N integers X1, X2, ..., Xn where 1 ≤ Xi ≤ 100.

Output
For each number Xi in the third line, print the symbol S repeated Xi times,
one per line. Do not print any extra spaces after the symbol.

Input Output

+ +++++
5 ++
52437 ++++
+++
+++++++

Notes :
The first line contains the symbol "+".
The second line contains 5, meaning there are 5 numbers in the third line.
The third line contains the numbers 5 2 4 3 7.

● For the number 5, print "+" repeated 5 times: "+++++".


● For the number 2, print "+" repeated 2 times: "++".
● For the number 4, print "+" repeated 4 times: "++++".
● For the number 3, print "+" repeated 3 times: "+++".
● For the number 7, print "+" repeated 7 times: "+++++++".

—-----------------------------------------------------------------------------------------------------------
—-----------------------------------------------------------------------------------------------------------
6) You are given several test cases, each containing a number. For each number,
your task is to print the digits of that number from right to left,
separated by a space

Input
The first line contains an integer T (1 ≤ T ≤ 10), which represents the number of test cases.
The following T lines each contain a single number N (0 ≤ N ≤ 10^9).

Output
For each test case, print the digits of the number N in reverse order (from right to left),
with each digit separated by a space.

Input Output

4 121
121 93
39 654321
123456 0021
1200

Notes :
For the first number 121:
The digits from right to left are 1, 2, 1. So the output is: 1 2 1.
For the second number 39:
The digits from right to left are 9, 3. So the output is: 9 3.
For the third number 123456:
The digits from right to left are 6, 5, 4, 3, 2, 1. So the output is: 6 5 4 3 2 1.
For the fourth number 1200:
The digits from right to left are 0, 0, 2, 1. So the output is: 0 0 2 1.

—-----------------------------------------------------------------------------------------------------------
—-----------------------------------------------------------------------------------------------------------
7) You will be given multiple pairs of numbers, N and M. For each pair:
1- Print the numbers between N and M in ascending order (from the smaller
number to the larger one).
2- Print the sum of those numbers.

The program should stop as soon as either N or M is less than or equal to zero.
When this happens, stop processing and do not print anything for that pair.

Input
You will be given multiple pairs of integers N and M.
Each pair is given on a separate line.
The program ends when you encounter a pair where either N or M is less than or equal to zero.

Output
For each valid pair of integers N and M:
Print the numbers between N and M in ascending order, separated by a space.
Then print the message sum = followed by the sum of the numbers.
If either N or M is less than or equal to zero, stop and do not print anything for
that pair.

Input Output

5 2 2 3 4 5 sum =14
5 7 5 6 7 sum =18
5 -1

5 2 2 3 4 5 sum =14
6 3 3 4 5 6 sum =18
5 0

Notes :
For the first line 5 2:
○ The numbers between 2 and 5 (inclusive) are 2 3 4 5.
○ The sum of these numbers is 14.
○ The output is: 2 3 4 5 sum =14.
For the second line 5 7:
○ The numbers between 5 and 7 (inclusive) are 5 6 7.
○ The sum of these numbers is 18.
○ The output is: 5 6 7 sum =18.
For the third line 5 -1:
○ Since -1 is less than zero, the program stops reading input and does not print
anything for this test case.
8) You are given a number N which represents the number of rows in a pyramid.
The task is to print a pyramid with N rows using the * symbol.

Input
The input consists of a single integer N (1 ≤ N ≤ 99), representing the number of rows
in the pyramid.

Output
For each row i (where 1 ≤ i ≤ N), print the correct number of stars (*), centered.
The rows must be aligned, so the stars in each row should be centered.

Input Output

4 *
***
*****
*******

3 *
***
*****
Solutions

1) You are given the names of two people. Each person has a first name
and a second name.
#include <iostream>
#include <string>
using namespace std;

int main()
{
string F1, S1, F2, S2;

cin >> F1 >> S1;


cin >> F2 >> S2;

if (S1 == S2)
{
cout << "ARE Brothers" << endl;
}
else
{
cout << "NOT" << endl;
}

return 0;
}
2) You are given a single letter X. The letter can either be in lowercase or
uppercase.
#include <iostream>
using namespace std;

int main()
{
char X;
cin >> X;

if (X >= 'a' && X <= 'z')


{
X = X - 32;
}
else if (X >= 'A' && X <= 'Z')
{
X = X + 32;
}

cout << X << endl;

return 0;
}

And
#include <iostream>
using namespace std;

int main()
{
char x;
cin >> x;

x = (x >= 'A' && x <= 'Z') ? x+32 :


(x >= 'a' && x <= 'z') ? x-32 : x;

cout << x << endl;

return 0;
}
3) You are given a person's age in days. Your task is to convert this age into
years, months, and days.
#include <iostream>
using namespace std;

int main()
{
int RemainingDays, Years, Months, Days;
cin >> RemainingDays;

Years = RemainingDays / 365;


RemainingDays = RemainingDays % 365;

Months = RemainingDays / 30;


RemainingDays = RemainingDays % 30;

Days = RemainingDays;

cout << Years << " years" << endl;


cout << Months << " months" << endl;
cout << Days << " days" << endl;

return 0;
}
4) You are given a number N. You need to determine whether it is a float or
an integer.
#include <iostream>
using namespace std;

int main()
{
double num, diff;
int value;

cin >> num;

value = num;
diff = num-value;

if(diff==0)
cout << "int " << value << "\n";
else
cout << "float " << value << " " << diff << "\n";

return 0;
}
5) You are given a symbol and a list of integers. Your task is to print the symbol
repeated a specific number of times based on the integers in the list.
#include <iostream>
using namespace std;

int main()
{
char ch;
int N;
int X;

cin >> ch;


cin >> N;

for (int i = 0; i < N; i++)


{
cin >> X;

for (int j = 0; j < X; j++)


cout << ch;

cout << endl;


}

return 0;
}
6) You are given several test cases, each containing a number. For each number,
your task is to print the digits of that number from right to left,
separated by a space
#include <iostream>
using namespace std;

int main()
{
int T;
cin >> T;

while (T--)
{
int N;
cin >> N;

if (N == 0)
{
cout << "0" << endl;
}

while (N > 0)
{
cout << N % 10 << " ";
N /= 10;
}

cout << endl;


}

return 0;
}
7) You will be given multiple pairs of numbers, N and M. For each pair:
1- Print the numbers between N and M in ascending order (from the smaller
number to the larger one).
2- Print the sum of those numbers.

The program should stop as soon as either N or M is less than or equal to zero.
When this happens, stop processing and do not print anything for that pair.
#include <iostream>
using namespace std;

int main()
{
int N, M, Sum;

while (cin >> N >> M)


{
if (N <= 0 || M <= 0)
break;

Sum = 0;

for (int i = min(N, M); i <= max(N, M); i++)


{
cout << i << " ";
Sum += i;
}

cout << "Sum =" << Sum << endl;


}

return 0;
}
8) You are given a number N which represents the number of rows in a pyramid.
The task is to print a pyramid with N rows using the * symbol.
#include <iostream>
using namespace std;

int main()
{
int N;
cin >> N;

for (int i = 1; i <= N; i++)


{
for (int j = 0; j < N - i; j++)
cout << " ";

for (int j = 0; j < 2 * i - 1; j++)


cout << "*";

cout << endl;


}

return 0;
}

You might also like