public class HelloWorld{
public static void main(String args[]){
System.out.println("HelloWorld");
}
}
public class TeamMemberNames{
public static void main(String args[]){
System.out.println("john");
System.out.println("dale");
System.out.println("lukas");
}
}
public class FamilyMemberNames{
public static void main(String args[]){
System.out.println("john1");
System.out.println("dale2");
System.out.println("lukas2");
}
}
public class Information{
public static void main(String args[]){
System.out.println("john, I am staying in USA, my age is 25, I am graduated in computer science");
}
}
public class HomeAddress{
public static void main(String args[]){
System.out.println("10-41, Main Bazar, Talupula, Anantapur Dist, AP, 515581");
}
}
public class Addition{
public static void main(String args[]){
System.out.println("Addition of two numbers = "+(10+20));
}
}
public class Substraction{
public static void main(String args[]){
System.out.println("Substraction of two numbers = "+(10-20));
}
}
public class Multiplication{
public static void main(String args[]){
System.out.println("Multiplication of two numbers = "+(10*20));
}
}
public class Division{
public static void main(String args[]){
System.out.println("Division of two numbers = "+(100/20));
}
}
public class Modulus{
public static void main(String args[]){
System.out.println("modulus of two numbers = "+(100%20));
}
}
package com.java.examples;
// Write a java program to use a byte data type to perform addition operation.
public class ByteAdditionOperation {
public static void main(String args[]) {
byte value1 = 10;
byte value2 = 110;
int result = value1+value2; // if we combine byte and byte result will be in int
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a byte data type to perform Subtraction operation.
public class ByteSubtractionOperation {
public static void main(String args[]) {
byte value1 = 100;
byte value2 = 9;
int result = value1-value2; // if we combine byte and byte result will be in int
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a byte data type to perform multiplication operation.
public class ByteMultiplicationOperation {
public static void main(String args[]) {
byte value1 = 5;
byte value2 = 5;
int result = value1*value2; // if we combine byte and byte result will be in int
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a byte data type to perform division operation.
public class ByteDivisionOperation {
public static void main(String args[]) {
byte value1 = 50;
byte value2 = 5;
int result = value1/value2; // if we combine byte and byte result will be in int
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a byte data type to perform modulo operation.
public class ByteModulosOperation {
public static void main(String args[]) {
byte value1 = 120;
byte value2 = 90;
int result = value1%value2; // if we combine byte and byte result will be in int
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a byte data type to perform Arithmetic operations.
public class ByteArithmeticOperation {
public static void main(String args[]) {
byte value1 = 5;
byte value2 = 3;
// Addition
int additionResult = value1+value2;
System.out.println(additionResult);
// Subtraction
int subtractionResult = value1-value2;
System.out.println(subtractionResult);
// Multiplication
int multiplicationResult = value1*value2;
System.out.println(multiplicationResult);
// Division
int divisionResult = value1/value2;
System.out.println(additionResult);
// Modulus
int modulusResult = value1%value2;
System.out.println(modulusResult);
}
}
package com.java.examples;
// Write a java program to use a short data type to perform addition operation.
public class ShortAdditionOperation {
public static void main(String args[]) {
short value1 = 4800;
short value2 = 345;
int result = value1+value2; // if we combine short and short result will be in int
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a short data type to perform substraction operation.
public class ShortSubtractionOperation {
public static void main(String args[]) {
short value1 = 4800;
short value2 = 345;
int result = value1-value2; // if we combine short and short result will be in int
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a short data type to perform multiplication operation.
public class ShortMultiplicationOperation {
public static void main(String args[]) {
short value1 = 4800;
short value2 = 345;
int result = value1*value2; // if we combine short and short result will be in int
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a short data type to perform division operation.
public class ShortDivisionOperation {
public static void main(String args[]) {
short value1 = 4800;
short value2 = 345;
int result = value1/value2; // if we combine short and short result will be in int
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a short data type to perform modulo operation.
public class ByteModulosOperation {
public static void main(String args[]) {
short value1 = 4800;
short value2 = 345;
int result = value1%value2; // if we combine short and short result will be in int
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a short data type to perform Arithmetic operations.
public class ShortArithmeticOperation {
public static void main(String args[]) {
short value1 = 4800;
short value2 = 345;
// Addition
int additionResult = value1+value2;
System.out.println(additionResult);
// Subtraction
int subtractionResult = value1-value2;
System.out.println(subtractionResult);
// Multiplication
int multiplicationResult = value1*value2;
System.out.println(multiplicationResult);
// Division
int divisionResult = value1/value2;
System.out.println(additionResult);
// Modulus
int modulusResult = value1%value2;
System.out.println(modulusResult);
}
}
package com.java.examples;
// Write a java program to use a int data type to perform addition operation.
public class IntAdditionOperation {
public static void main(String args[]) {
int value1 = 543567;
int value2 = 12367;
int result = value1+value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a int data type to perform substraction operation.
public class IntSubtractionOperation {
public static void main(String args[]) {
int value1 = 543567;
int value2 = 12367;
int result = value1-value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a int data type to perform multiplication operation.
public class IntMultiplicationOperation {
public static void main(String args[]) {
int value1 = 543567;
int value2 = 12367;
int result = value1*value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a int data type to perform division operation.
public class IntDivisionOperation {
public static void main(String args[]) {
int value1 = 4800;
int value2 = 345;
int result = value1/value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a int data type to perform modulus operation.
public class IntModulosOperation {
public static void main(String args[]) {
int value1 = 4800;
int value2 = 345;
int result = value1%value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a int data type to perform Arithmetic operations.
public class IntArithmeticOperation {
public static void main(String args[]) {
int value1 = 4800;
int value2 = 345;
// Addition
int additionResult = value1+value2;
System.out.println(additionResult);
// Subtraction
int subtractionResult = value1-value2;
System.out.println(subtractionResult);
// Multiplication
int multiplicationResult = value1*value2;
System.out.println(multiplicationResult);
// Division
int divisionResult = value1/value2;
System.out.println(additionResult);
// Modulus
int modulusResult = value1%value2;
System.out.println(modulusResult);
}
}
package com.java.examples;
// Write a java program to use a long data type to perform addition operation.
public class LongAdditionOperation {
public static void main(String args[]) {
long value1 = 678767788;
long value2 = 6765;
long result = value1+value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a long data type to perform substraction operation.
public class LongSubtractionOperation {
public static void main(String args[]) {
long value1 = 678767788;
long value2 = 6765;
long result = value1-value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a long data type to perform multiplication operation.
public class LongMultiplicationOperation {
public static void main(String args[]) {
long value1 = 678767788;
long value2 = 6765;
long result = value1*value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a long data type to perform division operation.
public class LongDivisionOperation {
public static void main(String args[]) {
long value1 = 678767788;
long value2 = 6765;
long result = value1/value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a long data type to perform modulus operation.
public class LongModulosOperation {
public static void main(String args[]) {
long value1 = 678767788;
long value2 = 6765;
long result = value1%value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a long data type to perform Arithmetic operations.
public class LongArithmeticOperation {
public static void main(String args[]) {
long value1 = 678767788;
long value2 = 6765;
// Addition
long additionResult = value1+value2;
System.out.println(additionResult);
// Subtraction
long subtractionResult = value1-value2;
System.out.println(subtractionResult);
// Multiplication
long multiplicationResult = value1*value2;
System.out.println(multiplicationResult);
// Division
long divisionResult = value1/value2;
System.out.println(additionResult);
// Modulus
long modulusResult = value1%value2;
System.out.println(modulusResult);
}
}
package com.java.examples;
// Write a java program to use a float data type to perform addition operation.
public class FloatAdditionOperation {
public static void main(String args[]) {
float value1 = 123.98f;
float value2 = 67.87f;
float result = value1+value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a float data type to perform substraction operation.
public class FloatSubtractionOperation {
public static void main(String args[]) {
float value1 = 123.98f;
float value2 = 67.87f;
float result = value1-value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a float data type to perform multiplication operation.
public class FloatMultiplicationOperation {
public static void main(String args[]) {
float value1 = 123.98f;
float value2 = 67.87f;
float result = value1*value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a float data type to perform division operation.
public class FloatDivisionOperation {
public static void main(String args[]) {
float value1 = 123.98f;
float value2 = 67.87f;
float result = value1/value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a float data type to perform modulus operation.
public class FloatModulosOperation {
public static void main(String args[]) {
float value1 = 123.98f;
float value2 = 67.87f;
float result = value1%value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a float data type to perform Arithmetic operations.
public class LongArithmeticOperation {
public static void main(String args[]) {
float value1 = 123.98f;
float value2 = 67.87f;
// Addition
float additionResult = value1+value2;
System.out.println(additionResult);
// Subtraction
float subtractionResult = value1-value2;
System.out.println(subtractionResult);
// Multiplication
float multiplicationResult = value1*value2;
System.out.println(multiplicationResult);
// Division
float divisionResult = value1/value2;
System.out.println(additionResult);
// Modulus
float modulusResult = value1%value2;
System.out.println(modulusResult);
}
}
package com.java.examples;
// Write a java program to use a double data type to perform addition operation.
public class DoubleAdditionOperation {
public static void main(String args[]) {
double value1 = 876564.98;
double value2 = 876.09;
double result = value1+value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a double data type to perform substraction operation.
public class DoubleSubtractionOperation {
public static void main(String args[]) {
double value1 = 876564.98;
double value2 = 876.09;
double result = value1-value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a double data type to perform multiplication operation.
public class DoubleMultiplicationOperation {
public static void main(String args[]) {
double value1 = 876564.98;
double value2 = 876.09;
double result = value1*value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a double data type to perform division operation.
public class DoubleDivisionOperation {
public static void main(String args[]) {
double value1 = 876564.98;
double value2 = 876.09;
double result = value1/value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a double data type to perform modulus operation.
public class DoubleModulosOperation {
public static void main(String args[]) {
double value1 = 876564.98;
double value2 = 876.09;
double result = value1%value2;
System.out.println(result);
}
}
package com.java.examples;
// Write a java program to use a double data type to perform Arithmetic operations.
public class LongArithmeticOperation {
public static void main(String args[]) {
double value1 = 876564.98;
double value2 = 876.09;
// Addition
double additionResult = value1+value2;
System.out.println(additionResult);
// Subtraction
double subtractionResult = value1-value2;
System.out.println(subtractionResult);
// Multiplication
double multiplicationResult = value1*value2;
System.out.println(multiplicationResult);
// Division
double divisionResult = value1/value2;
System.out.println(additionResult);
// Modulus
double modulusResult = value1%value2;
System.out.println(modulusResult);
}
}
package com.java.examples;
// Write a java program to use a char data type to perform display simple characters.
public class CharExample {
public static void main(String args[]) {
char c = 'A';
System.out.println(c);
char c = '1';
System.out.println(c);
char c = '%';
System.out.println(c);
}
}
package com.java.examples;
// Write a java program to use a boolean data type to display true or false.
public class BooleasnExample {
public static void main(String args[]) {
boolean b1 = true;
System.out.println(b1);
boolean b2 = false;
System.out.println(b2);
}
}
public class Increment {
public static void main(String[] args) {
int x = 5;
x++;
System.out.println(x);
}
}
public class Decrement {
public static void main(String[] args) {
int x = 5;
--x;
System.out.println(x);
}
}
public class NagationExample{
public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a);//-11 (minus of total positive value which starts from 0)
System.out.println(~b);//9 (positive of total minus, positive starts from 0)
System.out.println(!c);//false (opposite of boolean value)
System.out.println(!d);//true
}}
public class EqualTo {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x == y); // returns false because 5 is not equal to 3
System.out.println(result);
}
}
public class NotEqual {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x != y); // returns true because 5 is not equal to 3
System.out.println(result);
}
}
public class GreaterThan {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x > y); // returns true because 5 is greater than 3
System.out.println(result);
}
}
public class LessThan {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x < y); // returns false because 5 is less than 3
System.out.println(result);
}
}
public class GreaterThanOrEqualTo {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x >= y); // returns true because 5 is greater, or equal, to 3
System.out.println(result);
}
}
public class LessThanOrEqualTo {
public static void main(String[] args) {
int x = 5;
int y = 3;
boolean result = (x <= y); // returns false because 5 is less than, or equal, to 3
System.out.println(result);
}
}
public class LogicalAnd {
public static void main(String[] args) {
int x = 5;
System.out.println(x > 3 && x < 10); // returns true because 5 is greater than 3 AND 5 is less than 10
}
}
public class LogicalAnd {
public static void main(String[] args) {
int x = 5;
System.out.println(x > 3 && x < 10); // returns true because 5 is greater than 3 AND 5 is less than 10
}
}
public class LogicalNot {
public static void main(String[] args) {
int x = 5;
System.out.println(!(x > 3 && x < 10)); // returns false because ! (not) is used to reverse the result
}
}
/ Java program to find largest among two
// numbers using ternary operator
import java.io.*;
class Ternary {
public static void main(String[] args)
{
// variable declaration
int n1 = 5, n2 = 10, max;
System.out.println("First num: " + n1);
System.out.println("Second num: " + n2);
// Largest among n1 and n2
max = (n1 > n2) ? n1 : n2;
// Print the largest number
System.out.println("Maximum is = " + max);
}
}
/ Java program to find largest among two
// numbers using ternary operator
import java.io.*;
class Ternary {
public static void main(String[] args)
{
// variable declaration
int n1 = 5, n2 = 10, max;
System.out.println("First num: " + n1);
System.out.println("Second num: " + n2);
// Largest among n1 and n2
max = (n1 > n2) ? n1 : n2;
// Print the largest number
System.out.println("Maximum is = " + max);
}
}
Fibonacci series example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
In fibbonacci series a number is the sum of previous two numbers example is above. The first two numbers of fibonacci series are 0 and 1.
package com.java.examples;
public class FibonacciSeriesExample {
public static void main(String args[])
{
int a=0;
int b=1;
int c;
int i;
int count=10;
System.out.print(a+" "+b);
for(i = 2; i < count; ++i)
{
c=a+b;
System.out.print(" " +c);
a=b;
b=c;
}
}
}
Prime numbers example: For example 2, 3, 5, 7, 11, 13, 17
Prime number is a number that is greater than 1 and divided by 1 or itself only.
package com.java.examples;
public class PrimeNumberExample {
public static void main(String args[])
{
int i;
int m=0;
int flag=0;
int n=3;
m=n/2;
if(n==0 || n==1) {
System.out.println(n+" is not prime number");
}else{
for(i=2; i <= m; i++) {
if(n%i==0) {
System.out.println(n+" is not prime number");
flag=1;
break;
}
}
if(flag==0) {
System.out.println(n+" is prime number");
}
}
}
}
palindrome numbers example: For example 545, 151, 34543, 343, 171, 48984
A palindrome number is a number that is same after reverse.
package com.java.examples;
public class palindromeNumberExample {
public static void main(String args[])
{
int remainder;
int sum = 0;
int temp;
int number = 454;
temp = number;
while(number > 0) {
remainder = number % 10;
sum = (sum * 10) + remainder;
number = number/10;
}
if(temp == sum) {
System.out.println("palindrome number ");
} else {
System.out.println("not palindrome");
}
}
}
Factorial example: 5! = 5*4*3*2*1 = 120
formula for factorial: n*(n-1)*(n-2)*(n-3)*(n-4)
package com.java.examples;
class FactorialExample{
public static void main(String args[]){
int i;
int factorial = 1;
int number = 5;
for(i=1; i <= number; i++) {
fact = fact * i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
An Armstrong number is a positive m-digit number that is equal to the sum of the mth powers of their digits.
153: 13 + 53 + 33 = 1 + 125+ 27 = 153
125: 13 + 23 + 53 = 1 + 8 + 125 = 134 (Not an Armstrong Number)
package com.java.examples;
public class Armstrong {
public static void main(String[] args) {
int number = 371, originalNumber, remainder, result = 0;
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result = result + Math.pow(remainder, 3);
originalNumber /= 10;
}
if(result == number) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}
}
The positive numbers 1, 2, 3... are known as natural numbers and its sum is the result of all numbers starting from 1 to the given number.
package com.java.examples;
public class SumNatural {
public static void main(String[] args) {
int num = 100, sum = 0;
for(int i = 1; i <= num; ++i)
{
// sum = sum + i;
sum += i;
}
System.out.println("Sum = " + sum);
}
}
In this program, you'll learn to generate multiplication table of a given number. This is done by using a for loop in Java.
package com.java.examples;
public class MultiplicationTable {
public static void main(String[] args) {
int num = 5;
for(int i = 1; i <= 10; ++i)
{
System.out.printf("%d * %d = %d \n", num, i, num * i);
}
}
}
/**
* Write a Java Program to check Retirement Based on Age or Service.
* Conditions
* a. A person who is having age 60 or more than 60 is eligible for Retirement
* b. A person who's service 30 or more than 30 can be eligible for Retirement
*/
package com.java.examples;
public class RetirementAgeCheck {
public static void main(String args[]) {
int age = 55;
int service = 30;
if(age >= 60 || service >= 30) {
System.out.println("A person is eligible for retirement age = "+age+" :: Service = "+service);
} else {
System.out.println("A person is not eligible for retirement age = "+age+" :: Service = "+service);
}
}
}
package com.java.program;
public class Weekdays {
public static void main(String[] args) {
int day=2;
switch(day) {
case 1:System.out.println("Sunday");
break;
case 2:System.out.println("Monday");
break;
case 3:System.out.println("Tuesday");
break;
case 4:System.out.println("Wednesday");
break;
case 5:System.out.println("Thursday");
break;
case 6:System.out.println("Friday");
break;
case 7:System.out.println("Saturday");
break;
default:System.out.println("not a valid week day");
break;
}
}
}