Sunday 30 March 2014

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");

}

}

}


No comments:

Post a Comment