java programs, Java Programs - Java Programming Examples with Output, Java Programs | Java Programming Examples, Java Examples, Java Coding Samples, Java Programs - 500+ Simple & Basic Programming With ..., Java Programs Archives, Developing Java programs with the JDK, Top 20 Java Interview Programs for Programming and Coding ...
"#Core Java Program's "
First Example: Sum of two numbers
public class Exapme1 {
public static void main(String[] args) {
int num1 = 15, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}
Output:
Sum of these numbers: 30
Second Example: Sum of two numbers using Scanner
import java.util.Scanner;
public class Example2 {
public static void main(String[] args) {
int a, b, sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
a = sc.nextInt();
System.out.println("Enter Second Number: ");
b = sc.nextInt();
sc.close();
sum = a + b;
System.out.println("Sum of these numbers: "+sum);
}
}
Output:
Enter First Number: 121
Enter Second Number: 19
Sum of these numbers: 140
Program to display even numbers from 1 to n where n is 100
class Example {
public static void main(String args[]) {
int n = 100;
System.out.print("Even Numbers from 1 to "+n+" are: ");
for (int i = 1; i <= n; i++) {
//if number%2 == 0 it means its an even number
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
}
}
Output:
Even Numbers from 1 to 100 are: 2 4 6 8 10 12 14 16 18 20 22 24 26 28
30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76
78 80 82 84 86 88 90 92 94 96 98 100
Program to print odd numbers from 1 to n where n is 100
class Example {
public static void main(String args[]) {
int n = 100;
System.out.print("Odd Numbers from 1 to "+n+" are: ");
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
}
}
Java Program to check Even or Odd number
import java.util.Scanner;
class EvenOdd{
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");
}
}
Output 1:
Enter an Integer number:
78
Entered number is even
Output 2:
Enter an Integer number:
77
Entered number is odd
if you don't have java installed in your machine and if you want o install java in linux and any linux based os like mint, dabian also other also then please check this https://javawali.blogspot.com/2020/09/how-to-install-java-in-linux-mint.html

Comments
Post a Comment