100 Java Programs
100 Java Programs
100 Java Programs
2. Print Integer
in java
class Integers {
System.out.println(c);
System.out.println(t);
import java.util.Scanner;
class GetInputFromUser
int a;
float b;
String s;
System.out.println("Enter a string");
s = in.nextLine();
System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("Enter a float");
b = in.nextFloat();
import java.util.*;
class FahrenheitToCelsius {
float temperatue;
import java.util.Scanner;
class SwapNumbers
int x, y, temp;
x = in.nextInt();
y = in.nextInt();
temp = x;
x = y;
y = temp;
import java.util.Scanner;
class SwapNumbers
{
int x, y;
x = in.nextInt();
y = in.nextInt();
x = x + y;
y = x - y;
x = x - y;
class AddNumbers
int x, y, z;
x = in.nextInt();
y = in.nextInt();
z = x + y;
}
}
import java.util.Scanner;
import java.math.BigInteger;
class AddingLargeNumbers {
number1 = in.nextLine();
number2 = in.nextLine();
BigInteger sum;
sum = first.add(second);
class LargestOfThreeNumbers
int x, y, z;
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
else
if (learning) {
System.out.println("Java programmer");
else {
import java.util.Scanner;
class IfElse {
passingMarks = 40;
marksObtained = input.nextInt();
else {
class NestedIfElse {
char grade;
passingMarks = 40;
grade = 'B';
grade = 'C';
else
grade = 'D';
else {
grade = 'F';
class OddOrEven
int x;
x = in.nextInt();
if ( x % 2 == 0 )
else
int n, c, fact = 1;
n = in.nextInt();
if ( n < 0 )
else
fact = fact*c;
import java.util.Scanner;
import java.math.BigInteger;
class BigFactorial
{
int n, c;
System.out.println("Input an integer");
n = input.nextInt();
fact = fact.multiply(inc);
inc = inc.add(BigInteger.ONE);
class CompareStrings
s1 = in.nextLine();
s2 = in.nextLine();
if ( s1.compareTo(s2) > 0 )
else
class ForLoop {
int c;
System.out.println(c);
System.out.print("*");
}
}
System.out.print("*");
class WhileLoop {
Program in java
int n;
System.out.println("Input an integer");
System.out.println("Input an integer");
System.out.println("Out of loop");
int n, reverse = 0;
n = in.nextInt();
while( n != 0 )
n = n/10;
class BreakWhileLoop {
int n;
while (true) {
System.out.println("Input an integer");
n = input.nextInt();
if (n == 0) {
break;
import java.util.Scanner;
class BreakContinueWhileLoop {
int n;
while (true) {
System.out.println("Input an integer");
n = input.nextInt();
if (n != 0) {
continue;
else {
break;
}
}
class Alphabets
char ch;
System.out.println(ch);
System.out.println(t);
//For String
class EnhancedForLoop {
System.out.println(sample);
}
class MultiplicationTable
int n, c;
table");
n = in.nextInt();
System.out.println(n+"*"+c+" = "+(n*c));
table");
a = in.nextInt();
b = in.nextInt();
System.out.println(c+"*"+d+" = "+(c*d));
class PrimeNumbers
n = in.nextInt();
if (n >= 1)
System.out.println(2);
if ( num%j == 0 )
{
status = 0;
break;
if ( status != 0 )
System.out.println(num);
count++;
status = 1;
num++;
import java.util.Scanner;
class ArmstrongNumber
number");
n = in.nextInt();
temp = n;
// Count number of digits
while (temp != 0) {
digits++;
temp = temp/10;
temp = n;
while (temp != 0) {
remainder = temp%10;
temp = temp/10;
if (n == sum)
else
int c, p = 1;
p = p*n;
return p;
int n, num = 1, c, d;
want");
n = in.nextInt();
System.out.print(num+" ");
num++;
System.out.println();
class SubstringsOfAString
{
public static void main(String args[])
int i, c, length;
string = in.nextLine();
length = string.length();
System.out.println(sub);
class ReverseString
Program
class InvertString
System.out.println(a.reverse());
import java.util.*;
class Palindrome
original = in.nextLine();
if (original.equals(reverse))
else
//Another Method
import java.util.*;
class Palindrome
String inputString;
System.out.println("Input a string");
inputString = in.nextLine();
begin = 0;
end = length - 1;
if (inputString.charAt(begin) == inputString.charAt(end)) {
begin++;
end--;
else {
break;
if (i == middle + 1) {
System.out.println("Palindrome");
else {
System.out.println("Not a palindrome");
import java.util.Scanner;
class AddTwoMatrix
int m, n, c, d;
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
first[c][d] = in.nextInt();
second[c][d] = in.nextInt();
to subtract matrices
System.out.print(sum[c][d]+"\t");
System.out.println();
}
}
import java.util.Scanner;
class MatrixMultiplication
int m, n, p, q, sum = 0, c, d, k;
matrix");
m = in.nextInt();
n = in.nextInt();
first[c][d] = in.nextInt();
matrix");
p = in.nextInt();
q = in.nextInt();
if ( n != p )
else
second[c][d] = in.nextInt();
multiply[c][d] = sum;
sum = 0;
System.out.print("\n");
Program
import java.util.Scanner;
class TransposeAMatrix
int m, n, c, d;
m = in.nextInt();
n = in.nextInt();
matrix[c][d] = in.nextInt();
transpose[d][c] = matrix[c][d];
System.out.print(transpose[c][d]+"\t");
System.out.print("\n");
String s1="hello";
String s2="hello";
String s3="meklo";
String s4="hemlo";
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
System.out.println(s1.compareTo(s4));
}}
java Program
class StringEndwith{
public static void main(String args[]){
System.out.println(s1.endsWith("r")); //true
System.out.println(s1.endsWith("Lamror")); //true
System.out.println(s1.endsWith("lamror"));//false
//passing substring
Program
System.out.println(index1+" "+index2);//2 8
System.out.println(index4);//3
}}
java Program
"was"
System.out.println(replaceString);
}}
for(String w:words){
System.out.println(w);
}}
java
System.out.println(s1+"Technolamror");//without trim()
System.out.println(s1.trim()+"Technolamror");//with trim()
}}
in java Program
p
ublic class StringLowerExample
String s1lower=s1.toLowerCase();
System.out.println(s1lower);
}}
class Methods {
// Constructor method
Methods() {
class is created");
staticMethod();
object.nonStaticMethod();
// Static method
object");
}
// Non static method
void nonStaticMethod() {
object");
class StringMethods
int n;
System.out.println(s);
n = s.length();
t = s.replace("Java", "C++");
System.out.println(s);
System.out.println(t);
System.out.println(s);
System.out.println(u);
class StaticBlock {
static {
class StaticBlock {
static {
String os = System.getenv("OS");
if (os.equals("Windows_NT") != true) {
System.exit(1);
class Difference {
public static void main(String[] args) {
System.out.println("Programming is amazing.");
void show(){
System.out.println("Java is awesome.");
class Computer {
Computer() {
void computer_method() {
my.computer_method();
your.laptop_method();
class Laptop {
Laptop() {
void laptop_method() {
class Programming {
//constructor method
Programming() {
java Program
class Language {
String name;
Language() {
Language(String t) {
name = t;
java.getName();
cpp.getName();
void setName(String t) {
name = t;
void getName() {
class Division {
int a, b, result;
a = input.nextInt();
b = input.nextInt();
// try block
try {
result = a / b;
// catch block
catch (ArithmeticException e) {
if(age<18)
else
validate(13);