Program To Read A Number From The User and Print Whether It
Program To Read A Number From The User and Print Whether It
Program to read a number from the user and print whether it is positive or
negative.
Code:
import java.util.Scanner;
if (num > 0) {
} else if (num == 0) {
System.out.println("Zero");
} else {
Output:
2. Program to solve quadratic equations (use if, else if and else).
Code:
import java.util.Scanner;
System.out.print("a= ");
double a = number.nextDouble();
System.out.print("b= ");
double b = number.nextDouble();
System.out.print("c= ");
double c = number.nextDouble();
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
} else if (discriminant == 0) {
} else {
Output:
3. Take three numbers from the user and print the greatest number.
Code:
import java.util.Scanner;
greatestNumber = num2;
greatestNumber = num3;
Output:
4. Program that keeps a number from the user and generates an integer
between 1 and 7 and displays the name of the weekday.
Code:
import java.util.Scanner;
if (num > 0) {
} else if (num == 0) {
System.out.println("Zero");
} else {
}}}
Output:
5. Program that reads in two floating-point numbers and tests whether they
are the same up to three decimal places.
Code:
import java.util.Scanner;
if (no1Int==no2Int) {
} else {
Output:
6. Program that takes a year from user and print whether that year is a leap
year or not.
Code:
import java.util.Scanner;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
} else {
} else {
} else {
Output:
Code:
public class NaturalNumbers {
System.out.println(i);
}
Output:
8. Program to input 5 numbers from keyboard and find their sum and
average.
Code:
import java.util.Scanner;
int sum = 0;
double average;
sum += num;
Output:
Code:
import java.util.Scanner;
Output:
10. Program in Java to display the pattern like right angle triangle with a
number.
Input number of rows : 5
Expected Output :
12
123
1234
12345
Code:
import java.util.Scanner;
System.out.print(j);
System.out.println();
Output:
Hemanth k
Vu22csen0100711