Sei sulla pagina 1di 7

EXPERIMENT NUMBER: 1

Date of Performance :

Date of Submission :

AIM​: TO STUDY SELECTION & ITERATION CONTROL STATEMENTS

PROBLEM DEFINITION 1:

1A. Write a program to compute roots of quadratic equation

SOURCE CODE:

class EXPT1A

public static void main(String args[])

double a,b,c,d,r1,r2,real,imag; int select;

a=Integer.parseInt(args[0]);

b=Integer.parseInt(args[1]);
c=Integer.parseInt(args[2]);

d=(b*b)-(4*a*c);

if(d==0) select=0;

else if (d>0) select=1;

else select=2;

switch (select)

case 0:r1=(-b)/(2*a); r2=r1;

System.out.println("Roots are real and equal");

System.out.println("Roots are:"+r1+" and " +r2);

break;

case 1: r1=(-b+Math.sqrt(d))/(2*a);

r2=(-b-Math.sqrt(d))/(2*a);

System.out.println("Roots are real and distinct");

System.out.println("Roots are real" +r1+" and " +r2);

break;

case 2: d=-d; real=(-b)/(2*a); imag = (Math.sqrt(d))/(2*a);

System.out.println("Roots are real and imaginary");

System.out.println("Real part :"+real);

System.out.println("Imaginary part :"+imag);


break;

}}}

OUTPUT:

C:\Documents and Settings\test>d:

D:\>cd asl

D:\ASL>javac EXPT1A.java

D:\ASL>java EXPT1A 1 2 1

Roots are real and equal

Roots are:-1.0 and -1.0

D:\ASL>java EXPT1A 1 5 6

Roots are real and distinct

Roots are real-2.0 and -3.0

D:\ASL>java EXPT1A 1 5 7

Roots are real and imaginary

Real part:-2.5

Imaginary part: 0.8660254037844386


D:\ASL>

PROBLEM DEFINITION 2:

1B. Write a program to display entered integer number in words.

SOURCE CODE:

class EXPT1B

public static void main(String[] args)

int n, a, rev=0;

n=Integer.parseInt(args[0]);

while(n!=0)

rev=(rev*10)+(n%10);

n=n/10;
}

while(rev!=0)

a=rev%10;

rev=rev/10;

switch(a)

case 0: System.out.println("Zero"); break;

case 1: System.out.println("One"); break;

case 2: System.out.println("Two"); break;

case 3: System.out.println("Three"); break;

case 4: System.out.println("Four"); break;

case 5: System.out.println("Five"); break;

case 6: System.out.println("Six"); break;

case 7: System.out.println("Seven"); break;

case 8: System.out.println("Eight"); break;

case 9: System.out.println("Nine"); break;

}}}}
OUTPUT:

C:\Documents and Settings\test>d:

D:\>cd asl

D:\ASL>javac EXPT1B.java

D:\ASL>java EXPT1B 153

One

Five

Three

D:\ASL>java EXPT1B 4112

Four
One

One

Two

D:\ASL>

Potrebbero piacerti anche