TechSapphire Friday, 2024-10-11, 11:45 PM
Site menu
Login form
News
Play Games
  • Deep Freeze
  • Ice Slide
  • Gyroball
  • Fat Fish
  • Bush Royal Rampage
  • Question : Given three ints, a b c, print true if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more. 
    Note : Math.abs(num) computes the absolute value of a number.
    Output:
    1, 2, 10 -> true
    1, 2, 3  -> false
    4, 1, 3  -> true

    Answer:

    import java.util.*;
    import java.lang.Math;
    public class CloseFar
    {
     public static void main(String[] args) {
     Scanner sc=new Scanner(System.in); 
     System.out.println("Enter num 1 : "); 
     int a=sc.nextInt(); 
     System.out.println("Enter num 2 : "); 
     int b=sc.nextInt(); 
     System.out.println("Enter num 3 : "); 
     int c=sc.nextInt(); 
     if(Math.abs(b-a)<=1 || Math.abs(c-a)<=1)
     {
     System.out.println("true"); 
     }
     else 
     {
     System.out.println("false"); 
     }
     }
     }

    Click to download

    Categories
    Programming [27]
    Tips for programming
    Security [2]
    Security Tips
    Google [1]
    Use google faster then ever you use
    Project [14]
    HTML [2]
    Electronics [0]
    Data Structure [0]
    Database [16]
    SQL SERVER
    SSRS [1]
    Sql Server Reporting Services
    Copyright MyCorp © 2024