Java Programs (9)

LEVEL – 1   PROGRAMMING

 

1. Program to calculate the area of circle.

import java.util.Scanner;

class AreaOfCircle

{

   public static void main(String args[])

    {  

      

      Scanner s= new Scanner(System.in);

         System.out.println("Enter the radius:");

         double r= s.nextDouble();                //gets input using scanner method

         double  area=(22*r*r)/7 ;

         System.out.println("Area of Circle is: " + area);     

   }

}

 

2. Program to calculate the area of triangle.

import java.util.Scanner;

class Area_Of_Triangle

{

   public static void main(String args[])

    {  

      

      Scanner s= new Scanner(System.in);

       

         System.out.println("Enter  width of the Triangle:");

         double b= s.nextDouble();

 

         System.out.println("Enter  height of the Triangle:");

          double h= s.nextDouble();

 

          double area=(b*h)/2;                                    //Area = (base*height)/2

      System.out.println("Area of Triangle is: " + area);     

   }

}

 

3. Program to calculate the area of rectangle.

import java.util.Scanner;

class AreaOfRectangle

{

   public static void main(String args[])

    {  

      

      Scanner s= new Scanner(System.in);

       

         System.out.println("Enter the length:");

         double l= s.nextDouble();

        System.out.println("Enter the breadth:");

         double b= s.nextDouble();

       

         

          double  area=l*b;

      System.out.println("Area of Rectangle is: " + area);     

   }

}

 

4. Program to calculate Perimeter of circle

import java.util.Scanner;

class PerimeterOfCircle

{

   public static void main(String args[])

    {  

      

                 Scanner s= new Scanner(System.in);

       

         System.out.println("Enter the radius:");

        

                 double r= s.nextDouble();

        

                 double  c=(22*2*r)/7 ;

     

                 System.out.println("Perimeter of Circle is: " +c);     

   }

}

 

5. Program to calculate Perimeter of Parallogram/Rectangle

import java.util.Scanner;

class PerimeterOfParallelogram

{

   public static void main(String args[])

    {  

      

         Scanner s= new Scanner(System.in);

       

         System.out.println("Enter the height of the Parallelogram/Rectangle :");

        

         double h= s.nextDouble();

                 

                 System.out.println("Enter the breadth of the Parallelogram:");

        

                 double b= s.nextDouble();

         

         double  perimeter=2*(h+b);

    

                 System.out.println("perimeter of Parallelogram is: " + perimeter);     

   }

}

 

6. Program to calculate volume of cylinder

import java.util.Scanner;

class VolumeOfCylinder

{

   public static void main(String args[])

    {  

      

      Scanner s= new Scanner(System.in);

       

         System.out.println("Enter the radius:");

         double r=s.nextDouble();

         System.out.println("Enter the height:");

         double h=s.nextDouble();

 

            double  volume=((22*r*r*h)/7);

 

            System.out.println("volume of Cylinder is: " +volume);

      

        

   }

}

 

7. Program to calculate volume of Sphere

import java.util.Scanner;

class VolumeOfSphere

{

     

   public static void main(String args[])

    {  

      

               

                Scanner s= new Scanner(System.in);

                System.out.println("Enter the radius of sphere:");

                double r=s.nextDouble();

               

               

      

                 double  volume= (4*22*r*r*r)/(3*7);

 

             System.out.println("Volume of shphere is: " +volume);

      }

}

 

The Fibonacci Sequence is the series of numbers:  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The next number is found by adding up the two numbers before it.

8. Program to print fibonnaci  series (using for loop)

import java.util.Scanner;

class Fibonacci

{

                public static void main(String[] args)

                {

                     Scanner sc=new Scanner(System.in);

                     System.out.println("enter number of terms");

                     int n=sc.nextInt();

                     int i=0,j=1,nextTerm;

                     System.out.println("Fibonacci series is ");

                for(int c=0;c<n;c++)

                   {

                     if(c<=1)

                     nextTerm=c;

                     else

                      {

                        nextTerm=i+j;

                         i=j;

                         j=nextTerm;

                    }

                System.out.println(nextTerm);

                }

                }

}

 

9. Program to print fibonnaci  series (using while loop)

import java.util.Scanner;

class Fibonacci

{

                public static void main(String[] args)

                {

                     Scanner sc=new Scanner(System.in);

                     System.out.println("enter number of terms");

                     int n=sc.nextInt();

                     int i=0,j=1,nextTerm;

                     System.out.println("Fibonacci series is ");

                for(int c=0;c<n;c++)

                   {

                     if(c<=1)

                     nextTerm=c;

                     else

                      {

                        nextTerm=i+j;

                         i=j;

                         j=nextTerm;

                    }

                System.out.println(nextTerm);

                }

                }

}