Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
12Activity
0 of .
Results for:
No results containing your search query
P. 1
Java Lab Manual -Yna

Java Lab Manual -Yna

Ratings: (0)|Views: 1,501|Likes:
Published by Amar Singh
jntu cse java lab manual
jntu cse java lab manual

More info:

Categories:Types, Research
Published by: Amar Singh on Apr 27, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/29/2013

pdf

text

original

 
 
Object Oriented 
 
 Programming Lab
 
 Manual
 
 A. Yogananda
1
 
 Week1 :
 
a) Write a Java program that prints all real solutions to the quadratic equation ax2 + bx + c = 0.Read in a, b, c and use the quadratic formula. If the discriminant b2 -4ac is negative, display amessage stating that there are no real solutions.
 import java.io.*; import java.math.*; class RealSolution{public static void main(String[] args)throws IOException{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));System.out.println("Enter value for a ");int a=Integer.parseInt(br.readLine());System.out.println("Enter value for b ");int b=Integer.parseInt(br.readLine());System.out.println("Enter value for c ");int c=Integer.parseInt(br.readLine());int d=(b*b)-(4*a*c);if(d>0) {System.out.println("roots are real distinct");double r1= -b+(Math.sqrt(d)/(2*a)); double r2= -b-(Math.sqrt(d)/(2*a)); System.out.println("r1="+Math.round(r1)+"\n r2="+r2);}else if(d<0)System.out.println("roots are imaginary and complex");else{}} }System.out.println("roots are equal");double r1= -b/(2*a); double r2= -b/(2*a); System.out.println("r1 ="+Math.round(r1)+"\nr2="+r2);
OUTPUT:
 C:\nanda>javac RealSolution.java C:\nanda>java RealSolutionEnter value for a Enter value for a 1 1 Enter value for b Enter value for b 1 3Enter value for c Enter value for c 1 2 roots are imaginary and complex roots are real distinct r1 =-2r2=-3.5
 A. Yogananda
2
 
b) The Fibonacci sequence is defined by the following rule:
 
The fist two values in the sequence are 1 and 1. Every subsequent value is the sum of the twovalues preceding it. Write a Java program that uses both recursive and non recursive functions toprint the nth value in the Fibonacci sequence.
 import java.io.*; public class Fibonacci {public static void main(String[] args)throws IOException{System.out.println("Enter the value for n ");BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine());System.out.println("Fibonacci numbers Using Recursion");for (int i = 0; i <= n-1; i++){int f = fib(i);System.out.println("fib(" + i + ") = " + f); }System.out.println("Fibonacci numbers with Out Using Recursion");int a=0,b=1; System.out.print(a+"\t"+b);for(int i=0;i<n-2;i++) {int c=a+b;a=b;b=c;System.out.print
\t
+(c); } }public static int fib(int n){if (n < 2)return n;else return fib(n - 1) + fib(n - 2); }}
OUTPUT:
 C:\nanda\programs>javac Fibonacci.javaC:\nanda\programs>java Fibonacci Enter the value for n 5 Fibonacci numbers Using Recursionfib(0) = 0fib(1) = 1fib(2) = 1fib(3) = 2fib(4) = 3Fibonacci numbers with Out Using Recursion 0 1 1 2 3
 A. Yogananda
3

Activity (12)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
Saurabh Gandhi liked this
Saurabh Gandhi liked this
chandrasekar C liked this
chandrasekar C liked this
prasad liked this
Armin Suljovic liked this
Eden Garcia liked this
subramanyam62 liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->