0% found this document useful (0 votes)
32 views9 pages

Java Programs For PT-2

Uploaded by

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

Java Programs For PT-2

Uploaded by

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

Java programs for PT-2

1. Addition of Two Numbers:

import java.util.Scanner; // Import the Scanner class

class MyClass {

public static void main(String[] args) {

int x, y, sum;

Scanner myObj = new Scanner(System.in); // Create a Scanner object

System.out.println("Type a number:");

x = myObj.nextInt(); // Read user input

System.out.println("Type another number:");

y = myObj.nextInt(); // Read user input

sum = x + y; // Calculate the sum of x + y

System.out.println("Sum is: " + sum); // Print the sum

}}

3. Check Even or Odd Number:

//It is a program of odd and even number.

public class IfElseExample {

public static void main(String[] args) {

//defining a variable

int number=13;

//Check if the number is divisible by 2 or not


if(number%2==0){

System.out.println("even number");

}else{

System.out.println("odd number");

4. Check Leap Year in Java:

public class LeapYearExample {

public static void main(String[] args) {

int year=2020;

if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){

System.out.println("LEAP YEAR");

else{

System.out.println("COMMON YEAR");

5. Check Vowel or Consonant in Java:

public class VowelConsonant {

public static void main(String[] args) {

char ch = ‘A’;
if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’

|| ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’) {

System.out.println(ch + ” is a vowel.”);

} else {

System.out.println(ch + ” is a consonant.”);

6.

Java Program to demonstrate the use of If else-if ladder.

//It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.

public class IfElseIfExample {

public static void main(String[] args) {

int marks=65;

if(marks<50){

System.out.println("fail");

else if(marks>=50 && marks<60){

System.out.println("D grade");

else if(marks>=60 && marks<70){

System.out.println("C grade");

else if(marks>=70 && marks<80){

System.out.println("B grade");
}

else if(marks>=80 && marks<90){

System.out.println("A grade");

}else if(marks>=90 && marks<100){

System.out.println("A+ grade");

}else{

System.out.println("Invalid!");

6. Program to check POSITIVE, NEGATIVE or ZERO:

import java.util.Scanner; // Import the Scanner class to get user input

public class NumberChecker {

public static void main(String[] args) {

Scanner input = new Scanner(System.in); // Create a Scanner object

System.out.print("Enter a number: "); // Prompt the user for input

double number = input.nextDouble(); // Read the user's input as a double

if (number > 0) {

System.out.println(number + " is a positive number.");

} else if (number < 0) {

System.out.println(number + " is a negative number.");

} else {

System.out.println(number + " is zero.");

}
}7.Java program to check vote eligibility

import java.util.Scanner;

public class Voting {

public static void main(String[] args)

// Declaring variables

int age, diff;

// taking user input

Scanner scan = new Scanner(System.in);

System.out.println("Please enter your age: ");

age = scan.nextInt();

// Checking condition for voting..

if(age>=18)

System.out.println("You are eligible for voting.");

else

diff = (18 - age);

System.out.println("Sorry,You can vote after: "+ diff + " years");

8. Java Program to demonstrate the use of Nested If Statement.


public class JavaNestedIfExample {

public static void main(String[] args) {

//Creating two variables for age and weight

int age=20;

int weight=80;

//applying condition on age and weight

if(age>=18){

if(weight>50){

System.out.println("You are eligible to donate blood");

}}

9. Java Program to demonstrate the use of Nested If Statement.

public class JavaNestedIfExample2 {

public static void main(String[] args) {

//Creating two variables for age and weight

int age=25;

int weight=48;

//applying condition on age and weight

if(age>=18){

if(weight>50){

System.out.println("You are eligible to donate blood");

} else{

System.out.println("You are not eligible to donate blood");

} else{
System.out.println("Age must be greater than 18");

} }

10. Area and peri of a square

import java.util.Scanner; // Import the Scanner class to read user input

public class SquareCalculator {

public static void main(String[] args) {

// Create a Scanner object to get input from the console

Scanner input = new Scanner(System.in);

// Prompt the user to enter the side of the square

System.out.print("Enter the side length of the square: ");

// Read the side length entered by the user

double side = input.nextDouble();

// Calculate the area of the square

double area = side * side;

// Calculate the perimeter of the square

double perimeter = 4 * side;

// Display the calculated area

System.out.println("The area of the square is: " + area);

// Display the calculated perimeter

System.out.println("The perimeter of the square is: " + perimeter);

}
11. display their sum, product, difference and division and modulus

import java.util.Scanner;
class Calculation
{
public static void main(String args[]) {
int num1, num2;
int sum, sub, mult, mod;
float div;
Scanner op=new Scanner(System.in);
System.out.print("Enter first number: ");
num1=op.nextInt();
System.out.print("Enter second number:");
num2=op.nextInt();

sum = num1 + num2;


sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
mod = num1 % num2;

System.out.println("SUM = "+sum); System.out.println("DIFFERENCE = "+sub);


System.out.println("PRODUCT = "+mult); System.out.println("QUOTIENT =
"+div); System.out.println("MODULUS = "+mod);
}
}
12. Write a program in Java to compute the perimeter and area of a triangle,
with its three sides given as a, b, and c using the input from user:
import java.util.Scanner;
public class Triangle
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first side: ");
double a = in.nextDouble();
System.out.print("Enter second side: ");
double b = in.nextDouble();
System.out.print("Enter third side: ");
double c = in.nextDouble();
double perimeter = a + b + c;
double s = perimeter / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
System.out.println("Perimeter = " + perimeter);
System.out.println("Area = " + area);
}
}

You might also like