- 1
code java
help code jave Write a java program to display the sum of 2 numbers a and b if a>b and their product if a<=b.
4 Answers
+ 1
yousef481 What is ConsoleInput is Java? I never heard about this.
Try this logic
import java.util.Scanner;
class Xyz {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int a = sc.nextInt();
int b = sc.nextInt();
if (a < 0 || b < 0) {
System.out.println("Enter positive number!");
return;
}
if (a > b) {
System.out.println("Sum of " + a + " & " + b + " = " + (a + b));
} else {
System.out.println("Multiplication of " + a + " & " + b + " = " + (a * b));
}
}
}
+ 1
Hi ,go through basics of java or if you have written any code so far , share the link to it and we will help you .
+ 1
yousef481 check the following code and sorry but i don't know about the methods you used for input .
import java.util.*;
public class Program
{
public static void main(String[] args)
{
int a;
int b;
int sum;
int product;
System.out.print("Enter two numbers to add\n");
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
b=sc.nextInt();
if(a>b){
sum = a + b;
System.out.printf("Sum of the numbers = %d\n", sum);
}
else if(a<=b){
product=a*b;
System.out.printf("Product of the numbers = %d\n", product);
}
}
}