Write a program to find whether a given integer is positive or negative.Sample Input 1:Enter the number:45Sample Output 1:45 is a positive numberSample Input 2: Enter the number:-364Sample Output 2:-364 is a negative numberScanner sc = new Scanner(System.in);public static void main(String[] args) {import java.util.*;public class Main {if(num>=0)System.out.println(num+" is a negative number");elseSystem.out.println("Enter the number:");int num=sc.nextInt(); }}System.out.println(num+" is a positive number");
Question
Write a program to find whether a given integer is positive or negative.Sample Input 1:Enter the number:45Sample Output 1:45 is a positive numberSample Input 2: Enter the number:-364Sample Output 2:-364 is a negative numberScanner sc = new Scanner(System.in);public static void main(String[] args) {import java.util.*;public class Main {if(num>=0)System.out.println(num+" is a negative number");elseSystem.out.println("Enter the number:");int num=sc.nextInt(); }}System.out.println(num+" is a positive number");
Solution
It seems like you're trying to write a Java program to determine whether a given integer is positive or negative. However, your code is a bit jumbled. Here's a corrected version:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
int num = sc.nextInt();
if(num >= 0) {
System.out.println(num + " is a positive number");
} else {
System.out.println(num + " is a negative number");
}
}
}
Here's how this program works:
- It first imports the necessary Java utilities.
- It then defines a public class named
Main. - Inside this class, it defines the main method that the Java Virtual Machine (JVM) calls when the program starts.
- It creates a
Scannerobjectscwhich is used to get the input from the user. - It prompts the user to enter a number.
- It reads the input number using
sc.nextInt(). - It checks if the number is greater than or equal to 0. If it is, it prints that the number is positive. If it's not (i.e., it's less than 0), it prints that the number is negative.
Similar Questions
Write a program to check whether a number is positive or negative. Take an integer, num, as an input from the console using input() function and print the result to the console as shown in the sample test case. Sample Input and Output 1: num: 02 positive Sample Input and Output 2: num: -52142 negative Sample Input and Output 3: num: 0 zero
Embark on a numerical adventure with David. Design a program to classify an input integer as positive, negative, or zero. Your goal is to determine the sign of the given number using relational and ternary operators and display the output.Input format :The input consists of an integer n, representing the number to be analyzed.Output format :The output displays the message "The number is [positive/negative/zero]" based on the analysis of the given number.Refer to the sample output for formatting specifications.Code constraints :In the given scenario, the given test cases fall under the following constraints:−106 ≤ n ≤ 106Sample test cases :Input 1 :-9874Output 1 :The number is negativeInput 2 :7456Output 2 :The number is positiveInput 3 :0Output 3 :The number is zeroNote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0
public class TestNumbers{ public static void main(String[] args) { // Get a random number from -10 up to 10. int number = (int) (Math.random() * 21) - 10; System.out.println("The number is " + number); // is it positive? if (number > 0) { System.out.println(number + " is positive!"); } // is it 0? if (number == 0) { System.out.println(number + " is zero!"); } // is it negative? // Add another if statement }}
Thara is attending a math lecture, and her professor asks students to swap two numbers using a bitwise operator. The professor finds it difficult to check the logic of 50 students. So, he decided to write a Java program to get two numbers, first and second, from the user and swap the numbers. He is not good at programming. Help them by developing an application using your Java skills.Note:Use bitwise operator.Negative and zero are also accepted as inputs and numbers have to be swapped accordingly.In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input 1:Enter the numbers8743Sample Output 1:Before swappingfirst = 87, second = 43After swappingfirst = 43, second = 87
Kevin is trying to solve a number puzzle. If the number is greater than fifty, he wants the difference between the digits of the number; otherwise, if the number is less than or equal to fifty, he wants to reverse the number and find the difference in digits. Help Kevin to solve the problem using a Java program.Requirements:The input number should be a two-digit integer, or else display "Invalid number".Note:In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input/Output 1:Enter the number56-1Explanation:56 is greater than 50, so subtract 5 - 6 = -1Sample Input/Output 2:Enter the number484Explanation:48 is less than 50, so reverse 48 to 84 then subtract 8 - 4 = 4Sample Input/Output 3:Enter the number9Invalid number
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.