Computer Science, asked by heartbeats96, 5 months ago

WATERMELON
One hot summer day Peter and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.
Peter and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. But the difference between the 2 parts should be minimal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight. Write an algorithm, draw flowchart and python program.

 Input Format
The first (and the only) input line contains integer number w (1 ≤ w ≤ 100) — the weight of the watermelon bought by the boys.
 Output Format
If the input value is not within the range, print "Invalid Input".
In the first line of the output, print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case.
If the first line of the output is YES, the next line of the output consists of 2 integers separated by a space. In case of distinct integers, the smallest number should appear first.
 Sample Input 1
8
 Sample Output 1
YES
4 4 
Sample Input 2
11
 Sample Output 2
NO 
Sample Input 3
124
 Sample Output 3
Invalid Input​

Answers

Answered by dreamrob
0

Algorithm:

Step 1: Start

Step 2: Take input in the variable weight from the user

Step 3: Check if weight is less than 1 or greater than 100. If true,

Print "Invalid Input"

Step 4: If the above condition is false then check if weight%2 != 0. If true,

Print "NO"

Step 5: If the above condition is also false, then move to else block.

  1. Print "YES"
  2. x = (int)(weight / 2)
  3. Check if x % 2 == 0. If true, Print (x, " ", x)
  4. Else print (x-1, " ", x+1)

Step 6: End

Program:

weight = int(input("Enter the weight of the watermelon : "))

if weight < 1 or weight > 100:

   print("Invalid Input")

elif weight % 2 != 0:

   print("NO")

else:

   print("YES")

   x = (int)(weight / 2)

   if x % 2 == 0:

       print(x, " ", x)

   else:

       print(x-1, " ", x+1)

Output 1:

Enter the weight of the watermelon : 8

Yes

4   4

Output 2:

Enter the weight of the watermelon : 11

NO

Output 3:

Enter the weight of the watermelon : 124

Invalid Input

Attachments:
Answered by saidurrahmantuhin
0

Explanation:

My current attempt is:

#include <stdio.h>

int main () {

int w,x,y;

printf("Enter The Weight");

scanf("%d" , &w) ;

for(y=2;y<=w;y=y+2) {

x=w-y;

if(x%2==0) {

for(x;x>=y;x=w-y) {

y=y+2 ;

printf("Yes,It can divide to %d", x);

printf("and %d\n", y-2);

}

} else {

printf("No");

}

}

return 0;

}

Similar questions