Sei sulla pagina 1di 4

1.

class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }

Parameters used in First Java Program


Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().

o class keyword is used to declare a class in java.


o public keyword is an access modifier which represents visibility. It means
it is visible to all.
o static is a keyword. If we declare any method as static, it is known as the
static method. The core advantage of the static method is that there is no
need to create an object to invoke the static method. The main method is
executed by the JVM, so it doesn't require to create an object to invoke
the main method. So it saves memory.
o void is the return type of the method. It means it doesn't return any
value.
o main represents the starting point of the program.
o String[] args is used for command line argument. We will learn it later.
o System.out.println() is used to print statement. Here, System is a class,
out is the object of PrintStream class, println() is the method of
PrintStream class.

Java Program to Print an Integer (Entered by the


User)
1. import java.util.Scanner;
2.
3. public class HelloWorld {
4.
5. public static void main(String[] args) {
6.
7. // Creates a reader instance which takes
8. // input from standard input - keyboard
9. Scanner reader = new Scanner(System.in);
10. System.out.print("Enter a number: ");
11.
12. // nextInt() reads the next integer from the keyboard
13. int number = reader.nextInt();
14.
15. // println() prints the following line to the output screen
16. System.out.println("You entered: " + number);
17. }
18. }

Sum of two numbers


public class AddTwoNumbers
{
public static void main(String[] args)
{
int num1 = 5;
int num2 = 15;
int sum;
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
} }

Sum of two numbers using Scanner


import java.util.Scanner;
public class AddTwoNumbers2
{
public static void main(String[] args)
{
int num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt();
System.out.println("Enter Second Number: ");
num2 = sc.nextInt();
sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
} }

Java Program to check Even or Odd


number
import java.util.Scanner;
class CheckEvenOdd
{
public static void main(String args[])
{
int num;
System.out.println("Enter an Integer number:");
//The input provided by user is stored in num
Scanner input = new Scanner(System.in); num = input.nextInt();
/* If number is divisible by 2 then it's an even number
else odd number*/
if ( num % 2 == 0 )
System.out.println("Entered number is even");
else
System.out.println("Entered number is odd");
} }

Program to read two integer and print product of


them
import java.util.Scanner;
public class Demo
{
public static void main(String[] args)
{
/* This reads the input provided by user * using keyboard
*/
Scanner scan = new Scanner(System.in);
System.out.print("Enter first number: ");
// This method reads the number provided using keyboard
int num1 = scan.nextInt();
System.out.print("Enter second number: ");
int num2 = scan.nextInt(); // Closing Scanner after the use
scan.close(); // Calculating product of two numbers
int product = num // Displaying the multiplication result
System.out.println("Output: "+product);
} }

Java Program to calculate simple interest


import java.util.Scanner;
public class JavaExample
{
public static void main(String args[])
{
float p, r, t, sinterest;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Principal : ");
p = scan.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = scan.nextFloat();
System.out.print("Enter the Time period : ");
t = scan.nextFloat();
scan.close();
sinterest = (p * r * t) / 100;
System.out.print("Simple Interest is: " +sinterest);
} }

Java Program to Find the Largest Among Three


Numbers
1. public class Largest {
2.
3. public static void main(String[] args) {
4.
5. double n1 = -4.5, n2 = 3.9, n3 = 2.5;
6.
7. if( n1 >= n2 && n1 >= n3)
8. System.out.println(n1 + " is the largest number.");
9.
10. else if (n2 >= n1 && n2 >= n3)
11. System.out.println(n2 + " is the largest number.");
12.
13. else
14. System.out.println(n3 + " is the largest number.");
15. }
16. }

Program to convert char to String


class CharToStringDemo
{
public static void main(String args[])
{ // Method 1: Using toString() method
char ch = 'a';
String str = Character.toString(ch);
System.out.println("String is: "+str);
// Method 2: Using valueOf() method
String str2 = String.valueOf(ch);
System.out.println("String is: "+str2);
} }

Converting String to Char


class StringToCharDemo
{
public static void main(String args[])
{ // Using charAt() method
String str = "Hello";
for(int i=0; i<str.length();i++)
{
char ch = str.charAt(i);
System.out.println("Character at "+i+" Position: "+ch);
} } }

Java char to int

Potrebbero piacerti anche