[go: up one dir, main page]

0% found this document useful (0 votes)
37 views10 pages

Javaassingment VU22CSEN0100736

The document contains 9 Java programs that demonstrate various programming concepts like: 1) Calculating the sum and average of 3 user-input numbers 2) Checking if a user-input number is positive, negative, or zero 3) Printing the multiplication table for a user-input number up to a given range 4) Checking if a user-input year is a leap year 5) Printing triangular patterns of numbers 6) Solving a quadratic equation by calculating the discriminant 7) Additional programs for checking sign of a number, printing natural numbers, comparing floating-point numbers to 3 decimal places, and finding the greatest of 3 numbers.

Uploaded by

rdudi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views10 pages

Javaassingment VU22CSEN0100736

The document contains 9 Java programs that demonstrate various programming concepts like: 1) Calculating the sum and average of 3 user-input numbers 2) Checking if a user-input number is positive, negative, or zero 3) Printing the multiplication table for a user-input number up to a given range 4) Checking if a user-input year is a leap year 5) Printing triangular patterns of numbers 6) Solving a quadratic equation by calculating the discriminant 7) Additional programs for checking sign of a number, printing natural numbers, comparing floating-point numbers to 3 decimal places, and finding the greatest of 3 numbers.

Uploaded by

rdudi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Assingment-1

VU22csen0100736
Rajmouli.D

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);
}
}
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");
}}}
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));
}

}
}
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.");
}
}
}
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();
}

}
}

Type your text


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");
}
}
}
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");
}}}
public class NaturalNumbers {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
import java.util.Scanner;

//Program that reads in two floating-point numbers and tests whether they are the same up to three decimal places.

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.");
}
}
}
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);


}
}

You might also like