write a menu driven program to accept a no do the fpllowing operation based on selection
1) palindrome no
2)fibonacci series
3)find the factorial of given no using recursion
Answers
Answered by
5
Hello
1)
Palindrome Number--
import java.util.Scanner;
class Palindrome{
public static void main(String args[]){
System.out.print("Enter Number: ");
Scanner read = new Scanner(System.in);
int num = read.nextInt();
int n = num;
//reversing number
int rev=0,rmd;
while(num > 0)
{
rmd = num % 10;
rev = rev * 10 + rmd;
num = num / 10;
}
if(rev == n)
System.out.println(n+" is a Palindrome Number!");
else
System.out.println(n+" is not a Palindrome Number!");
}
}
Output--Enter Number:
552255
552255 is a Palindrome Number!
3)
Factorial using recursion
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Factorial {
public static void main(String args[]) throws NumberFormatException, IOException{
System.out.println("Enter an integer to calculate it's factorial ");
//get input from the user
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
//call the recursive function to generate factorial
int result= fact(a);
System.out.println("Factorial of "+a+" is = " + result);
}
static int fact(int b)
{
if(b <= 1)
//if the number is 1 then return 1
return 1;
else
//else call the same function with the value - 1
return b * fact(b-1);
}
}
Output--Enter an integer to calculate it's Factorial
6
Factorial of 6 is = 720
Note:Can u tell me what is fibonacci series so that I can write a program on it.
1)
Palindrome Number--
import java.util.Scanner;
class Palindrome{
public static void main(String args[]){
System.out.print("Enter Number: ");
Scanner read = new Scanner(System.in);
int num = read.nextInt();
int n = num;
//reversing number
int rev=0,rmd;
while(num > 0)
{
rmd = num % 10;
rev = rev * 10 + rmd;
num = num / 10;
}
if(rev == n)
System.out.println(n+" is a Palindrome Number!");
else
System.out.println(n+" is not a Palindrome Number!");
}
}
Output--Enter Number:
552255
552255 is a Palindrome Number!
3)
Factorial using recursion
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Factorial {
public static void main(String args[]) throws NumberFormatException, IOException{
System.out.println("Enter an integer to calculate it's factorial ");
//get input from the user
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
//call the recursive function to generate factorial
int result= fact(a);
System.out.println("Factorial of "+a+" is = " + result);
}
static int fact(int b)
{
if(b <= 1)
//if the number is 1 then return 1
return 1;
else
//else call the same function with the value - 1
return b * fact(b-1);
}
}
Output--Enter an integer to calculate it's Factorial
6
Factorial of 6 is = 720
Note:Can u tell me what is fibonacci series so that I can write a program on it.
nitish8089:
ya ! if you in ICSE you can use switch for this...
Similar questions