Showing posts with label icse. Show all posts
Showing posts with label icse. Show all posts

 

Write a Java program to find the Area of the Circle, Rectangle and Triangle.




PROGRAM :

import java.util.*;
class area1
{
  
  public static void main(String[] args) throws InputMismatchException
  {
   Scanner sc = new Scanner(System.in);
   double area;
   
   System.out.println("Main Menu");

   System.out.println("1. Circle");

   System.out.println("2. Ractangle");

   System.out.println("3. Triangle");

   System.out.println("Enter your choice(1,2,3) : ");

   int ch;
   ch=sc.nextInt();

   switch(ch)
   {
    case 1:
         System.out.print("Enter the radius : ");
         double r=sc.nextDouble();

         area=r*r*3.14;
         System.out.println("\nThe area of Circle is : "+area);
         break;

    case 2:
         System.out.print("Enter the length : ");
         double l=sc.nextDouble();

         System.out.print("Enter the width :  ");
         double w=sc.nextDouble();

         area=l*w;
         System.out.println("\nThe area of Ractangle is : "+area);
         break;

    case 3:
         System.out.println("Enter sides of triangle : ");

         System.out.print("Enter side1 : ");
         double a=sc.nextDouble();

         System.out.print("Enter side2 : ");
         double b=sc.nextDouble();

         System.out.print("Enter side3 : ");
         double c=sc.nextDouble();

         double s=(a+b+c)/2;
         area=Math.sqrt(s*(s-a)*(s-b)*(s-c));

         System.out.println("\nThe area of Triangle is : "+area);
         break;

    default:
         System.out.println("Wrong choice !!!");
         break;
    }
  }
}



Java program for the pattern of 'H' | Patterns in JAVA | JAVA POINT

 

Java Program for H



Ques - Write a java program for the following pattern of 'H' :


*.    *

*     *

*     *

*****

*     *

*     *

*     *


PROGRAM :


import java.util.Scanner;

class PatternH

 

 {

     public static void main(String []args)

     {

         Scanner sc=new Scanner(System.in);

         System.out.print("Enter the number of rows : ");

         int i, j, n;

         int r=sc.nextInt();

         int c=r-2;

         if(r%2!=0)

         {

             n=r/2;

         }

         else 

         {

             n=(r/2)-1;

         }    

         for(i=0;i<r;i++)

         {

             for(j=0;j<c;j++)

             {

                 if((j==0)||(j==(c-1)))

                 {

                     System.out.print("*");

                 }     

                 else if(i==n)

                 {

                     System.out.print("*");

                 }    

                 else

                 {

                     System.out.print(" ");

                 }

             }

             System.out.println();

         }

        }

 }


/* Output :

 

Enter the number of rows : 7


*     *

*     *

*     *

*****

*     *

*     *

*     *

 

*/

Java program for the pattern of 'G' | Patterns in JAVA | JAVA POINT

 

Java Program for G



Ques - Write a java program for the following pattern of 'G' :


* * * *                      

*               

*               

*     * * *  

*     *   * 

*     *   * 

* * * *   *  


PROGRAM :


import java.util.Scanner;

public class PatternG

{

    public static void main(String[] args)

    {

        System.out.println("Enter the number of rows : ");

        Scanner scan=new Scanner(System.in);

        int n=scan.nextInt();

        System.out.println();

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

        {

            for (int j=0;j<n-1 ;j++ )

            {

                if (j==0 || (i==0 || i==n-1) && j<=n/2 || (j==n/2 || j==n- 

                      2) && i>=n/2 || (i==n/2 && j>=n/2))

                {

                    System.out.print("* ");

                }

                else

                {

                    System.out.print("  ");

                }

            }

            System.out.println();

        }

    }

}


/* Output :

 

Enter the number of rows : 7


* * * *                      

*               

*               

*     * * *  

*     *   * 

*     *   * 

* * * *   * 

 

*/

Java program for the pattern of 'I' | Patterns in JAVA | JAVA POINT

 

Java Program for I



Ques - Write a java program for the following pattern of 'I' .


*****

   *

   *

   *

   *

*****



PROGRAM :


import java.util.Scanner;

class I

 {

     public static void main(String []args)

     {

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

         Scanner sc=new Scanner(System.in);

         int i, j, n;

         int r=sc.nextInt();


         for(i=0;i<r;i++)

         {

             for(j=0;j<5;j++)

             {

                 if((i==0)||(i==(r-1)))

                 {

                     System.out.print("*");

                 }    

                 else if(j==2)

                 {

                     System.out.print("*");

                 }    

                 else

                 {

                     System.out.print(" ");

                 }

             }

             System.out.println();

         }

        }

 }


/* Output :

 

Enter the length : 6


*****

   *

   *

   *

   *

*****

 

*/

Java program for the pattern of 'F' | Patterns in JAVA | JAVA POINT

 

Java Program for F



Ques - Write a java program for the following pattern of 'F' :


* * * *    

*     

*     

* * * * 

*      

*     


PROGRAM :


import java.util.Scanner;
public class PatternF
{
    public static void main(String[] args)
    {
        System.out.print("Enter the number of rows : ");

        Scanner scan=new Scanner(System.in);

        System.out.println();

        int n=scan.nextInt();

        for (int i=0;i<n ;i++ )
        {
            for (int j=0;j<=n/2 ;j++ )
            {
                if (i==0||i==n/2||j==0)
                {
                    System.out.print("* ");
                }
                else
                {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}


/*

Output:

Enter the number of rows : 7


* * * * 

*         

*                 

* * * * 

*         

*         

*         

*/


Related post:

Java codes for pattern design of alphabet 'G'

Java program for the pattern of 'E' | Patterns in JAVA | JAVA POINT

 

Java program for E



Ques : Write a java program for following Pattern of  'E' :


     * * * * 

            *                 

     *         

     * * * * 

     *         

             *                 

     * * * * 


PROGRAM :


import java.util.Scanner;

public class PatternE

{

    public static void main(String[] args)

    {

        System.out.print("Enter the number of      

        rows : ");.                // 7

        Scanner scan=new Scanner(System.in);

        int n=scan.nextInt();

        System.out.println();

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

        {

            for (int j=0;j<=n/2 ;j++ )

            {

                if (i==0 || i==n-1 || i==n/2 || j==0)

                {

                    System.out.print("* ");

                }

                else

                {

                    System.out.print("  ");

                }

            }

            System.out.println();

        }

    }

}

Java program for the pattern of 'D' | Patterns in JAVA | JAVA POINT

 

Java program for D


Ques : Write a Java program for the following pattern of 'D'


* * *  
*      * 
*      * 
*      * 
* * *  


PROGRAM :


import java.util.Scanner;
public class PatternD
{
    public static void main(String[] args)
    {
        System.out.print("Enter the number of rows : ");
        Scanner scan=new Scanner(System.in);
        System.out.println();
        int n=scan.nextInt();
        for (int i=0;i<n ;i++ )
        {
            for (int j=0;j<=n/2 ;j++ )
            {
                if (i==0 && j!=n/2 || i==n-1 && j!=n/2 || j==0 || j==n/2                                                                              && i!=0 && i!=n-1)
                {
                    System.out.print("* ");
                }
                else
                {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}


Output:


Enter the number of rows : 5


* * *   
*       * 
*       * 
*       * 
* * *  

Java program for the pattern of 'A' | Pattern Designs | JAVA POINT

 
Java codes for A

Ques : Write a Java program for the following pattern of 'A'


* * *
* *
* *
* * * * *
* *
* *
* *


PROGRAM :



import java.util.Scanner;

public class PatternA
{
public static void main(String[] args)
{
System.out.print("Enter the number of
rows : ");. // 9
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
System.out.println();
for (int i=0;i<n ;i++ )
{
for (int j=0;j<=n/2 ;j++ )
{
if (i==0 && j!=0 && j!=n/2 || j==0 &&
i!=0 || j==n/2 && i!=0 || i==n/2)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}


Also read :