Computer Science, asked by antara03dhn, 1 month ago

Solve for if- else and ternary operator 1) Write a program to accept two integer nos. x and y and display their sum if x is greater than y otherwise display their difference.​

Answers

Answered by BrainlyProgrammer
5

Answer:

//The following códes have been made using Java programming.

import java.util.*;

class SumDiff{

public static void main (String ar []){

Scanner sc=new Scanner (System.in);

System.out.println("Enter 2 numbers");

int x=sc.nextInt();

int y=sc.nextInt();

String s=(x>y)?"Sum="+(x+y):"Difference="+(y-x);

System.out.println(s);

}

}

•Required Output Attached

Logic:-

  • Accept 2 numbers
  • Check if x>y, yes? print sum otherwise print difference

Note:-

  • You can also use Math.abs() to get positive difference.
Attachments:
Answered by purveshKolhe
4

 \huge  {\red  {\boxed  {\green  {\boxed  {\mathfrak{answer \:  : }}}}}}

You can use the following program::

import java.util.Scanner;

public class Brainly {

public static void main(String [] args) {

Scanner sc = new Scanner(System.in);

// We are having two variables named x and y.

int x = sc.nextInt();

int y = sc.nextInt();

if (x > y) {

System.out.println(x + y);

}

else {

System.out.println(x - y);

}

}

}

Output::

Let us take x = 7 and y = 5, then it will output : --

12

Now, let us take x = 5 and y = 7, it's output :--

-2

_________________________

Attachments:
Similar questions