0% found this document useful (0 votes)
80 views

Source Code: 7.write A Program To Show Fibonacci Series Up To N-TH Terms Using Recursion

The documents provide source code for 12 Java programs that solve problems related to recursion, series summation, simple interest calculation, solving quadratic equations, finding prime numbers within a range, and calculating the greatest common divisor of two numbers. The source code includes comments explaining the purpose and includes sample inputs/outputs for each program.

Uploaded by

RAHUL DAS
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)
80 views

Source Code: 7.write A Program To Show Fibonacci Series Up To N-TH Terms Using Recursion

The documents provide source code for 12 Java programs that solve problems related to recursion, series summation, simple interest calculation, solving quadratic equations, finding prime numbers within a range, and calculating the greatest common divisor of two numbers. The source code includes comments explaining the purpose and includes sample inputs/outputs for each program.

Uploaded by

RAHUL DAS
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/ 5

7.

Write a program to show Fibonacci series up to n-th terms using recursion


Source Code
import java.util.Scanner;
public class Program7 {
public static void main(String args[]) {
System.out.println("Enter number upto which Fibonacci series
to print: ");
int number = new Scanner(System.in).nextInt();
System.out.println("Fibonacci series upto " + number +"
numbers: ");
for(int i=1; i<=number; i++){
System.out.print(fibonacci(i) +" ");
}
}
public static int fibonacci(int number){
if(number == 1 || number == 2){
return 1;
}
return fibonacci(number-1) + fibonacci(number -2);
}
}
Output
Enter number upto which Fibonacci series to print:
19
Fibonacci series upto 19 numbers :
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

8.Write a Program of Sum of Series (1+x+x2+x3+x4+………… up to n-th terms).

Source Code
import java.util.Scanner;
public class Program8 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the value of N");
int n=in.nextInt();
System.out.println("Enter the value of X");
int x=in.nextInt();
int sum=0;
for(int i=0;i<=n;i++) {
sum+=Math.pow(x, i);
}
System.out.println("The sum:"+sum);
}
}

Output
Enter the value of N
5
Enter the value of X
7
The sum:19608

9.Write a program to calculate the simple interest (si) while your inputs are
principle (p), time in years (n) and rate of interest (r) [take input using command-
line argument].

Source Code

public class Program9 {


public static void main(String[] args)
{
double p, t, r;
p=Double.parseDouble(args[0]);
t=Double.parseDouble(args[1]);
r=Double.parseDouble(args[2]);
double si=(p*r*t)/100;
System.out.format("Simple Interest: %.2f", si);
}
}

Output
Java Program9 5000 3 20
Simple Interest: 3000.00

10. Write a program to find the real roots of the quadratic equation ax2 + bx + c =
0 where a, b and c are constants
Source Code
import java.util.Scanner;
public class Program10 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
double a, b, c, root1, root2;
System.out.println("Enter the value of a:");
a=in.nextDouble();
System.out.println("Enter the value of b:");
b=in.nextDouble();
System.out.println("Enter the value of c:");
c=in.nextDouble();
root1=(-b-Math.sqrt(Math.pow(b, 2)-4*a*c))/(2*a);
root2=(-b+Math.sqrt(Math.pow(b, 2)-4*a*c))/(2*a);
System.out.format("Root1:%.2f Root2:%.2f", root1, root2);
}
}
Output

Enter the value of a:


1
Enter the value of b:
9
Enter the value of c:
2
Root1: -8.77 Root2: -0.23

11.WAP to print all prime number within a given range

Source Code
import java.util.Scanner;
public class Program11 {
public static boolean checkPrime(int n) {
int c=0; for(int i=2;i<n/2;i++) {
if(n%i==0) {
c++;
}
}
if(c>0) {
return false;
}
else{
return true;
}
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int a, b;
System.out.println("Enter the lower range");
a=in.nextInt();
System.out.println("Enter the upper range");
b=in.nextInt();
System.out.println("The prime numbers in this range are-");
for(int i=a;i<=b;i++){
if(checkPrime(i)==true) {
System.out.print(i+" ");
}
}
}
}
Output
Enter the lower range
1
Enter the upper range
100
The prime numbers in this range are-
1 2 3 4 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83
89 97

12.WAP to calculate GCD of two numbers


Source Code
import java.util.Scanner;
public class Program12{
public static int findGcd(int a, int b) {
if(b%a==0) {
return a;
}
else {
return findGcd(b%a, a);
}
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int a, b;
System.out.println("Enter the two numbers");
a=in.nextInt();
b=in.nextInt();
int g, s;
g=(a>b)?a:b;
s=(a<b)?a:b;
int gcd=findGcd(s,g);
System.out.println("Gcd:"+gcd);
}
}
Output
Enter the two numbers
10
12
Gcd:2

You might also like