20R21A04M4
WEEK-1
1.write a java program that prints all real solutions to the quadratic equation
ax^2+bx+c=0.Read a,b,c and use the quadratic formula,if the discriminant is b^2-4ac is
negative.Display a message stating that there are no real solutions.
AIM: To print all real solutions to quadratic equation and to display thet there are no real
solutions.
CODE:
import java.util.*;
class Quad
{
public static void main(String args[ ])
{
int a,b,c;
double x,y;
Scanner sc=new Scanner(System.in );
System.out.println("enter a,b,c values");
a=sc. nextInt( );
b=sc. nextInt( );
c=sc. nextInt( );
int k=b*b-(4*a*c);
if(k<0)
{
System.out.println("there is no reals"+k);
}
else
{
double l=Math.sqrt(k);
x=(-b-l)/2*a;
y=(-b+l)/2*a;
System.out.println("real values are:"+x+" "+y);
}
}
}
OUTPUTS:
2. write a java program that prints the Fibonacci sequence is defined by the following rules
are first sequence are 1 and 1 and the subsequent values are sum of the two values are
preceding it.
AIM: To print the Fibonacci sequence starting sequence with 1 and 1.
CODE:
import java.util.*;
class Fib
{
public static void main(String args[ ])
{
int x=1,y=1,z,n;
Scanner sc=new Scanner(System.in );
System.out.println("enter n value");
n=sc. nextInt( );
System.out.println(x+" "+y);
for(int i=2;i<n;++i)
{
z=x+y;
System.out.println("print the z value"+z);
x=y;
y=z;
}
}
}
OUTPUTS:
3.write a java program for addition of two numbers by using scanner class. Read the two
inputs.
AIM: To print the addition of two numbers.
CODE:
import java.util.*;
class Add
{
public static void main(String args[ ])
{
int x,y,z;
Scanner sc=new Scanner(System.in );
System.out.println("enter x value");
x=sc. nextInt( );
System.out.println("enter y value");
y=sc. nextInt( );
z=x+y;
System.out.println("Add of two numbers"+z);
}
}
OUTPUTS:
4.write a java program to implement given number is even or odd by using scanner
class.
AIM: To print whether the number is even or odd.
CODE:
import java.util.*;
class Evenodd
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in );
System.out.println("enter n value");
int n=sc. nextInt( );
if(n%2==0)
{
System.out.println("given number is even:"+n);
}
else
{
System.out.println("given number is odd:"+n);
}
}
}
OUTPUTS: