Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Friday, 11 July 2014

Program for Full Pyramid in Java

Program for Full Pyramid and size can be input by user.

import java.util.*;
public class Pyramid
{
    public static void main(String Args[])  
    {
        int n;
        Scanner sc = new Scanner(System.in);
               n= sc.nextInt();
   for (int i = 1; i <= n; i++)
            {
                  for (int j = 0; j < (n - i); j++)
                        System.out.print("   ");
                  for (int j = 1; j <= i; j++)
                        System.out.print(" * ");
                  for (int k = 1; k < i; k++)
                        System.out.print(" * ");
                  System.out.println();
            }

            for (int i = n - 1; i >= 1; i--)
            {
                  for (int j = 0; j < (n - i); j++)
                        System.out.print("   ");
                  for (int j = 1; j <= i; j++)
                        System.out.print(" * ");
                  for (int k = 1; k < i; k++)
                        System.out.print(" * ");
                  System.out.println();
            }

            System.out.println();
     
    }
 
}

Thursday, 10 July 2014

Program for Right Triangle in Java

This is the program for Triangle and on place of "*" you can use any other variable as you want to print.

public class Triangle
{
    public static void main(String Args[])  
    {
    int i,j;
    for(i=0;i<10;i++)
 
    {
        System.out.println(" * ");
        for (j=0;j<=i;j++)
        {
            System.out.print(" * ");
       }}

    }
}

Wednesday, 9 July 2014

Program for Switch case with two cases

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
i=2;
switch(i)
{
case 1:
{
printf("case 1");
}
case 2:
{
printf("case 2");
break;
}
default:
{
printf("default");
}
getch();
}
}

Program for greatest of four numbers in java

Program is created using Switch-case and if else both. Switch case can be easily removed from this program.


import java.util.*;
public class Switchcase
{
    public static void main (String args[])
    {
        Scanner sc = new Scanner (System.in);
    System.out.println("please enter value of a");
    int a = sc.nextInt();
     System.out.println("please enter value of b");
    int b = sc.nextInt();
     System.out.println("please enter value of c");
    int c = sc.nextInt();
     System.out.println("please enter value of d");
    int d = sc.nextInt();
  int e=1;
  switch(1)
  {
      case 1:
          if (a>b&&a>c&&a>d)
          {
               
              System.out.println("Number a is greater than all");
          }
    else
     {
      if(b>a&&b>c&&b>d)
     {
          System.out.println("Number b is greater than all");
      }
       else
      {
      if(c>a&&c>b&&c>d)
      {
        System.out.println("Number c is greater than all");
      }
      else
      {
          if(d>a&&d>b&&d>c)
          {
              System.out.println("Number d is greater than all");
          }
            else
          {
                System.out.println("All numbers are equal");
      }
     }
    }
   }
  }
 }
}