You are on page 1of 6

1.

Java program to print the “ Hello Java”

Import java.io.*;

Class A

public static void main(String args [])

System.out.println(“ Hello Java”);

2.Java program to take an integer as input and print it

import java.io.*;

import java.util.Scanner;

// Driver Class

class GFG {

// main function

public static void main(String[] args)

// Declare the variables

int num;

// Input the integer

System.out.println("Enter the integer: ");

// Create Scanner object

Scanner s = new Scanner(System.in);

// Read the next integer from the screen

num = s.nextInt();

// Display the integer

System.out.println("Entered integer is: " + num);

Input
Enter the integer: 10

Output

Entered integer is: 10

3.Java Program to print Multiplication of two floating point Number.

import java.io.*;

class GFG {

public static void main(String[] args)

// f is to ensures that numbers are float DATA TYPE

float f1 = 1.5f;

float f2 = 2.0f;

// to store the multiplied value

float p = f1 * f2;

// to print the product

System.out.println("The product is: " + p);

Output

The product is: 3.0

4.Java Program to Swap Two values using third variable using temp variable

// Importing generic libraries

import java.util.*;

class GFG {

// Function to swap two numbers

// Using temporary variable

static void swapValuesUsingThirdVariable(int m, int n)

// Swapping the values

int temp = m;

m = n;

n = temp;

System.out.println("Value of m is " + m
+ " and Value of n is " + n);

// Main driver code

public static void main(String[] args)

// Random integer values

int m = 9, n = 5;

// Calling above function to

// reverse the numbers

swapValuesUsingThirdVariable(m, n);

Output

Value of m is 5 and Value of n is 9

5.Java Program to Check if Given Integer is Odd or Even

// Importing required classes

import java.io.*;

import java.util.Scanner;

// Main class

class GFG {

// Main Driver Method

public static void main(String[] args)

// Declaring and initializing integer variable

int num = 10;

// Checking if number is even or odd number

// via remainder

if (num % 2 == 0) {

// If remainder is zero then this number is even

System.out.println("Entered Number is Even");

}
else {

// If remainder is not zero then this number is

// odd

System.out.println("Entered Number is Odd");

Output

Entered Number is Even

6.Java Program to Find the Biggest of 3 Numbers

// Importing generic Classes/Files

import java.io.*;

class GFG {

// Function to find the biggest of three numbers

static int biggestOfThree(int x, int y, int z)

return z > (x > y ? x : y) ? z : ((x > y) ? x : y);

// Main driver function

public static void main(String[] args)

// Declaring variables for 3 numbers

int a, b, c;

// Variable holding the largest number

int largest;

a = 5;

b = 10;

c = 3;

// Calling the above function in main

largest = biggestOfThree(a, b, c);

// Printing the largest number

System.out.println(largest + " is the largest number.");

}
}

Output

10 is the largest number.

7.Java program to find a leap year

// Importing Classes/Files

import java.io.*;

// Class for leap-year dealing

public class GeeksforGeeks {

// Method to check leap year

public static void isLeapYear(int year)

// flag to take a non-leap year by default

boolean is_leap_year = false;

// If year is divisible by 4

if (year % 4 == 0) {

is_leap_year = true;

// To identify whether it is a

// century year or not

if (year % 100 == 0) {

// Checking if year is divisible by 400

// therefore century leap year

if (year % 400 == 0)

is_leap_year = true;

else

is_leap_year = false;

// We land here when corresponding if fails

// If year is not divisible by 4

else

// Flag dealing- Non leap-year

is_leap_year = false;

if (!is_leap_year)
System.out.println(year + " : Non Leap-year");

else

System.out.println(year + " : Leap-year");

// Driver Code

public static void main(String[] args)

// Calling our function by

// passing century year not divisible by 400

isLeapYear(2000);

// Calling our function by

// passing Non-century year

isLeapYear(2002);

Output

2000 : Leap-year

2002 : Non Leap-year

You might also like