Comparing Two Numbers
class Comparing{
public static void main(String[] args) {
int a=24, b=25;
if (a == b){
[Link]("Both are equal");
}
else if(a>b){
[Link]("a is greater than b");
}
else{
[Link]("b is greater than a");
}
}
}
Determining the largest number
class largernumber{
public static void main(String[] args) {
int x=500, y=70, z=3000;
if (x>y){
if (x>z){
[Link]("x is greater");
}
else{
if(z>y){
[Link]("z is greater");
}
else{
[Link]("y is greater");
}
}
}
else{
if (y>z){
[Link]("y is greater");
}
}
}
}
Write a program to list all even numbers between two numbers
import [Link].*;
class AllEvenNum{
public static void main(String[] args) {
try{
BufferedReader br1 = new BufferedReader(new InputStreamReader([Link])
);
[Link]("Enter number : ");
int num = [Link]([Link]());
[Link]("Even Numbers:");
for (int i=1;i <=num ; i++){
if(i%2==0 ){
[Link](i+",");
}
}
}
catch(Exception e){}
}
}
Factorial Examples - Java Factorial Example to calculate factorial of any given number
import [Link].*;
class Factorial{
public static void main(String[] args) {
try{
BufferedReader object = new BufferedReader(new InputStreamReader(Syste
[Link]));
[Link]("enter the number");
int a= [Link]([Link]());
int fact= 1;
[Link]("Factorial of " +a+ ":");
for (int i= 1; i<=a; i++){
fact=fact*i;
}
[Link](fact);
}
catch (Exception e){}
}
}
Palindrome Number
import [Link].*;
public class Palindrome {
public static void main(String [] args){
try{
BufferedReader object = new BufferedReader(
new InputStreamReader([Link]));
[Link]("Enter number");
int num= [Link]([Link]());
int n = num;
int rev=0;
[Link]("Number: ");
[Link](" "+ num);
for (int i=0; i<=num; i++){
int r=num%10;
num=num/10;
rev=rev*10+r;
i=0;
}
[Link]("After reversing the number: "+ " ");
[Link](" "+ rev);
if(n == rev){
[Link]("Number is palindrome!");
}
else{
[Link]("Number is not palindrome!");
}
}
catch(Exception e){
[Link]("Out of range!");
}
}
}
Write a program to construct a triangle with
the ‘*’
import [Link].*;
class triangle{
public static void main(String[] args) {
try{
BufferedReader object = new BufferedReader(new InputStreamReader(System.
in));
[Link]("enter the number");
int a= [Link]([Link]());
for (int i=1; i<a;i++ ){
for (int j=1; j<=i;j++ ){
[Link]("*");
}
[Link]("");
}
}
catch(Exception e){}
}
}
Checking whether a year is leap or not
class Leapyear
{
public static void main(String[] args)
{
int n=2000;
if (n%4==0){
[Link]("The given year is a leap year");
}
else{
[Link]("This is not a leap year");
}
}
}
Listing out leap years between certain period
class leapyears
{
public static void main(String[] args)
{
int i=2006;
int n;
for (n=1990; n<=i ; n++){
int l=n%4;
if (l==0){
[Link]("leap year: "+n);
}
}
}
}
Preparing table of a number by using loop
class PreparingTable{
public static void main(String[] args) {
int a=25, b=1;
[Link]("the table of "+a+"= ");
while(b<=10){
int c = a*b;
[Link](c);
b = b+1;
}
}
}