Custom Search
SEARCH THE WEB

Listing out leap years between certain period

we have to find out the leap years between 1990 and 2006. First define the two years under a class "leapyears". Let i = 2006 and n=1990. Now with the help of for loop method initialize the year as n=1990 and n<=i. Also apply the increment statement in the loop as we have to check one by one. 

As we know a leap year is divisible by 4, define an integer l=n%4. So if 'n' is divisible by 4 or l=0, then the particular year can be a leap year. For checking this, apply the if statement and if this satisfies then, the year will be a leap year. For listing out each year write "+n" in the System.out.println. 
 
Code of the program:

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){
        System.out.println("leap year: "+n);
      }
    }
  }
}

Checking whether a year is leap or not

we have taken the year 2000. So define an integer n=2000 in the class "Leapyear" and now apply "if else" condition. As we know leap year is divided by the integer 4 and so applying if condition as n/4=0, then "n" is a leap year. Now in the System.out.println write the message that the year is a leap year. Again applying "else" condition the output will be that the year is not a leap year. 
 
Program Code:
 
class  Leapyear
{
  public static void main(String[] args)
  {
    int n=2000;
    if (n%4==0){
    System.out.println("The given year is a leap year");
      }
    else{
    System.out.println("This is not a leap year");
  }
}
}

WAP to construct a triangle with the ‘*’

First of all make a class named 'triangle' under the Java I/O package and as we have to use the Buffer class, the application of all try and catch block is important for avoiding any kind of error. After creating BufferedReader object and input stream reader define an integer 'a' and apply parseInt method for the conversion of string into integer. 

Now apply the for loop and define an integer 'i' and it should be either less than or equal to the integer "a" (the input number). Again define another integer type variable "j" in another for loop. Here in the second for loop "j" the number of times we have to print *. 

Now compile and run the program and insert any number on your command window and surely you will get a triangle shape with star *. You can take any other object instead of *. 

Code of the program: 
 
import java.io.*;
class triangle{
  public static void main(String[] args) {
    try{
      BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("enter the number");
      int a= Integer.parseInt(object.readLine());
      for (int i=1; i<a;i++ ){
        for (int j=1; j<=i;j++ ){
          System.out.print("*");
        }
        System.out.println("");
      }
    }
    catch(Exception e){}
  }
}

A program for calculating area and perimeter of a rectangle

First of all create a class named RecArea under Java.io package. Now define two integer variable 'l' and 'w'. As the program will be based on keyboard numerical input, it is important for every programmer to use correct data without any mistake. In this case the exception methods like try/catch mechanism helps in detecting user input errors. So before starting the functional code, enclosed it with try clause so that any error in the statement causes the execution of the catch clauses. 

Now create an abstract buffer class which is the super class of all classes and represents a stream of input bytes.  The InputSreamReader reads the character stream and stores it in the buffer class. Now use parseInt for both length and width of the rectangle. This is an instance of class method and is used to convert a string to an integer. Define the area as l*w and perimeter as 2*(l+w) and in the end use the catch exception. 

Now compile and run the program and input the value as you see the message and get the ultimate result. If you find any kind of error, then check the whole program again. 

Code of the program:
 
import java.io.*;
class RecArea
{
  public static void main(String[] args)
  {
    int l=0;
    int w=0;
    try{
     
      BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter length of rectangle : ");
      l = Integer.parseInt(br1.readLine());
      System.out.println("Enter width of rectangle : ");
      w = Integer.parseInt(br1.readLine());
      int area = l*w;
      System.out.println("Area of Rectangle : "+area);
      int perimiter = 2*(l+w);
      System.out.println("Perimeter: " + perimiter);
    }catch(Exception e){System.out.println("Error : "+e);}
  }
  }

Palindrome Number

With the help of this program, we are going to determine whether the given number is palindrome or not.  To achieve the desired result, firstly we have to define a class named "Palindrome". After that we will ask the user to enter any integer type number and then we will reverse it. After reversing the number we will check whether the given number is palindrome or not. If the given number is larger, then it will display a message "Out of range!". 
 
import java.io.*;

public class Palindrome  {
      public static void main(String [] args){
      try{
      BufferedReader object = new BufferedReader(
                  new 
InputStreamReader(System.in));
      System.out.println("Enter number");
      int num= Integer.parseInt(object.readLine());
        int n = num;
        int rev=0;
        System.out.println("Number: ");
        System.out.println(" "+ num);
        for (int i=0; i<=num; i++){
          int r=num%10;
          num=num/10;
          rev=rev*10+r;
          i=0;
        }
        System.out.println("After reversing the number: "" ");
        System.out.println(" "+ rev);      
        if(n == rev){
        System.out.print("Number is palindrome!");
        }
        else{
        System.out.println("Number is not palindrome!");
        }
      }
      catch(Exception e){
        System.out.println("Out of range!");
      }
  }
}         

Calculate factorial of any given number - factorial of number in double

import java.io.*; 

class Factorial1{
  public static void main(String[] args) {
      try{
        BufferedReader object = new BufferedReader(
       
new InputStreamReader(System.in));
        System.out.println("enter the number");
        int a= Integer.parseInt(object.readLine());
        double fact= 1;
        System.out.println("Factorial of " +a+ ":");
        for (int i= 1; i<=a; i++){
              fact=fact*i;
        }
        System.out.println(fact);
    }
    catch (Exception e){
      System.out.println("Array out of bounds exaception");
    }

  }
}

Program to calculate factorial of any given number

This Java programming tutorial will teach you the methods for writing program to calculate factorial of any given number. First of all define a class "Factorial" under the Java I/O package. Java I/O package has a input stream and a output stream in which input stream is used for reading the stream and memory allocating and the output stream is used for writing bytes. As in this program we are going to insert certain instruction by creating buffer reader class, it is necessary to use 'try' and 'catch' block for catching and handling exceptions during execution of the program.  

Here, we have to create a buffer for the string class that can be used to instantiate a changeable object for storing and processing a string of character. The strings length and content change as soon as any object is inserted, replaced or removed from the StringBuffer object. 

Now create a buffer object that inherits properties from the string object class. Now create an InputStreamReader that reads bytes and decodes them into character by using certain 'charset'. Now use the ParseInt method for converting the parses the string argument to a decimal integer and define 'a' as an integer. Take an integer variable as fact=1 and insert the message in the System method. 

Now applying for loop with conditions as integer i=1(intializer),  i<=a and i++ as increment operator. So output result will be like fact=fact*i.

 

 

Here is the code of the program:

import java.io.*; 
class Factorial{
  public static void main(String[] args) {
      try{
        BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the number");
        int a= Integer.parseInt(object.readLine());
        int fact= 1;
        System.out.println("Factorial of " +a+ ":");
        for (int i= 1; i<=a; i++){
              fact=fact*i;
        }
        System.out.println(fact);
    }
    catch (Exception e){}
  }
}

Custom Search
SEARCH THE WEB

All Rights Reserved
Contact Us