0% found this document useful (0 votes)
48 views5 pages

Java Programs for Basic Algorithms

The document contains code examples for several Java programs: 1) A program that compares two numbers and prints which is greater or if they are equal. 2) A program that determines the largest of three numbers. 3) A program that prints all even numbers between two input numbers. 4) A program that calculates the factorial of a given number. 5) A program that checks if a number is a palindrome. 6) A program that constructs a triangle of asterisks based on a user input number. 7) Programs that check if a year is a leap year and list all leap years between two years. 8) A program that prints the multiplication table of a given number.

Uploaded by

ranjitmehrok
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views5 pages

Java Programs for Basic Algorithms

The document contains code examples for several Java programs: 1) A program that compares two numbers and prints which is greater or if they are equal. 2) A program that determines the largest of three numbers. 3) A program that prints all even numbers between two input numbers. 4) A program that calculates the factorial of a given number. 5) A program that checks if a number is a palindrome. 6) A program that constructs a triangle of asterisks based on a user input number. 7) Programs that check if a year is a leap year and list all leap years between two years. 8) A program that prints the multiplication table of a given number.

Uploaded by

ranjitmehrok
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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;
    }
  }
}

You might also like