Tuesday, 5 December 2023

Lecture 2 | Variables in Java | Input Output | Complete Placement Course

                                                                  JAVA PROGRAMMING

 

 

public class Main {
public static void main(String[] args) {
System.out.print("Hello World");
}
}
//output: Hello World
 
 
public class Main {
public static void main(String[] args) {
System.out.print("Hello World with Java");
}
}
//output: Hello World with Java
 
 
public class Main {
public static void main(String[] args) {
System.out.print("Hello World with Java");
System.out.print("Hello World with Java");
}
}

//output: Hello World with JavaHello World with Java
 
 
//println
public class Main {
public static void main(String[] args) {
System.out.println("Hello World with Java");
System.out.println("Hello World with Java");
System.out.print("Hello World with Java");
}
}

//output:
// Hello World with Java
// Hello World with Java
// Hello World with Java
 
 
 
 
//BACKSLASH N "\n"
public class Main {
public static void main(String[] args) {
System.out.print("Hello World with Java\n");
System.out.print("Hello World with Java\n");
System.out.print("Hello World with Java");
}
}

//output:
// Hello World with Java
// Hello World with Java
// Hello World with Java
 
 
 
//\n between words
public class Main {
public static void main(String[] args) {
System.out.print("Hello World with java \nfrom \nApni Kaksha");
}
}

//output:
// Hello World with java
// from
// Apni Kaksha
 
 
 
# Revise 
//Three way to print output in Java
//1. print
//2. println
//3. "\n"
 
 
 
//# question: print the pattern
public class Main {
public static void main(String[] args) {
//output
System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("****");
}
}

//output:
// *
// **
// ***
// ****

 
 
//NOTE: type sout and press enter to print the print statement shortcut
 
 
//# QUIZ: PRINT THE STARS
public class Main {
public static void main(String[] args) {
System.out.println("*");
System.out.println("**");
}
}
 
 
 
 
 
variables in java


public class Main {
public static void main(String[] args) {
//variables
String name = "tony stark";
System.out.println(name);
int a = 25;
int b = 10;
int age = 48;
System.out.println(a);
System.out.println(b);
System.out.println(age);

double price = 25.5;
System.out.println(price);

// change b and name value
b = 20;
name = "ironman";
System.out.println(b);
System.out.println(name);
}
}
 
 
 
// code for sum of two numbers
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 25;
int sum = a + b;
System.out.println(sum);
}
}

//code for product of two numbers
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 25;
int mul = a * b;
System.out.println(mul);
}
}
 

 
 
//quiz option solution
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
int ans = a * b / a - b;
System.out.println(ans);
}
}
 //output: 0
 
 
 
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
int ans = (a*b)/(a-b);
System.out.println(ans);
}
}

//output: 10
 
 
 
//taking input in java using scanner class
import java.util.*;

public class Main {
public static void main(String[] args) {
//Input
Scanner sc = new Scanner(System.in);
String name = sc.next();
System.out.println(name);
}
}
//output:
//input tony stark
//output tony
 
 
 
 //taking input using next line code
import java.util.*;
public class Main {
public static void main(String[] args) {
//Input
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println(name);
}
}

//output:
// tony stark and iron man
// tony stark and iron man
 
 
 
import java.util.*;
public class Main {
public static void main(String[] args) {
//Input
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int sum = a + b;
System.out.println(sum);

}
}

//output:
// 100
// 100
// 200
 

No comments:

Post a Comment

Lecture 2 | Variables in Java | Input Output | Complete Placement Course

                                                                                 JAVA PROGRAMMING     public class Main { public static...