0% found this document useful (0 votes)
55 views

How To Print An Integer Entered by An User

The document contains 5 code snippets that demonstrate different Java programming concepts: 1. A program that takes an integer input from the user and prints it. 2. A program that swaps the values of two float variables using a temporary variable. 3. A program that checks if a given year is a leap year. 4. A program that reverses a given integer using a while loop. 5. A program that multiplies two float numbers and prints the product.

Uploaded by

Amal Jesudas
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)
55 views

How To Print An Integer Entered by An User

The document contains 5 code snippets that demonstrate different Java programming concepts: 1. A program that takes an integer input from the user and prints it. 2. A program that swaps the values of two float variables using a temporary variable. 3. A program that checks if a given year is a leap year. 4. A program that reverses a given integer using a while loop. 5. A program that multiplies two float numbers and prints the product.

Uploaded by

Amal Jesudas
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/ 4

1.

How to Print an Integer entered by an user


import java.util.Scanner;

public class HelloWorld {

public static void main(String[] args) {

// Creates a reader instance which takes

// input from standard input - keyboard

Scanner reader = new Scanner(System.in);

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

// nextInt() reads the next integer from the keyboard

int number = reader.nextInt();

// println() prints the following line to the output screen

System.out.println("You entered: " + number);

2. Swap two numbers using temporary variable


public class SwapNumbers {

public static void main(String[] args) {

float first = 1.20f, second = 2.45f;

System.out.println("--Before swap--");

System.out.println("First number = " + first);

System.out.println("Second number = " + second);


// Value of first is assigned to temporary

float temporary = first;

// Value of second is assigned to first

first = second;

// Value of temporary (which contains the initial value of first) is assigned to second

second = temporary;

System.out.println("--After swap--");

System.out.println("First number = " + first);

System.out.println("Second number = " + second);

3. Java Program to Check a Leap Year


public class LeapYear {

public static void main(String[] args) {

int year = 1900;

boolean leap = false;

if(year % 4 == 0)

if( year % 100 == 0)

// year is divisible by 400, hence the year is a leap year

if ( year % 400 == 0)

leap = true;
else

leap = false;

else

leap = true;

else

leap = false;

if(leap)

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

else

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

4. Reverse a Number using a while loop in Java

public class ReverseNumber {

public static void main(String[] args) {

int num = 1234, reversed = 0;

while(num != 0) {

int digit = num % 10;

reversed = reversed * 10 + digit;

num /= 10;

}
System.out.println("Reversed Number: " + reversed);

5. Multiply Two Floating Point Numbers


public class MultiplyTwoNumbers {

public static void main(String[] args) {

float first = 1.5f;

float second = 2.0f;

float product = first * second;

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

You might also like