Showing posts with label How to Print Swastik Pattern in Java. Show all posts
Showing posts with label How to Print Swastik Pattern in Java. 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;
    }
  }
}



How to Print Swastik Pattern in Java | Best Pattern Programs in Java



java codes for swastik pattern



Write a Java Program to design a Swastik of given size.


In this program, we will learn about how to write the code for Swastik Pattern in Java by using stars, with the user-defined rows and columns.


To get the perfect Swastik shape, the user should input odd numbers for rows and columns so as to find the mid of rows and columns easily.


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


Program:


import java.util.Scanner;

class swastik
{
 public static void main(String args[])
 {
  int n,row, col;

  Scanner sc=new Scanner(System.in);

  System.out.println("Enter the size greater than 4 to get the perfect shape of the Swastik... ");

  n=sc.nextInt();

  if (n%2==0)

      n=n+1;  
      row=n;
      col=n;

  for (int i = 0; i < row; i++)
  {     
   for (int j = 0; j < col; j++)       
   {           
     if (i < (row / 2) )       
     {
      if (j < (col / 2)  )
      {
       if (j == 0)


        System.out.print("*");


       else        

   
        System.out.print(" "+ " ");         
      }


      else if (j == (col / 2))

         
        System.out.print(" *");       


      else         


      {
       if (i == 0)       

        
         System.out.print(" *");       
      }       
     }      

     else if (i == (row / 2)  )         


       System.out.print("* ");       


     else         
     { 
      if (j == (col / 2) || j == col - 1)     

      
       System.out.print("* ");
     
      else if (i == row - 1)           
      {


       if (j <= (col / 2) || j == col - 1)                 
         System.out.print("* ");         

  
       else            

 
         System.out.print(" "+ " ");         
      }              

      else     


        System.out.print(" "+" ");     
     } 
    }

    System.out.print("\n");
  }
 }
}


/*

Output:


 Enter the number greater than 4 to 

get the perfect shape of the Swastik  


 7


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


*/


Read also :

Java program for Special Numbers

Java program for Armstrong Numbers