Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /* ==================================================
- Chapter 3: Logical and conditional expressions
- Ex2: Ax+B=0
- ===================================================
- */
- public class MyProgram {
- public static void main(String[] args) {
- //variables
- int a,b;
- float x;
- Scanner s=new Scanner(System.in);
- //user input
- System.out.println("Solution for Ax+B=0");
- System.out.println("Enter A: ");
- a=s.nextInt();
- System.out.println("Enter B: ");
- b=s.nextInt();
- // X = NO solution
- if(a==0 && b!=0)
- System.out.println("X = NO solution");
- else if(b==0) {
- // X = infinity solution
- if (a == 0)
- System.out.println("X = infinity solutions");
- else
- // X = 0
- System.out.println("X = 0");
- }
- else{
- //X = one solution
- x=(-1*((float)b/a));
- System.out.println("X = "+ x);
- //option 2:
- //System.out.println("X= -"+b+"/"+a);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment