Showing posts with label JAVA PROGRAMS-For. Show all posts
Showing posts with label JAVA PROGRAMS-For. Show all posts

Sunday, 30 March 2014

JAVA PROGRAMS-Stack Program In Java

Stack Program In Java

Program Name:- stackdemo.java

import java.io.*;
class stack
{
int s[]=new int[100];
int top;
void stack()
 {
 top=0;
 System.out.println("\n The stack is empty");
 }
public void push()throws IOException
 {
 DataInputStream inp=new DataInputStream(System.in);
 int x;
 if(top==100)
  {
  System.out.println("The Stack is full");
  }
 else
  {
  System.out.print("\n Enter the value  :");
  x=Integer.parseInt(inp.readLine());
  s[top]=x;
  top++;
  System.out.println("\n The value is pushed");
  }
 }
public void pop()throws IOException
 {
 DataInputStream inp=new DataInputStream(System.in);
 int y;
 if(top==0)
  {
  System.out.println("\n The stack is empty");
  }
 else
  {
  top--;
  y=s[top];
  s[top]='\0';
  System.out.println("\n The value is poped");
  }
 }
void display()
 {
 int i;
 if(top==0)
 {
  System.out.println("\nThe stack is empty");
  }
 else
  {
   System.out.println("\nValues are top-->");
  }
 for(i=top-1;i>=0;i--)
  System.out.println(s[i]);
 }
}
public class stackdemo
{
public static void main(String args[])throws IOException
 {
 stack in=new stack();
 int item=0;
 int opt=1;
 String s=new String();
 while(opt<4)
 {
 System.out.println("\n\n\t\t Main menu\n\n1.push\n\n2.Pop\n\n3.Display\n\n4.Exit\n\nSelection the option");
 DataInputStream din=new DataInputStream(System.in);
 s=din.readLine();
 opt=Integer.parseInt(s);
 switch(opt)
  {
   case 1:
   System.out.println("\nEnter the elementis add");
   in.push();
   break;
   case 2:
   System.out.println("\nEnter the delete item");
   in.pop();
   break;
   case 3:
   System.out.println("\nThe stack element is:");
   in.display();
   break;
   default:
   opt=4;
   }
  }
 }
}

JAVA PROGRAMS-For,While,Do-While,Ternary Loops programs in Java

For,While,Do-While,Ternary Loops programs in Java

Program Name:- simplepgm2

import java.io.*;

class simplepgm

{

void for_fun()

{

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

for(int i=1;i<=5;i++)

{

for(int j=1;j<=i;j++)

{

System.out.println(""+j);

}

System.out.println("\n");

}

}

void while_fun()

{

int a=0,b=1,c=1;

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

System.out.println(a+"\n"+b+"\n");

while(c<=0)

{

int f=a+b;

System.out.println(f+"\n");

c+=1;

a=b;

b=f;

}

}

 void dowhile_fun()

 {

 int sum=0,i=1;

 do

{

sum=sum+i;

i+=1;

}

while(i<=10);

System.out.println("sum="+sum);

}

void ternary_fun()

{

int a=10,b=15,c=5;

int x=(a>b&&a>c)?a:(b>c)?b:c;

System.out.println(x+" is big");

}

}

class simplepgm2

{

public static void main(String arg[])throws IOException

{

char choice;

simplepgm obj=new simplepgm();

do

{

System.out.println("\n 1.For funtion");

System.out.println("\n 2.While funtion");

System.out.println("\n 3.do while funtion");

System.out.println("\n 4.Ternary funtion");

choice=(char)System.in.read();

}while(choice<'1'||choice>'8');

switch(choice)

{

case '1':

obj.for_fun();

break;

case '2':

obj.while_fun();

break;

case '3':

obj.dowhile_fun();

break;

case '4':

obj.ternary_fun();

break;

default:

System.out.println("not current choice");

}

}

}