CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Jeff Jeffy
Level 15
Irvine
  • 09.03.2019
  • 940views
  • 1comment

Pretty sure my math is right

Question about the task Rule of the triangle
Java Syntax,  Level 4,  Lesson 4
Resolved

Use the keyboard to enter three numbers a, b, and c (the lengths of the sides of the proposed triangle).
Determine whether a triangle with these sides can exist.
Display the result as follows:
"The triangle is possible." - if a triangle with these sides could exist.
"The triangle is not possible." - if a triangle with these sides cannot exist.

Hint:
A triangle can exist only if the sum of two of its sides is greater than the third side.
You need to compare each side with the sum of the other two.
If even one side is larger or equal to the sum of the other two sides, then no such triangle exists.

Requirements:
  • The program should read three numbers from the keyboard.
  • The program should display text on the screen according to the task conditions.
  • If a triangle with the specified sides could exist, you need to display: "The triangle is possible."
  • If a triangle with the specified sides cannot exist, you need to display: "The triangle is not possible."
package com.codegym.task.task04.task0415; /* Rule of the triangle */ import java.io.*; import java.util.Scanner; public class Solution { public static void main(String[] args) throws Exception { //write your code here Scanner in=new Scanner(System.in); int a=in.nextInt(); int b=in.nextInt(); int c=in.nextInt(); if ((a+b<c)||(c+b<a)||(a+c<b)){ System.out.println("The triangle is not possible."); } else{ System.out.println("The triangle is possible."); } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Khurram
Level 16 , Lahore, Pakistan
10 March 2019, 07:24solution
if a = 2 , b = 3 and c = 5 a + b = 5 which is equal to (but not greater than) c, the code will mention that the triangle is possible. However it is not. you need to consider the situation where sum of 2 sides is equal to the length of the 3rd side. The triangle should not be possible in such case.
+3
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.