Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 48

Computer Project

Name: Mohd. Anwar

Class: 11-C

Internal Examiner’s
sign:

External Examiner’s
sign:

1
Acknowledgement
I  would like to express my special thanks
of gratitude to my teacher Atul Sir as
well as our Senior Principal Mrs. Jyoti
Kashyap and Principal Mrs. Shivani
Singh who gave me the golden
opportunity to do this wonderful project
on the topic (Java Programs), which also
helped me in doing a lot of Research and
I came to know about so many new
things I am really thankful to them.
Secondly, I would also like to thank my
parents and friends who helped me a lot
in finalizing this project within the
limited time frame.

2
Program 1
Q). Special factorian number is a number whose sum of
factorial of its digits is equal to the number itself.
For example: 145 ; 5!+4!+1! = 120+24+1 = 145
Write a program in java to design a class CHECK with
following details:
Data Members:
n : integer to store a number
Member Functions:
CHECK() : constructor to assign 0 to n
void input() : accepts value of integer n
int fact(int X) : returns the factorial of X
void isspecial() : checks whether n is a special
factorian number or not and prints
the appropriate message
Specify the class CHECK giving details of the
constructor and all the functions. Define main() function
to create the object of the class and call the required
functions.

import java.util.*;
class CHECC
{
int n;
public CHECC()
{

3
n=0;
} // default constructor
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
n = sc.nextInt();
}// for taking input from the user
public int fact(int X)
{
int p=1;
int t=X;
for(int i=1;i<=t;i++)
{
p=p*i; // calculating the factorial
}
return p;
}
public void isSpecial()
{
int t=n;
int s=0;
while(t!=0)
{
int d=t%10;
int f=fact(d); // invoking function fact() to find the
// factorial of the digit
s=s+f; // calculating sum of factorial of digits
t=t/10;
}
if (s==n) // checking if sum of factorial of digits is
// equal to number or not

4
System.out.println(n+" is a special factorian
number");
else
System.out.println(n+" is not a special factorian
number");
}
public static void main ()
{
CHECC obj = new CHECC(); // creating object
obj.input(); // invoking the functions
obj.isSpecial();
}
}

OUTPUT
Enter a number
145
145 is a special factorian number

5
Program 2
Q). A Special two-digit number is a number such that
when the sum of its digits is added to the product of its
digits , the result is equal to the original two-digit
number.
For example: 59 Sum of digits = 14 Product of digits =
45 Sum of ‘Sum of digits’ and ‘Product of digits’ =
14+45=59
Design a class Special to print such special two-digit
numbers in the specified range.
The details of the members of the class are given below:

Data Members:
m,n : integers to specify the range
cnt : to count the number of special
two-digit numbers
Member Functions:
Special() : default constructor
void readRange() : accepts two integers
boolean checkSpecial(int x) : checks whether a
two-digit number is
special or not.Returns
true if it is special
otherwise returns false
void countSpecial() : counts and displays the
special two-digit numbers
in the given range
void disp() : displays the range and
the count of special
two-digit numbers with
appropriate messages

6
Specify the class Special giving details of the
constructor and the member functions void readRange(),
boolean checkSpecial(int); void countSpecial() and void
disp(). Define the main() function to create an object and
call the functions in order to achieve the task

import java.util.*;
class Special
{
int m,n;
int cnt;
public Special()
{
m=0;
n=0;
cnt=0;
} // default constructor
public void readRange()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the range");
m=sc.nextInt();
n=sc.nextInt();
} // taking the input(range) from the user
public boolean checkSpecial(int x)
{
int t=x;
int s=0;
int p=1;
while(t!=0)
{
int d=t%10;
s=s+d; // calculating the sum of digits

7
p=p*d; // calculating the product of digits
t=t/10;
}
int A=s+p;
if (A==x) // checking if sum of ‘Sum of digits’ and
// the ‘Product of digits’ is equal to the
// original number
return true;
else
return false;
}
void countSpecial()
{
for (int i=m;i<=n;i++)
{
boolean r = checkSpecial(i);// invoking function
// to check whether i is a special number or not
if(r)
{
cnt++; // counting the number of special
// numbers
System.out.println(i);
}
}
}
public void disp()
{
System.out.println("The entered range from "+m+"
to "+n); // displaying the range
System.out.println("The number of special two digit
numbers in the range is "+cnt); // displaying the number
// of special numbers present in the inputted range
}

8
public static void main()
{
Special obj = new Special();
obj.readRange();
obj.countSpecial(); // invoking functions
obj.disp();
}
}

OUTPUT
Enter the range
0
100
19
29
39
49
59
69
79
89
99
The entered range from 0 to 100
The number of special two digit numbers in the range
is 9

9
Program 3
Q). Define a class Series with the following details:
Data Members:
int N : to accept N from the user
Member Functions:
void input() : to accept N from the user
double sum(int x) : to return the sum of the series
1+2+.....+x terms
double fact(int x) : to calculate and return the factorial
of x
void call() : to print the sum of the following
series up to N terms by using
methods of the class
1/1!+(1+2)/2!+(1+2+3)/3!+.......N terms
Write the method main to create the object of the class
Series and call functions accordingly to enable the task.

import java.util.*;
class Series
{
int N;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of terms");
N = sc.nextInt();
} // for taking the input from the user
double sum(int x)

10
{
double s=0.0;
int c=1;
for (int i=0;i<x;i++)
{
s=s+c; // calculating the sum of the series
// 1+2+….x terms
c++;
}
return s;
}
double fact(int x)
{
double p=1.0;
for ( int i=1;i<=x;i++)
{
p=p*i; // calculating the factorial of x
}
return p;
}
void call()
{
double sum=0;
int c=1;
for (int i=1;i<=N;i++)
{
double s = sum(c); // invoking functions
double f = fact(c);
sum =sum+(s/f); // calculating sum of the series

11
// required in the question
c++;
}
System.out.println("Sum of the series is "+sum);
}
static void main ()
{
Series obj = new Series();
obj.input();
obj.call();
}
}

OUTPUT
Enter the number of terms
3
Sum of the series is 3.5

12
Program 4

Q). Design a class NUM with the given details to print all
prime palindrome numbers between the given limits
Class name : NUM
Data Members:
int low : stores lower limit
int high : stores upper limit
Member Functions:
NUM(int min,int max) : to initialise the data members
by min and max
boolean isPrime(int x) : returns true if x is prime else
returns false
int isPalindrome(int x) : returns 1 if x is palindrome else
returns 0
void printPrimePalindrome() : prints all the numbers from
high to low which are
prime as well as palindrome
You need not write the main() function.

import java.util.*;
class NUM
{
int low;
int high;
public NUM(int min,int max)
{
low=min;

13
high=max;
} // parameterized constructor
boolean isPrime(int x)
{
int t=x;
int c=0;
for (int i=1;i<=t;i++)
{
if(t%i==0) // checking if i is a factor of t
c++; // counting the number of factors
}
if(c==2)
return true;
else
return false;
}
public int isPalindrome(int x)
{
int t=x;
int rev=0;
while(t!=0)
{
int d=t%10;
rev=(rev*10)+d; // calculating the reverse of the
number x
t=t/10;
}
if (rev==x) // checking if reverse number is same as
original number

14
return 1;
else
return 0;
}
public void printPrimePalindrome()
{
for (int i=low;i<=high;i++)
{
boolean r=isPrime(i); // checking if i is prime or
not
int q=isPalindrome(i); // checking if i is
palindrome or not
if(r && q==1)
System.out.println(i);
}
}
}

OUTPUT
101
131
151
181
191
313
353
373
383

15
Program 5

Q). A library charges fine for returning the book late. As


per the rule, a book can be retained for 7 days without
any fine. After 7 days a fine will be charged for the
excess days as follows:
Number of excess days Fine per day ()
1 to 5 2.00
6 to 10 3.00
above 10 days 5.00
Design a class Library with the following details:
Data Members:
Name : name of the book
Author : author of the book
P : price of the hook in decimals
D : number of days taken in returning the book
F : stores the fine.
Member functions:
Library(...) : parameterized constructor
void fine() : calculates the fine for excess days, prints
the book details along with the number of
days, fine and total amount to be paid.
Total amount =
2% of the price of the book * (total number of days)+fine
You need not write the main() method.

import java.util.*;
class Library

16
{
String Name , Author;
double P,F;
int D;
public Library(String N,String A,double P,int D)
{
Name=N;
Author=A;
this.P=P;
this.D=D;
} // parameterized constructor
public void fine()
{
int ed=D-7; // calculating the number of excess
days
if (ed==0)
F=0.0;
else if(ed>=1 && ed<=5)
F=2.0*ed; // calculating the fine
else if(ed>=6 && ed<=10)
F=(ed-5)*3.0+10.0; // calculating the fine
else if(ed>10)
F=(ed-10)*5.0+25.0; // calculating the fine
double amt = ((0.02*P)*D)+F; // calculating the total
amount to be paid as per the given formula
// printing the details of the book along with the total
amount to be paid
System.out.println("Name of the book is "+Name);
System.out.println("Author of the book is "+Author);

17
System.out.println("Number of days="+D);
System.out.println("Fine to be paid="+F);
System.out.println("Total Amount="+amt);
}
}

OUTPUT
Name of the book is Harry Potter The Deathly Hallows
Author of the book is JK Rowling
Number of days=7
Fine to be paid=0.0
Total Amount=28.0

18
Program 6
A class Check has been defined to print all palindrome
numbers from 1 upto the given limit. The details of the
class are given below:
Data Members:
limit : integer to store limit
Member Functions:
Check(int l) : to initialise limit by l
boolean ispalindrome(int x) : checks and returns true if x
is palindrome else returns
false
void dsiplayall() : prints all palindrome
numbers from 1 upto the limit
Specify the class Check giving details of the constructor
and other methods of the class. Also define the main
function to create an object of the class and the
functions accordingly to enable the task.

import java.util.*;
class Check
{
int limit;
public Check(int l)
{
limit=l;
} // parameterized constructor
boolean ispalindrome(int x)
{

19
int t=x;
int rev=0;
while(t!=0)
{
int d=t%10;
rev=(rev*10)+d; // calculating the reverse of the
number x
t=t/10;
}
if (rev==x) // checking if the reverse is same as the
original number
return true;
else
return false;
}
void displayall()
{
for(int i=1;i<=limit;i++)
{
boolean r=ispalindrome(i); // checking if i is a
palindrome number or not
if(r)
System.out.println(i);
}
}
public static void main ()
{
Scanner sc =new Scanner(System.in);
System.out.println("Enter the limit");

20
int lim=sc.nextInt(); // taking input from the user
Check obj = new Check(lim);
obj.displayall();
}
}

OUTPUT
Enter the limit
50
1
2
3
4
5
6
7
8
9
11
22
33
44

21
Program 7
Q). A happy number is a number in which the eventual
sum of the square of the digits of the number is equal to
1.
Design a class Happy to check if a given number is a
happy number or not.
Members of the class are given below:
Class name : Happy
Data member:
n : stores the number.
Member functions:
Happy() : constructor to assign to a

void getnum(int nn) : assigns the parameter value


to the member i.e. n=nn
int sum_sq_digits(int x) : returns the sum of the square
of the digits of the numbers
void isHappy() : checks and prints whether the
given number is a happy
number or not by calling the
appropriate functions and
displays an appropriate message
Specify the class Happy giving details of the
constructor(), void getnum(int), int sum_sq_digits(int)
and void ishappy(). Also define the main function to
create an object of the class and call the methods to
check for a happy number.

22
import java.util.*;
class Happy
{
int n ;
public Happy()
{
n=0;
} // default constructor
public void getnum(int nn)
{
n=nn;
}
int sum_sq_digits(int x)
{
int s=0;
int t=x;
while (t!=0)
{
int d=t%10;
int sq=d*d;
s=s+sq; // calculating the sum of square of digits
t=t/10;
}
return s;
}
void ishappy()
{
int t=n;
int s=0;

23
while(t>9)
{
s=sum_sq_digits(t); // calculating the eventual
sum of the square of the digits
t=s;
}
if (t==1) // checking if the eventual sum of the
square of the digits is equal to 1
System.out.println(n+" is a Happy number");
else
System.out.println(n+" is not a Happy number");
}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int number = sc.nextInt(); // taking the input from
the user
Happy obj = new Happy();
obj.getnum(number);
obj.ishappy();
}
}

OUTPUT
Enter a number
28
28 is a Happy number

24
Program 8
Q). 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:
n : stores the number
rev : stores the reverse of the number.
Member functions/methods:

Emirp(int nn) : to assign n=nn and rev=0


int isprime(int x) : checks if the number is prime
and returns 1 if prime otherwise
returns 0
void isEmirp() : reverses the given number and
checks if both the original number and
the reverse of number are prime, by
invoking the function isprime(int) and
displays whether the number is an
emirp number or not.
Specify the class Emirp giving details of the functions int
isprime(int) and void isEmirp().

import java.util.*;
class Emirp

25
{
int n;
int rev;
public Emirp(int nn)
{
n=nn;
rev=0;
} // parameterized constructor
public int isprime(int x)
{
int t=x;
int c=0;
for (int i=1;i<=t;i++)
{
if(t%i==0) // checking if i is a factor of t
c++; // counting the number of factors
}
if (c==2)
return 1;
else
return 0;
}
public void isEmirp()
{
int a=isprime(n);
int t=n;
while(t!=0)
{
int d=t%10;

26
rev=(rev*10)+d; // calculating the reverse of the
number x
t=t/10;
}
int b=isprime(rev); // checking if the reverse of the
number is prime or not
if (a==b && a==1)
System.out.println(n+" is an Emirp number");
else
System.out.println(n+" is not an Emirp number");
}
public static void main ()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int num=sc.nextInt(); // taking the input from the
user
Emirp obj=new Emirp(num);
obj.isEmirp();
}
}

OUTPUT
Enter a number
31
31 is an Emirp number

27
Program 9
Q). Define a class Find with the following detail:
Class name : Find
Data members :
lim : stores limit
Member functions:
Find(int m) : constructor to initialise lim
by m
boolean iscomposite(int x) : returns true if x is a
composite number else
return false
void sumcomposite() : prints sum of all
composite numbers from
1 to lim
Specify the class Find giving details of the constructor
Find(int), boolean iscomposite(int) and void
sumcomposite(). You need not write the main() function.

import java.util.*;
class Find
{
int lim;
public Find(int m)
{
lim=m;
} // parameterized constructor
boolean iscomposite(int x)
{

28
int t=x;
int c=0;
for (int i=1;i<=t;i++)
{
if(t%i==0) // checking if i is a factor of t
c++; // checking if i is a factor of t
}
if (c>2)
return true;
else
return false;
}
public void sumcomposite()
{
int s=0;
for (int i=1;i<=lim;i++)
{
boolean r=iscomposite(i); // checking if i is a
composite number or not
if(r)
s=s+i; // calculating the sum of the composite
numbers in the given range
}
System.out.println("Sum is "+s);
}
}

OUTPUT
Sum is 37

29
Program 10
Q). A Magic number is a number in which the eventual
sum of digits of the number is equal to 1.
Design a class Magic to check if a given number is a
magic number or not. Some of the members of the
class are given below:
Class name : Magic
Data Members:
n : stores the number
Member Functions:
Magic() : constructor to assign o to n
void getnum() : to assign the parameter value
to the member n=nn
int sumofdigits(int nn) : returns the sum of the digits
of the number received
void ismagic() : checks if the number is a
magic number by calling the
function sumofdigits(int) and
displays appropriate message
Specify the class Magic giving details of the constructor,
void getnum(int) , int sumofdigits(int)and void ismagic()
Write the main function to create the object of the class
and call the functions accordingly.

import java.util.*;
class Magic
{
int n ;

30
public Magic()
{
n=0;
} // default constructor
public void getnum(int nn)
{
n=nn;
}
int sumOfDigits(int nn)
{
int s=0;
int t=nn;
while (t!=0)
{
int d=t%10;
s=s+d; // calculating the sum of the digits
t=t/10;
}
return s;
}
void isMagic(int x)
{
int t=x;
int s=0;
while(t>9)
{
s=sumOfDigits(t); // calculating the eventual sum
of the digits
t=s;

31
}
if (t==1) // checking if the eventual sum of the digits
is equal to 1 or not
System.out.println(n+" is a Magic number");
else
System.out.println(n+" is not a Magic number");
}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int number = sc.nextInt(); // taking the input from
the user
Magic obj = new Magic();
obj.getnum(number);
obj.isMagic(obj.n);
}
}

OUTPUT
Enter a number
172
172 is a Magic number

32
Program 11
Q). Define a class Taxi having the following description:
Class Name : Taxi
Data Members:
int taxino : to store taxi number
long code : to store passenger’s code
int km : to store number of kilometres
Member Functions:
Taxi() : constructor to initialise taxino ,
code ,and km to 0
void input() : to accept taxi number , code of
passenger and distance
double calculate() : to calculate and return the bill
amount for a customer
according to the following
prescribed rates:
Kilometres travelled (km) Rate/km
Up to 1 Rs 5
Above 1 up to 6 Rs 10
Above 6 up to 12 Rs 15
Above 12 Rs 20
Specify the class Taxi giving the details of void input() ,
double calculate() and void display() only. You need not
write the main() function.

import java.util.*;
class Taxi
{

33
int taxino;
long code;
int km;
public Taxi()
{
taxino=0;
code=0;
km=0;
} // default constructor
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the taxi number");
taxino=sc.nextInt(); // inputting the taxi number
System.out.println("Enter the passenger code");
code=sc.nextLong(); // inputting the passenger
code
System.out.println("Enter the distance travelled (in
km)");
km=sc.nextInt(); // inputting the distance travelled
}
public double calculate()
{
int bill=0;
if(km<=1)
bill=5*km;
else if(km>1 && km<=6)
bill=(km-1)*10+5;

34
else if(km>6 && km<=12) // calculating the bill
according to the given slabs
bill=(km-6)*15+55;
else if(km>12)
bill=(km-12)*20+145;
return bill;
}
public void display()
{
input();
System.out.println("Taxi no \t Kilometres travelled \t
Bill amount");
double b=calculate(); // calculating the bill by
invoking function calculate
System.out.println(taxino+"\t"+km+"\t"+b);
}
}

OUTPUT
Enter the taxi number
1529
Enter the passenger code
58694200123
Enter the distance travelled (in km)
100
Taxi no Kilometres travelled Bill amount
1529 100 1905.0

35
Program 12
Q). Design a class Midsum with the following details
Class name : Midsum
Dats members:
min : integer to store lower limit
max : integer to store upper limit
Member functions:
Midsum(int m,int n) : initialise min by m and max by n
int sumfirstlast(int x) : returns the sum of first and last
digit’s of x
void printall() : prints all the numbers from min
to max whose sum of first and last digit is equal to sum
of all the digits except first and last.
Specify the class Midsum giving details of the
constructor and other methods, Write the main function
to create an object of the class and call the methods
accordingly.

import java.util.*;
class Midsum
{
int min , max;
public Midsum(int m,int n)
{
min=m;
max=n;
} // parameterized constructor
int sumfirstlast(int x)

36
{
int t=x;
int f=t%10; // chopping the last digit
int l=0;
while(t!=0)
{
l=t%10; // calculating the first digit
t=t/10; }
int sum=f+l; // calculating the sum of first and the
last digits
return sum; }
public void printall()
{
for (int i=min;i<=max;i++)
{
int sfl=sumfirstlast(i); // calculating the sum of
first and the last digits by invoking function
int ts=0;
int t=i;
while(t!=0)
{
int d=t%10;
ts=ts+d; // calculating the sum of all the digits
t=t/10; }
int srem=ts-sfl; // calculating the sum of the
middle digits
if (srem==sfl)
System.out.println(i); } }
public static void main ()

37
{
Scanner sc =new Scanner(System.in);
System.out.println("Enter the maximum and
minimum limits");
int minimum = sc.nextInt();
int maximum = sc.nextInt(); // taking the input from
the user
Midsum obj = new Midsum(minimum,maximum);
obj.printall(); }}

OUTPUT
Enter the maximum and minimum limits
1000
1100
1010
1021
1032
1043
1054
1065
1076
1087
1098
1100

38
Program 13
Q). Define a class Abc with the following details:
Class name : Abc
Data member:
lim : stores limit.
Member functions:
Abc(int m) : constructor to initialise lim by m
int reverse(int x) : returns reverse of the integer in x
void printpalindrome(): prints all palindrome numbers
from 100 to limit (assume that the value of limit is more
than 100).
Specify the class Abc giving details of methods int
reverse() and void printpalindrome().
You need not write main() function.

import java.util.*;
class Abc
{
int lim;
public Abc(int m)
{
lim=m;
} // parameterized constructor
int reverse(int x)
{
int t=x;
int rev=0;
while(t!=0)

39
{
int d=t%10;
rev=(rev*10)+d; // calculating the reverse of the
number x
t=t/10; }
return rev; }
public void printpalindrome()
{
for (int i=100;i<=lim;i++)
{ int rev=reverse(i); // calculating the reverse of
the number i by invoking function
if (rev==i) // checking if i is palindrome or not
System.out.println(i); } }
public static void main ()
{ Scanner sc = new Scanner(System.in);
System.out.println("Enter the limit(>100)");
int l=sc.nextInt(); // taking input from the user
Abc obj = new Abc(l);
obj.printpalindrome(); } }

OUTPUT
Enter the limit(>100)
150
101
111
121
131
141

40
Program 14
Q). Design a class DDR to create double dimension
array of size m*n and print the sum of boundary
elements and the largest number present in the array.
Class name : DDR
Data members:
arr[][] : stores the matrix elements
m : integer to store number of rows
n : integer to store number of columns
Member functions:
DDR() : default constructor
DDR (int mm, int n) : to initialise the size of the matrix,
m=mm and n=nn and create the
Array
void fillarray() : accepts elements of the matrix
void sumboundary() : prints the sum of boundary
elements of array prints
void maxarray() : prints largest number present in
the array.
Specify the class DDR giving details of the constructor
void fillarray() , void sumboundary and void maxarray().
Also define the main function to create the object of the
class and call the methods accordingly.

import java.util.*;
class DDR
{
int arr[][];

41
int m,n;
static Scanner sc = new Scanner(System.in);
public DDR()
{
m=0;n=0;
} // default constructor
public DDR(int mm,int nn)
{
m=mm;
n=nn;
arr = new int[m][n];
} // parameterized constructor
public void fillarray()
{
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
System.out.println("Enter the element for
row="+i+" and column="+j);
arr[i][j]=sc.nextInt();
}
}
} // taking input from the user
public void sumboundary()
{
int s=0;
for (int i=0;i<m;i++)
{

42
for ( int j=0;j<n;j++)
{
if ( i==0 || i==(m-1) || j==0 || j==(n-1))
s=s+arr[i][j]; // calculating sum of the boundary
elements of the array }}
System.out.println("Sum of boundary elements is
"+s); }
public void maxarray()
{
int max=arr[0][0];
int r=0;
int c=0;
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
if (arr[i][j]>max)
{
max=arr[i][j]; // finding the largest number of
the array
r=i;
c=j; }}}
System.out.println("Largest element "+max+" found
at row="+r+" and column="+c); }
public static void main ()
{ System.out.println("Enter the number of rows and
columns for the array");
int row = sc.nextInt();
int col = sc.nextInt(); // taking input from the user

43
DDR obj = new DDR(row,col);
obj.fillarray();
obj.sumboundary();
obj.maxarray(); } }

OUTPUT
Enter the number of rows and columns for the array
3
3
Enter the element for row=0 and column=0
52
Enter the element for row=0 and column=1
36
Enter the element for row=0 and column=2
45
Enter the element for row=1 and column=0
25
Enter the element for row=1 and column=1
89
Enter the element for row=1 and column=2
63
Enter the element for row=2 and column=0
52
Enter the element for row=2 and column=1
41
Enter the element for row=2 and column=2
54
Sum of boundary elements is 368
Largest element 89 found at row=1 and column=1

44
Program 15
A Friendly number is a number the eventual sum of the
digits of the number is equal to 9.
Design a class Friendly to check if a given number is a
Friendly number or not. Some of the member functions
of the class are given below.
Class name : Friendly
Data member:
Num : to store the number
Member Functions:
Friendly(int n) : to assign value of n toNum
int sumOfDigits(int nn) : Returns the sum of the digits
boolean isFriendly(int x) : returns true if the number is
Friendly by calling the function sumOfDigits(int)
otherwise returns false.
void display() : display appropriate message
“Number is a Friendly number” OR “Number is not a
Friendly number” by calling the function isFriendly(int)
Write the main( ) function to create object for the class
and call functions.

import java.util.*;
class Friendly
{

45
int num ;
public Friendly(int n)
{
num = n;
} // parameterized constructor
int sumOfDigits(int nn)
{
int s=0;
int t=nn;
while (t!=0)
{
int d=t%10;
s=s+d; // calculating the sum of the digits
t=t/10; }
return s; }
boolean isFriendly(int x)
{
int t=x;
int s=0;
while(t>9)
{

46
s=sumOfDigits(t); // calculating the eventual sum
of the digits
t=s; }
if (t==9)
return true;
else
return false; }
public void display()
{
boolean r= isFriendly(num); // checking if num is a
friendly number or not
if (r)
System.out.println(num+" is a friendly number");
else
System.out.println(num+" is not a Friendly number");
}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int number = sc.nextInt(); // taking input from the
user
Friendly obj = new Friendly(number);

47
obj.display(); }}

OUTPUT
Enter a number
675
675 is a friendly number

48

You might also like