0% found this document useful (0 votes)
38 views10 pages

RECURSIVE

The document contains multiple Java programs that demonstrate various mathematical concepts and number classifications, including converting hexadecimal to decimal, identifying evil numbers, checking for pronic numbers, palindromes, disarium numbers, Dudeney numbers, calculating LCM and HCF, and determining perfect and Armstrong numbers. Each program is structured with a main method that executes the respective functionality and prints the results. The implementations utilize recursion, mathematical operations, and user input to achieve their objectives.

Uploaded by

antarikhyag
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)
38 views10 pages

RECURSIVE

The document contains multiple Java programs that demonstrate various mathematical concepts and number classifications, including converting hexadecimal to decimal, identifying evil numbers, checking for pronic numbers, palindromes, disarium numbers, Dudeney numbers, calculating LCM and HCF, and determining perfect and Armstrong numbers. Each program is structured with a main method that executes the respective functionality and prints the results. The implementations utilize recursion, mathematical operations, and user input to achieve their objectives.

Uploaded by

antarikhyag
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/ 10

1.

HEX TO DEC
import java.util.*;
class DeciHex {
String Hex = "";
void hex(int deci) {
if (deci != 0) {
int k = 0;
char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F'};
k = deci % 16;
Hex = hex[k] + Hex;
deci /= 16;
hex(deci);
}
}
public static void main(String[] args) {
DeciHex ob = new DeciHex();
ob.hex(4557);
System.out.println(ob.Hex);
}
}
2. Evil Number using recursion
import java.util.*;
class Evil {
int bin=0;
int binary(int deci) {
if (deci==0)
return 0;
else{
bin=(deci%2)+10;
return bin* binary(deci/2);
}
}
public static void main(String[] args) {
Evil ob=new Evil();
int c=0;
int k=ob.binary(17);
while(k>0){
if(k%10==1)
c=c+1;
k/=10;
}
if (c%2==0)
System.out.println("ITS AN EVIL NUMBER");
else
System.out.println("NOT AN EVIL NUMBER");
}
}
3.PRONIC NO.
import java.util.Scanner;
public class Pronic {
public static boolean isPronic(int n) {
int sqrt = (int) Math.sqrt(n);
return (n == sqrt * (sqrt + 1)) || (n == (sqrt - 1) * sqrt);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (isPronic(number)) {
System.out.println(number + " is a pronic number.");
} else {
System.out.println(number + " is not a pronic number.");
} }}
4.Palindrome
import java.util.*;
class palin
{
static int rev(int n, int temp)
{
if (n == 0)
return temp;
temp = (temp * 10) + (n % 10);
return rev(n / 10, temp);
}
public static void main (String[] args)
{
int n = 121;
int temp = rev(n, 0);
if (temp == n)
System.out.println("yes");
else
System.out.println("no" );
}
}
5.Disarium
import java.util.Scanner;
class Disarium{
int num; int size;
public Disarium(int nn){
num = nn; size = 0;
}
public void countDigit(){
size = String.valueOf(num).length();
}
public int sumofDigits(int n, int p){
if(n < 10)
return n;
int term = (int)Math.pow(n % 10, p);
return term + sumofDigits(n / 10, p - 1);
}
public void check(){
if(num == sumofDigits(num, size))
System.out.println("Disarium number!");
else
System.out.println("Not a disarium number.");
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = Integer.parseInt(in.nextLine());
Disarium obj = new Disarium(n);
obj.countDigit();
obj.check();
}
6. Dudeney no.
import java.util.Scanner;
public class NumDude
{
int num;
public NumDude()
{
num=0;
}
public void input()
{
Scanner in = new Scanner(System.in); System.out.print("Enter the number:
");
num = in.nextInt();
}
public int sumDigits (int x)
{
if (x==0)
return 0;
else
return x%10 +sumDigits (x/10);
}
public void isDude()
{
int sdig=sumDigits (num);
if((sdig*sdig*sdig)== num)
System.out.println("Dudency number ");
else
System.out.println("Not a Dudency number ");
}
public static void main(String args[]) {
NumDude ob=new NumDude();
ob.input();
ob.isDude();
}}
8.LCM
class Main {
static int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
public
static void main(String[] args) {
int a = 15, b = 20;
System.out.println("LCM of " + a + " and " + b + " is " + lcm(a, b));
}
}
9.HCF
public class Main
{
static int gcd(int num1, int num2)
{
if (num1 == 0)
return num2;
if (num2 == 0)
return num1;
if (num1 == num2)
return num1;
if (num1 > num2)
return gcd(num1-num2, num2);
return gcd(num1, num2-num1);
}
public static void main(String[] args)
{
int num1 = 98, num2 = 56;
System.out.println("GCD of " + num1 +" and " + num2 + " is " +
gcd(num1, num2));
}
}
10.PERFECT NUMBER
import java.util.Scanner;
class Perfect{
int num;
public Perfect(int nn){
num = nn;
}
public int sum_of_factors(int i){
if(i > num / 2)
return 0;
if(num % i == 0)
return i + sum_of_factors(i + 1);
return sum_of_factors(i + 1);
}
public void check(){
if(num == sum_of_factors(1))
System.out.println("Perfect number!");
else
System.out.println("Not a perfect number.");
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n =sc.nextInt();
Perfect obj = new Perfect(n);
obj.check();
}
}
11.ARMSTRONG
public class Main
{
public static void main (String[]args)
{
int num = 1634, len;
len = order (num);
if (armstrong (num, len))
System.out.println(num + " is armstrong");
else
System.out.println(num + " is armstrong");
}
static int order (int x)
{
int len = 0;
while (x != 0 )
{
len++;
x = x / 10;
}
return len;
}
static boolean armstrong (int num, int len)
{
int sum = 0, temp, digit;
temp = num;
while (temp != 0)
{
digit = temp % 10;
sum = sum + (int)Math.pow(digit, len);
temp /= 10;
};
return num == sum;
}
}

You might also like