Program To Read A Number From The User and Print Whether It

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

1.

Program to read a number from the user and print whether it is positive or
negative.

Code:

import java.util.Scanner;

public class positive_negative {

public static void main(String[] args) {

Scanner number = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = number.nextInt();

if (num > 0) {

System.out.println("It's a Positive number");

} else if (num == 0) {

System.out.println("Zero");

} else {

System.out.println("It's a Negative number");

Output:
2. Program to solve quadratic equations (use if, else if and else).

Code:
import java.util.Scanner;

public class QuadraticEquation {

public static void main(String[] args) {

Scanner number = new Scanner(System.in);

System.out.println("Enter the coefficients of the quadratic equation: ");

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) {

double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);

double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);

System.out.println("Roots are real and different.");

System.out.println("Root 1= " + root1);

System.out.println("Root 2= " + root2);

} else if (discriminant == 0) {

double root = -b / (2 * a);

System.out.println("Roots are real and same.");

System.out.println("Root= " + root);

} else {

double realPart = -b / (2 * a);

double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);

System.out.println("Roots are complex and imaginary.");

System.out.println("Root 1: " + realPart + " + " + imaginaryPart + "i");

System.out.println("Root 2: " + realPart + " - " + imaginaryPart + "i");

Output:
3. Take three numbers from the user and print the greatest number.

Code:
import java.util.Scanner;

public class Greatest {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");

int num1 = scanner.nextInt();

System.out.print("Enter the second number: ");

int num2 = scanner.nextInt();

System.out.print("Enter the third number: ");

int num3 = scanner.nextInt();

int greatestNumber = num1;


if (num2 > greatestNumber) {

greatestNumber = num2;

if (num3 > greatestNumber) {

greatestNumber = num3;

System.out.println("The greatest number is: " + greatestNumber);

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;

public class positive_negative {

public static void main(String[] args) {

Scanner number = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = number.nextInt();

if (num > 0) {

System.out.println("It's a Positive number");

} else if (num == 0) {

System.out.println("Zero");

} else {

System.out.println("It's a Negative number");

}}}

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;

public class DecimalValue {

public static void main(String[] args) {

Scanner number = new Scanner(System.in);

System.out.print("Enter a floating-point number: ");

float no1 = number.nextFloat();

System.out.print("Enter another floating-point number: ");

float no2 = number.nextFloat();

int no1Int = (int) (no1 * 1000);

int no2Int = (int) (no2 * 1000);

if (no1Int==no2Int) {

System.out.println("They are same.");

} else {

System.out.println("They are different.");

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;

public class LeapYear {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a year: ");

int year = scanner.nextInt();

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0) {

System.out.println(year + " is a leap year.");

} else {

System.out.println(year + " is not a leap year.");

} else {

System.out.println(year + " is a leap year.");


}

} else {

System.out.println(year + " is not a leap year.");

Output:

7. Program to display the first 10 natural numbers.

Code:
public class NaturalNumbers {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++) {

System.out.println(i);

}
Output:

8. Program to input 5 numbers from keyboard and find their sum and
average.

Code:
import java.util.Scanner;

public class SumAverage {

public static void main(String[] args) {

Scanner number = new Scanner(System.in);

int sum = 0;

double average;

System.out.println("Enter 3 numbers: ");


for (int i = 0; i < 3; i++) {

int num = number.nextInt();

sum += num;

average = (double) sum / 3;

System.out.println("Sum: " + sum);

System.out.println("Average: " + average);

Output:

9. Program in Java to display the multiplication table of a given integer.

Code:
import java.util.Scanner;

public class Table {

public static void main(String[] args) {

Scanner number = new Scanner(System.in);

System.out.print("Enter the number: ");

int num = number.nextInt();

System.out.print("Enter the range: ");

int range = number.nextInt();

for (int i = 1; i <= range; i++) {

System.out.println(num + " x " + i + " = " + (num * i));

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;

public class Pattern {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of rows: ");

int rows = scanner.nextInt();

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {

System.out.print(j);

System.out.println();

Output:

Hemanth k
Vu22csen0100711

You might also like