Category: Java
Multiply two Floating Point Numbers in Java |
package JavaQuestions;
/**
*
* @author NaveenKhunteta
*
*/
public class FloatingMultiplication {
public static void main(String[] args) {
float f1 = 2.5f;
float f2 = 3.5f;
System.out.println("the product is: " + (f1 * f2));
}
}
Output:
the product is: 8.75
This is very simple, just define two integer variables and use + operator to add them.
package JavaQuestions;
/**
*
* @author NaveenKhunteta
*
*/
public class AddNumbers {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
System.out.println("sum of a and b is: " + sum);
}
}
Output:
sum of a and b is: 30
Print an Integer – entered by user from console/command line
package JavaQuestions;
import java.util.Scanner;
/**
*
* @author NaveenKhunteta
*
*/
public class PRintImnteger {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("plz enter a number: ");
int num = reader.nextInt();
System.out.println("you entered: " + num);
}
}
Scanner class is a default class which is coming from java.util package. It is used to take the user input from the console.
Example:
plz enter a number:
10
you entered: 10