PROBLEM DEFINITION :- Write menu driven program to implement recursive Functions for following tasks. A) To find GCD and LCM b) To print n Fibonacci numbers c) To find reverse of number d) To solve PDF

Title PROBLEM DEFINITION :- Write menu driven program to implement recursive Functions for following tasks. A) To find GCD and LCM b) To print n Fibonacci numbers c) To find reverse of number d) To solve
Author U know Who i am
Course Information technology
Institution Datta Meghe Institute of Medical Sciences
Pages 7
File Size 91.8 KB
File Type PDF
Total Downloads 72
Total Views 128

Summary

Java programming 20201jddjkddksksksksnsnßnsnhddjdidkdjdjdkdkdkdkdllddlslekdjdjejsjeiejsjejejwjsjekend ehejejjd PROBLEM DEFINITION :-
Write menu driven program to implement recursive Functions
for following tasks. A) To find GCD and LCM b) To print n
Fibonacci numbers c) To find r...


Description

Experiment 06 IT/A/12 PROBLEM DEFINITION :Write menu driven program to implement recursive Functions for following tasks. A) To find GCD and LCM b) To print n Fibonacci numbers c) To find reverse of number d) To solve 1 +2+3+4+……..+(n- l )+n. THEORY :FIBONACCI SERIES: In mathematics, the Fibonacci numbers, commonly denoted Fₙ, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, and for n > 1. RECURSIVE FUNCTIONS :- A recursive function is a function that calls itself during its execution. The process may repeat several times, outputting the result and the end of each iteration. The function Count() below uses recursion to count from any number between 1 and 9, to the number 10 CODE :import java.util.*;

public class EXP6 {

public static int getGcd(int a, int b) { if (b == 0) { return a; } else { return getGcd(b, a % b); } }

static int n1=0,n2=1,n3=0; static void FibNum(int n) { if(n>0) { n3 = n1 + n2; System.out.print(" "+n3); n1 = n2; n2 = n3; FibNum(n-1); }

}

public static void reverseNumber(int number) { if (number < 10) { System.out.println(number); return; } else { System.out.print(number % 10); reverseNumber(number / 10); } }

public static int addNumbers(int num) { if (num != 0) return num + addNumbers(num - 1); else return num;

} public static void main(String[] args) { int z; Scanner sc = new Scanner(System.in); do{ System.out.println("\nChoose a Task to be performed from below "); System.out.println("\n1.To find GCD and LCM"); System.out.println("2.To print n Fibonacci numbers"); System.out.println("3.To find reverse of number"); System.out.println("4.To solve 1+2+3+4+........+(n- 1)+n"); System.out.println("5.EXIT"); System.out.print("INPUT : "); z = sc.nextInt();

switch (z) { case 1:

int a, b,lcm, gcd; System.out.println("Enter Two Number");

a = sc.nextInt(); b = sc.nextInt(); gcd = getGcd(a, b); lcm = (a * b) / gcd; System.out.println("LCM = " + lcm); System.out.println("GCD = " + gcd); break;

case 2: System.out.println("Enter Value Of n : "); int n = sc.nextInt(); System.out.println("Fibonacci Series up to " + n + " terms: "); System.out.print(n1 + " " + n2); FibNum(n - 2); System.out.println("\n"); break;

case 3: System.out.println("Enter the number that you want to reverse: ");

int num = sc.nextInt(); System.out.println("The reverse of the given number is: "); reverseNumber(num); break;

case 4:

System.out.println("Enter the value of n in order to find sum of n terms : "); int nt = sc.nextInt(); int sum = addNumbers(nt); System.out.print("Sum of first" + nt + "Natural Numbers is " + sum); break;

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

default: System.out.println("INVALID INPUT"); }

}while(z!=5); }

}...


Similar Free PDFs