****Hello World******
output: Hello World
*****Print integers*****
output:
1
2
3
4
5
6
7
8
9
10
*****If-else statement*****
class First { public static void main(String[] arguments) { System.out.println("Hello World"); } }
class Integers { public static void main(String[] arguments) { int c; //declaring a variable /* Using for loop to repeat instruction execution */ for (c = 1; c <= 10; c++) { System.out.println(c); } } }
class Condition { public static void main(String[] args) { boolean learning = true; if (learning) { System.out.println("Java programmer"); } else { System.out.println("What are you doing here?"); } } }
Swapping using temporary or third variable
import java.util.Scanner; class SwapNumbers { public static void main(String args[]) { int x, y, temp; System.out.println("Enter x and y"); Scanner in = new Scanner(System.in); x = in.nextInt(); y = in.nextInt(); System.out.println("Before Swapping\nx = "+x+"\ny = "+y); temp = x; x = y; y = temp; System.out.println("After Swapping\nx = "+x+"\ny = "+y); } }
Output:Enter x and y34Before Swappingx=3y=4After swappingx=4y=3
No comments:
Post a Comment