Showing posts with label JAVA codes. Show all posts
Showing posts with label JAVA codes. Show all posts

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

 

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


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 to check the Special number | JAVA POINT

Java Program for Special number


Write a Java program to check that a given number is Special number or not.


Special number

If the sum of the factorial of each digit of a number (N) is equal to the number itself, the number (N) is called a Special number or Peterson number or Krishnamurthy Number. 


PROGRAM :


import java.util.Scanner;

public class specialnum
{
 public static void main(String[] args)
 {
        
    int n, num, r,
    sum = 0;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number to check : ");
    n = sc.nextInt();
    num = n;
    while (num > 0)
    {
      r = num % 10;
      int fact=1;
      for(int i=1;i<=r;i++)
      {
        fact=fact*i;
      }
      sum = sum+fact;
            num = num / 10;
    }
    if(n==sum)
    {
     System.out.println("Special Number" );
     }
     else
     {
       System.out.println("Not Special Number" );
        }
    }

}



/*

Output :


Enter the number to be checked : 145

( 145=1!+4!+5!=1+24+120=145 )

145 is a Special number.

*/

Java program to check the Neon Number | JAVA POINT

 

Write a Java program to check that a given number is Neon number or not.


Neon number Java program



Neon number

A number is said to be a Neon Number if the sum of digits of the square of the number is equal to the number itself.


PROGRAM :


import java.util.*;

public class neon

{

    public static void main(String args[])

    {

        Scanner ob=new Scanner(System.in);

        System.out.print("\nEnter the number to be checked : ");

        int num=ob.nextInt();

        int square=num*num;

        int sum=0;

        while(square!=0)//Loop to find the sum of digits.

        {

            int a=square%10;

            sum=sum+a;

            square=square/10;

        }

        if(sum==num)

        {

            System.out.println(num+" is a Neon Number.");

        }

        else

        {

            System.out.println(num+" is not a Neon Number.");

        }

    }

}

/*

Output :


Enter the number to be checked : 9

 9 is a Neon Number.

(Workings 9*9=81 sum of digits of square = 8 + 1 = 9.)

*/



Java program to check the DISARIUM number | JAVA POINT

 

Write a Java program to check that a given number is  DISARIUM number or not.



DISARIUM number



DISARIUM number

A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the original number.


PROGRAM :


import java.util.Scanner;

class disarium

  public static void main(String[] args)

  {

   Scanner sc=new Scanner(System.in);

   System.out.print("\nEnter the number to be checked : ");

   int n=sc.nextInt();


   int copy = n, d = 0, sum = 0;

   String s = Integer.toString(n); //converting the number into a String

   int len = s.length(); //finding the length of the number i.e. no.of digits

       

   while(copy>0)

   {

    d = copy % 10; //extracting the last digit

    sum = sum + (int)Math.pow(d,len);

    len--;

    copy = copy / 10;

   }

             

   if(sum == n)

     System.out.println(n+" is a Disarium Number.");

   else

     System.out.println(n+" is not a Disarium Number.");

  }

}



/*

Output :


Enter the number to be checked : 135

135 is a Disarium Number.

(Workings 1+3*3+5*5*5 = 1+9+125 = 135

other DISARIUM are 89, 175, 518 etc)

*/



Java program to insert an element into the array | JAVA POINT

Write a Java program to insert an element into the array.


Java program to insert an element into the array



PROGRAM :


import java.util.*;
class arrinsert
{  
 public static void main(String[] args)
 {
  int n,pos,x;


  Scanner sc=new Scanner(System.in);
  System.out.print("\nEnter size of array : ");
  n=sc.nextInt();


  int a[]=new int[n+1];


  System.out.print("\nEnter all the elements of array : \n");
  
  System.out.println();


  for(int i=0; i<n; i++)
  {
   a[i]=sc.nextInt();
  }


  System.out.println("\nBefore insertion the array is :");


  for(int i=0; i<n; i++)
  {
   System.out.print(a[i]+" ");
  }


  System.out.println();


  System.out.print("\nEnter the position where you want to insert the element: ");
  pos=sc.nextInt();


  System.out.print("\nEnter the element you want to insert : ");
  x=sc.nextInt();


  for(int i=(n-1); i>=(pos-1); i--)
  {
   a[i+1]=a[i];
  }
  a[pos-1]=x;


  System.out.println("\nAfter insertion");


  for(int i=0; i<n; i++)
  {
   System.out.print(a[i]+",");
  }
  System.out.print(a[n]);
 }
}


/*

Output :


Enter size of array : 5

Enter all the elements of array : 

4 6 2 6 7 

Before insertion the array is :
4 6 2 6 7 

Enter the position where you want to insert the element: 3

Enter the element you want to insert : 8

After insertion
4,6,8,2,6,7

*/

Java Program to display all the Prime numbers between 1 and n

Write a Java Program to display all the prime numbers between 1 to n (the number n is entered by user).


Prime numbers



Prime Numbers


Prime numbers are the numbers that have only two factors, that are, 1 and the number itself.


PROGRAM :


import java.util.Scanner;

class PrimeNumbers
{
   public static void main (String[] args)
   {
      Scanner scanner = new Scanner(System.in);
      int i =0;
      int num =0;


      String  primeNumbers = " ";


      System.out.println("Enter the value of n:");
      int n = scanner.nextInt();


      for (i = 1; i <= n; i++)     
      {   
         int counter = 0;

  
         for(num =i; num>=1; num--)
         {
    if (i%num == 0)
    {
counter = counter + 1;
    }
}


if (counter == 2)

    primeNumbers = primeNumbers + i + " "; 

           //Appended the Prime number to the String
}
      }


      System.out.println("Prime numbers from 1 to n are :");
      System.out.println(primeNumbers);
   }
}


/*

Output:

Enter the value of n:

100

Prime numbers from 1 to n are :

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

*/


Java Program to check the Special Number

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 'B' | JAVA Patterns | JAVA POINT

 

Java codes for B

Ques : Write a Java Program for the following pattern 'B'


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


PROGRAM :


import java.util.Scanner;
public class PatternB
{
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!=n/2 || j==0 || j==n/2 &&
i!=0 && i!=n/2 && i!=n-1 || i==n/2
&& j!=n/2 || i==n-1 && j!=n/2)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}