[go: up one dir, main page]

0% found this document useful (0 votes)
253 views13 pages

Import Public Class Exercise1: Java Util Scanner

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

import java.util.

Scanner;

public class Exercise1 {

public static void main(String[] args)

Scanner in = new Scanner(System.in);

System.out.print("Input number: ");

int input = in.nextInt();

if (input > 0)

System.out.println("Number is positive");

else if (input < 0)

System.out.println("Number is negative");

else

System.out.println("Number is zero");

Copy

Sample Output:
Input number: 35
Number is positive
Flowchart:

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Java Conditional Statement Exercises


Next: Write a Java program to solve quadratic equations (use if, else if and else).

What is the difficulty level of this exercise?


EASY MEDIUM HARD
Based on 763 votes, average difficulty level of this exercise is Easy .
 New Content published on w3resource :

 Python Itertools exercises

 Python Numpy exercises

 Python GeoPy Package exercises

 Python Pandas exercises

 Python nltk exercises

 Python BeautifulSoup exercises

 Form Template

 Composer - PHP Package Manager

 PHPUnit - PHP Testing

 Laravel - PHP Framework

 Angular - JavaScript Framework

 React - JavaScript Library

 Vue - JavaScript Framework

 Jest - JavaScript Testing Framework

by Taboola

Sponsored Links

You May Like

There are thousands of Canadian jobs available!Canada Immigration Express

Filipinos can earn from investing. Here's howinvesting-news-now


9 minutes of using it daily will melt extra weight nextfutstep.life

Iloilo is actually full of single guys. Check them out on this premium
dating siteDating.com

    

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0


Unported License.

©w3resource.com 2011-2020

 Privacy
 About
 Contact
 Feedback
 Advertise

import java.util.Scanner;

public class Exercise2 {

public static void main(String[] Strings) {

Scanner input = new Scanner(System.in);

System.out.print("Input a: ");

double a = input.nextDouble();

System.out.print("Input b: ");
double b = input.nextDouble();

System.out.print("Input c: ");

double c = input.nextDouble();

double result = b * b - 4.0 * a * c;

if (result > 0.0) {

double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);

double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);

System.out.println("The roots are " + r1 + " and " + r2);

} else if (result == 0.0) {

double r1 = -b / (2.0 * a);

System.out.println("The root is " + r1);

} else {

System.out.println("The equation has no real roots.");

Copy

Sample Output:
Input a: 1
Input b: 5
Input c: 2
The roots are -0.4384471871911697 and -4.561552812808831
Flowchart:
Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Program to check positive or negative number


Next: Write a Java program to to find the largest of three numbers.

What is the difficulty level of this exercise?


EASY MEDIUM HARD
Based on 486 votes, average difficulty level of this exercise is Medium .

 New Content published on w3resource :

 Python Itertools exercises

 Python Numpy exercises

 Python GeoPy Package exercises

 Python Pandas exercises

 Python nltk exercises

 Python BeautifulSoup exercises

 Form Template

 Composer - PHP Package Manager

 PHPUnit - PHP Testing

 Laravel - PHP Framework

 Angular - JavaScript Framework

 React - JavaScript Library

 Vue - JavaScript Framework

 Jest - JavaScript Testing Framework

    

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0


Unported License.

©w3resource.com 2011-2020
 Privacy
 About
 Contact
 Feedback
 Advertise

import java.util.Scanner;

public class Exercise3 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Input the 1st number: ");

int num1 = in.nextInt();

System.out.print("Input the 2nd number: ");

int num2 = in.nextInt();

System.out.print("Input the 3rd number: ");

int num3 = in.nextInt();

if (num1 > num2)

if (num1 > num3)

System.out.println("The greatest: " + num1);


if (num2 > num1)

if (num2 > num3)

System.out.println("The greatest: " + num2);

if (num3 > num1)

if (num3 > num2)

System.out.println("The greatest: " + num3);

Copy

Sample Output:
Input the 1st number: 25
Input the 2nd number: 78
Input the 3rd number: 87
The greatest: 87
Flowchart:
Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to solve quadratic equations (use if, else if and
else).
Next: Print a floating-point number in a specified format

What is the difficulty level of this exercise?


EASY MEDIUM HARD
Based on 280 votes, average difficulty level of this exercise is Easy .

 New Content published on w3resource :

 Python Itertools exercises

 Python Numpy exercises

 Python GeoPy Package exercises

 Python Pandas exercises

 Python nltk exercises

 Python BeautifulSoup exercises

 Form Template

 Composer - PHP Package Manager

 PHPUnit - PHP Testing

 Laravel - PHP Framework

 Angular - JavaScript Framework

 React - JavaScript Library

 Vue - JavaScript Framework

 Jest - JavaScript Testing Framework


by Taboola

Sponsored Links

You May Like

There are thousands of Canadian jobs available!Canada Immigration Express

9 minutes of using it daily will melt extra weight nextfutstep.life

Iloilo is actually full of single guys. Check them out on this premium
dating siteDating.com

Gathering Place for 40+ Singles in IloiloDatemyage.com

    

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0


Unported License. public class Exercise10 {X

public static void main(String[] args)


{
int i;
System.out.println ("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
{
System.out.println (i);
}
System.out.println ("\n");
}
}

Copy

Sample Output:
©w3resource.com 2011-2020

 Privacy
 About
 Contact
 Feedback
 Advertise

You might also like