0% found this document useful (0 votes)
21 views3 pages

Exp 3

The document contains three Java programs: the first calculates the power of a number, the second generates Fibonacci numbers up to a given term, and the third solves quadratic equations. Each program prompts the user for input and displays the results accordingly. Sample outputs for each program demonstrate their functionality with user-provided values.

Uploaded by

yenoj36493
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Exp 3

The document contains three Java programs: the first calculates the power of a number, the second generates Fibonacci numbers up to a given term, and the third solves quadratic equations. Each program prompts the user for input and displays the results accordingly. Sample outputs for each program demonstrate their functionality with user-provided values.

Uploaded by

yenoj36493
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

a)

import java.io.*;
import java.util.*;
class power_num
{
public static void main(String args[])
{
int x,y,p=1,a=1;
System.out.print("Enter x= ");
Scanner sc = new Scanner(System.in);
x= sc.nextInt();
System.out.print("Enter y= ");
y= sc.nextInt();
while(a<=y)
{
p=p*x;
a++;
}
System.out.println("Answer is= "+p);
}
}
output:
aditya@deux:~/SEA134$ javac power.java
aditya@deux:~/SEA134$ java power_num
Enter x= 5
Enter y= 3
Answer is= 125

b)

import java.lang.*;
import java.util.*;
class fibo
{
public static void main(String[] args)
{
int a=0,b=1,i,fib=1,n;
System.out.println("Enter n=>");
Scanner s=new Scanner(System.in);
n=s.nextInt();
for(i=0;i<=n;i++)
{
System.out.println(fib);
fib=a+b;
a=b;
b=fib;
}
}
}

Output:
aditya@deux:~$ cd SEA134
aditya@deux:~/SEA134$ java fibo.java
Enter n=>
5
1
1
2
3
5
8

3)
import java.lang.*;
import java.util.*;
class quadratic
{
public static void main(String[] args)
{
double a,b,c,d,x,x1,x2;
System.out.print("Enter a,b,c=>");
Scanner sc = new Scanner(System.in);
a= sc.nextInt();
b= sc.nextInt();
c= sc.nextInt();
d=Math.sqrt((b*b)-(4*a*c));
if(d==0)
{
x=-(b)/(2*a);
System.out.println("Equal roots are=>"+x);
}
else if(d>0)
{
x1=(-b+d)/2*a;
x2=(-b-d)/2*a;
System.out.println("Roots are=>"+x1+"\n"+x2);
}
else if(d<0)
{
System.out.print("Imaginary roots...");
}
}
}

Output:
aditya@deux:~/SEA134$ java root.java
Enter a,b,c=> 1 -2 1
Equal roots are=> 1.0
aditya@deux:~/SEA134$ java root.java
Enter a,b,c=> 1 -3 2
Roots are=> 2.0, 1.0

You might also like