Design a class Niven to check if a given number is a Niven number or not.
[A
number is said to be Niven if it is divisible by sum of its digits.
Example: 24 = 2 + 4=6 and 24 is divisible by 6
Some of the members of the class are given below:
Class name: Niven
Data members/instance variables:
num: to store the number
Methods/Member functions:
Niven(int nn): parameterized constructor to initialize the data member num=nn
int sum_of_digits(int i): returns the sum of digits of the number(num), , using a
recursive technique
void check(): checks whether the given number is Niven or not by invoking the
function sum_of_digits() and displays the result with an appropriate message
import [Link];
class Niven
{
int num;
public Niven(int nn)
{
num = nn;
}
int sum_of_digits(int i)
{
if (i == 0)
return 0;
else
return (i % 10 + sum_of_digits(i / 10));
}
public void check()
{
if(num%sum_of_digits(num)==0)
[Link](num + “ is a Niven Number”);
else
[Link](num + “ is not a Niven Number”);
}
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link](“Enter the number:”);
int n = [Link]();
Niven obj = new Niven(n);
[Link]();
}
}
Design a class Perfect to check if a given number is a perfect number or not. [A
number is said to be perfect if sum of the factors of the number excluding itself is
equal to the original number]
Example: 6 = 1 + 2 + 3 (where 1, 2 and 3 are factors of 6, excluding itself)
Some of the members of the class are given below:
Class name: Perfect
Data members/instance variables:
num: to store the number
Methods/Member functions:
Perfect (int nn): parameterized constructor to initialize the data member num=nn
int sumofFactors(int n): returns the sum of the factors of the number(num),
excluding itself, using a recursive technique
void check(): checks whether the given number is perfect by invoking the
function sum_of_factors() and displays the result with an appropriate message
Specify the class Perfect giving details of the constructor(), int
sum_of_factors(int) and void check().
Define a main() function to create an object and call the functions accordingly to
enable the task.
Answer:
import [Link];
class Perfect
{
int num;
int f;
public Perfect(int n)
{
n = num;
f = 1;
}
int sumofFactors(int i)
{
if(i==f)
{
return 0;
}
else if(i%f==0)
return f++ + sumOfFactors(i);
else
{
f++;
return sumOfFactors(i);
}
}
public void check()
{
if(num==sumOfFactors(num))
[Link](num + “ is a Perfect Number”);
else
[Link](num + “ is not a Perfect Number”);
}
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link](“Enter the number:”);
int n = [Link]();
Perfect obj = new Perfect(n);
[Link]();
}
}
A class Palin has been defined to check whether a positive
number is a Palindrome number or not.
The number ‘N’ is palindrome if the original number and it’s
reverse are the same.
Some of the members of the class are given below:
Class name: Palin
Data members/instance variables:
num: integer to store the number
revnum: integer to store the reverse of the number
Methods/Member functions:
Palin(): constructor to initialize data members with legal initial
values
void accept(): to accept the number
int reverse(int n,int revnum): reverses the parameterized
argument ‘n’ and stores it in revnum using a recursive technique
void check(): checks whether the number is a Palindrome by
invoking the function reverse() and display the result with an
appropriate message
import [Link].*;
class Palin
{
int num;
int revnum;
Palin()
{
num = 0;
revnum = 0;
}
void accept()
{
Scanner kb=new Scanner([Link]);
[Link]("Enter the Number:");
num = [Link]();
}
int reverse(int n, int revnum)
{
// base case
if (n == 0)
return revnum;
// stores the reverse
// of a number
revnum = (revnum * 10) + (n % 10);
return reverse(n / 10, revnum);
}
void check()
{
if(num == reverse(num))
{
[Link]("Number is palindrome");
}
else
{
[Link]("\n Number is not palindrome");
}
}
public static void main()
{
Palin p = new Palin();
[Link]();
[Link]();
}
}
Design a class ArmNum to check if a given number is an
Armstrong number or not.
[A number is said to be Armstrong if sum of its digits raised to
the power of length of the number is equal to the number]
Example:
371 = 33 + 73 + 13
1634 = 14 + 64 + 34 + 44
54748 = 55 + 45 + 75 + 45 + 85
Thus, 371, 1634 and 54748 are all examples of Armstrong
numbers.
Some of the members of the class are given below:
Class name: ArmNum
Data members/instance variables:
n: to store the number
len: to store the length of the number
Methods/Member functions:
ArmNum (int nn): parameterized constructor to initialize the data
member n = nn
int sum_pow(int i): returns the sum of each digit raised to the
power of the length of the number using recursive technique
For eg., 34 will return 32 + 42 (as the length of the number is 2)
void isArmstrong(): checks whether the given number is an
Armstrong number by invoking the function sum_pow () and
displays the result with an appropriate message.
Specify the class ArmNum giving details of the constructor( ), int
sum_pow(int) and void isArmstrong( ). Define a main() function
to create an object and call the functions accordingly to enable
the task.
Answer:
import [Link].*;
class ArmNum
{
int n;
int len;
ArmNum(int num)
{
n = num;
String s=[Link](n);
len=[Link]();
}
public int sumPow(int i)
{ if(i < 10)
return (int)[Link](i, len);
else
return (int)[Link](i % 10, len) + sumPow(i/10);
}
public void isArmstrong()
{
if(n == sumPow(n))
[Link](n+" is an Armstrong number.");
else
[Link](n+ " is not an Armstrong number.");
}
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter Number");
int num = [Link]();
ArmNum obj = new ArmNum(num);
[Link]();
}
}
A class Palin has been defined to check whether a positive
number is a Palindrome number or not.
The number ‘N’ is palindrome if the original number and it’s
reverse are the same.
Some of the members of the class are given below:
Class name: Palin
Data members/instance variables:
num: integer to store the number
revnum: integer to store the reverse of the number
Methods/Member functions:
Palin(): constructor to initialize data members with legal initial
values
void accept(): to accept the number
int reverse(int y): reverses the parameterized argument ‘y’ and
stores it in revnum using a recursive technique
void check(): checks whether the number is a Palindrome by
invoking the function reverse() and display the result with an
appropriate message
Specify the class Palin giving the details of the constructor (),
void accept(), int reverse(int) and void check().
Define the main() function to create an object and call the
functions accordingly to enable the task.
Answer:
import [Link].*;
class Palin
{
int num;
int revnum;
Palin()
{
num = 0;
revnum = 0;
}
void accept()
{
Scanner kb=new Scanner([Link]);
[Link]("Enter the Number:");
num = [Link]();
}
int reverse(int y)
{
int len =(y + " ").length();
if(len==1)
{
return y;
}
else
{
return(((y%10)*(int)[Link](10,len-1)) + reverse(y /10));
}
}
void check()
{
revnum = reverse(num);
if(num == revnum)
{
[Link]("Number is palindrome");
}
else
{
[Link]("Number is not palindrome");
}
}
public static void main()
{
Palin p = new Palin();
[Link]();
[Link]();
}
}
A disarium number is a number in which the sum of the digits to
the power of their respective position is equal to the number
itself. [10]
Example: 135 = 11 + 32 + 53
Hence, 135 is a disarium number.
Design a class Disarium to check if a given number is a disarium
number or not. Some of the members of the class are given
below:
Class name: Disarium
Data members/instance variables:
int num: stores the number
int size: stores the size of the number.
Methods/Member functions:
Disarium (int nn): parameterized constructor to initialize the
data members n = nn and size = 0
void countDigit(): counts the total number of digits and assigns it
to size
int sumofDigits (int n, int p): returns the sum of the digits of the
number(n) to the power of their respective positions (p) using
recursive technique
void check(): checks whether the number is a disarium number
and displays the result with an appropriate message
Specify the class Disarium giving the details of the
constructor! ), void countDigit(), int sumofDigits(int, int) and void
check(). Define the mainO function to create an object and call
the functions accordingly to enable the task.
Answer:
class Disarium
{
int num,size;
Disarium(int nn)
{
num=nn;
size=0;
}
void countDigit()
{
String s = [Link](n);
size = [Link]();
}
int sumOfDigits(int n, int p)
{
if(n==0)
return 0;
else
{
return (int)[Link](n%10,p)+sumOfDigits(n/10,--p);
}
}
void check()
{
if(sumOfDigits(num,size)==num)
[Link] (“Is a disarium no”);
else
[Link] (“Is not a disarium no”);
}
public static void main()
{
Disarium ob =new Disarium(135);
[Link]();
[Link]();
}
}
A number is said to be a magic number, if the sum of its
digits are calculated till a single digit recursively by
adding the sum of the digits after every addition. If the
single digit comes out to be 1,then the number is a
magic number.
For example-
Number= 50113
=> 5+0+1+1+3=10
=> 1+0=1
This is a Magic Number
import [Link];
class magicNumber
{
long num=0,temp=0;
public void accept()
{
Scanner sc=new Scanner([Link]);
[Link]("enter the number");
num=[Link]();
}
long SumOfDigits(long n)
{
if(n==0)
{
return 0;
}
else
{
return n%10 + SumOfDigits(n/10);
}
}
public void isMagicNumber()
{
temp=num;
while(temp>9)
{
temp=SumOfDigits(temp);
}
if(temp==1)
{
[Link](num+ "IS A MAGIC
NUMBER");
}
else
{
[Link](num+"IS NOT A MAGIC
NUMBER");
}
public static void main()
{
magicNumber ob1=new magicNumber();
[Link]();
[Link]();
}
}
An Emirp number is a number which is prime backwards and
forwards.
Example: 13 and 31 are both prime numbers. Thus,13 is an
Emirp number.
Design a class Emirp to check if a given number is Emirp number
or not. Some of the members of the class are given below:
Class name: Emirp
Data members / instance variables:
n:stores the number
rev:stores the reverse of the number
f:stores the divisor
Member functions:
Emirp (int nn):to assign n= nn, rev=0 and f=2
int isprime(int x):check if the number is prime using the
recursive technique and return 1 if prime otherwise return 0
void isEmirp(): reverse the given number and check if both the
original number and the reverse number are prime , by invoking
the function isprime(int) and display the result with an
appropriate message
Specify the class Emirp giving details of the constructor(int) , int
isprime(int) and void isEmirp(). Define the main() function to
create an object and call the methods to check for Emirp number
import [Link];
public class Emirp
{
int n,rev,f;
Emirp(int nn)
{
n=nn;
rev=0;
f=2;
}
int isprime(int x)
{
if(n==x)
{
return 1;
}
else if(n % x==0 || n==1)
{
return 0;
}
else
{
return isprime(x+1);
}
}
void isEmirp()
{
int x=n;
while(x!=0)
{
rev=(rev*10)+x%10;
x=x/10;
}
int ans1=isprime(f);
x=n;
n=rev;
f=2;
int ans2=isprime(f);
if(ans1==1 && ans2==1)
{
[Link](x+" is an Emirp number");
}
else
{
[Link](x+" is not an Emirp number");
}
}
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int x=[Link]();
Emirp obj1=new Emirp(x);
[Link]();
}
}
WAP to enter base(b) and exponent(e) and find the answer as be.
public class Power
{
public static void main ()
{
int base = 5, x = 3;
int p= power (base, x));
[Link]("Answer ="+p);
}
static int power (int base, int x)
{
if (x == 0) //Base Condition
return 1;
else
return (base * power (base, x - 1));
}
}
WAP to enter a number and find the factorial of that using recursive
function.
import [Link];
public class Factorial
{ int n,fact;
Factorial()
{
n=0;
fact=0;
}
int findFactorial(int num)
{
if(num==0)
return 1;
else if(num==1)
return 1;
else
return num*findFactorial(num-1);
}
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number :");
int n = [Link]();
Factorial ob=new Factorial();
int ans=ob. findFactorial( n)
[Link](“Factorial is=”+ans);
}
}
WAP to enter a decimal number and find its binary using recursive
function.
import [Link].*;
class Binary {
static int binaryConv(int n)
{
if (n == 1)
{
return 1;
}
return binaryConv(n / 2) * 10 + n % 2;
}
public static void main()
{
Scanner kb=new Scanner([Link]);
[Link](“Enter any decimal number”);
int n=[Link]();
[Link](binaryConv(n));
}
}
Second method
import [Link].*;
class Binary {
static int binaryConv(int n,String s)
{
if (n == 0)
{
return [Link](s);
}
else
{ s=[Link](n%2)+s;
return binaryConv(n / 2,s);
}
}
public static void main()
{
Scanner kb=new Scanner([Link]);
[Link](“Enter any decimal number”);
int n=[Link]();
String t=””;
[Link](binaryConv(n,t));
}
}
WAP to enter a String and reverse it using recursive function
class Stringreverse
{
static String reverse(String s, int p, String rev)
{
if(p<0)
return rev;
else
{
rev=rev+[Link](p);
return(reverse,s,p-1,rev);
}
}
public static void main()
{
Scanner kb=new Scanner([Link]);
[Link](“Enter any word”);
String n=[Link]();
int t=[Link]();
String r=reverse(n,t,””);
[Link](“Original String=”+n);
[Link](“Reverse string=+r);
}}