hpilo

Chapter3_Logic_Ex2

Dec 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*  ==================================================
  3.       Chapter 3: Logical and conditional expressions
  4.  
  5.                     Ex2: Ax+B=0
  6.     ===================================================
  7. */
  8.  
  9. public class MyProgram {
  10.     public static void main(String[] args) {
  11.        
  12.         //variables
  13.         int a,b;
  14.         float x;
  15.         Scanner s=new Scanner(System.in);
  16.        
  17.         //user input
  18.         System.out.println("Solution for Ax+B=0");
  19.         System.out.println("Enter A: ");
  20.         a=s.nextInt();
  21.         System.out.println("Enter B: ");
  22.         b=s.nextInt();
  23.        
  24.         // X = NO solution
  25.         if(a==0 && b!=0)
  26.             System.out.println("X = NO solution");
  27.  
  28.        else if(b==0) {
  29.  
  30.             // X = infinity solution
  31.             if (a == 0)
  32.                 System.out.println("X = infinity solutions");
  33.             else
  34.                 // X = 0
  35.                 System.out.println("X = 0");
  36.         }
  37.  
  38.         else{
  39.             //X = one solution
  40.             x=(-1*((float)b/a));
  41.             System.out.println("X = "+ x);
  42.  
  43.             //option 2:
  44.             //System.out.println("X= -"+b+"/"+a);
  45.         }
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment