Computer Project-2
Computer Project-2
PROJECT
2024-2025
NAME- SAKSHAM
RAJ CLASS- 12-A
ROLL NUMBER- 16
SCHOOL- ST. MARY’S CONVENT INTER
COLLEGE SUBJECT- COMPUTER
PROJECT
1
ACKNOWLEDGEMENT
2
PREFACE
The project file is being presented to provide the
viewers with a clear and logical aspects of
programming in JAVA.
The treatment of the project is simple, lucid,
absorbing to make the file readable. Algorithms, have
also been placed in the program so as to help to
understand how the program proceeds. Each program
is completely executed and its output is also provided
after the program.
The project is being presented to you with a
hope that it will surely be linked.
-SAKSHAM RAJ
3
INTRODUCTION -
ABOUT JAVA
Java is a high-level, class-based, object-oriented
programming language that is designed to have as few
implementation dependencies as possible. It is a general-
purpose programming language intended to let programmers
write once, run anywhere (WORA) meaning that compiled
Java code can run on all platforms that support Java without
the need to recompile. Java applications are typically compiled
to bytecode that can run on any Java virtual machine (JVM)
regardless of the underlying computer architecture. The
syntax of Java is similar to C and C++, but has
fewer low-level facilities than either of them. The Java runtime
provides dynamic capabilities that are typically not available
in traditional compiled languages.. Java was originally
developed by James
Gosling at Sun Microsystems.
4
INDEX
S.N PROGRAM PAGE DATE
O
1 Program to arrange words in order of potential. 7
2 Program to print smallest required number. 11
3 Program to print composite magic numbers. 15
4 Program to check if date is valid . 19
5 Program to check if a number is circular prime 23
or not.
6 Program to print a natural number in words. 27
7 Program to print Kaprekar numbers. 31
8 Program to fill a matrix with characters. 34
9 Program to check if a number is evil number or 38
not.
10 Program to check if a number is a valid ISBN 41
number.
11 Program to generate and print lucky numbers. 44
12 Program to print Goldbach numbers. 48
13 Program to delete a word from a sentence. 52
14 Program to print pell and Tribonacci series 56
15 Program to print Pyramidal pattern. 60
16 Program to print Pascal triangle. 63
17 Program to input a mathematical 67
expression and perform operation.
18 Program to print circular matrix. 71
19 Program print Hamming distance. 75
5
20 Program to print Magic square matrix. 80
21 Program to check if a string is palindrome or 84
not.
22 Program to check if a number is Armstrong or 89
not.
23 Program to reverse each elements of a given 94
matrix.
24 Program to add and remove integers using 100
queue.
25 Program to print all 2 digit prime number 105
using recursion.
26 Program to calculate monthly bill. 108
27 Program to print date in written form. 112
28 Program sort an array using Insertion sort. 116
29 Program to remove given element from an 120
array.
30 Program to print word with all missing 123
alphabets.
31 Bibliography. 126
6
PROGRAM -1
Potential of a word is found by adding the ASCII values of the alphabets. Write
a program to accept a string which may be terminated by either “.” , “?” , “,” ,
“!” only. The words of a sentence are separated by a single blank space and are
in UPPER CASE. Decode the words according to their potential and arrange
them in ascending order of their potential.
ALGORITHM:
7
CODING:
8
p[i]=K;
}
int y,z,temp;
for(y=0;y<x-1;y++) //using bubble sort technique to arrange the
words
{
for(z=0;z<x-1-y;z++)
{
if(p[z]>p[z+1])
{
temp=p[z];
st=wd[z];
p[z]=p[z+1];
wd[z]=wd[z+1]; //bubble sort loop
p[z+1]=temp;
wd[z+1]=st;
}
}
}
for(i=0;i<x;i++)
{
System.out.print(wd[i]+" ");
}
System.out.println(ch);
} //closing of main()
} //closingof class
9
VARIABLE DESCRIPTION TABLE-
10
PROGRAM -2
Given two positive numbers M and N is between 100 and 10000 and N is less
than 100. Find the smallest integer that is greater than M and whose digits
add up to N. For example, if M=100 and N=11, then smallest integer greater
than 100 whose digits add up to 11 is 119. Write a program to accept the
numbers M and N from the use and print the smallest required number
whose sum of all its digits is equal to N. Also, print the total number of digits
present in the required number. The program should check for the validity of
the inputs and display an appropriate message for an invalid input.
ALGORITHM:
11
CODING:
import java.util.*; //importing util package
class ReqSum
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter limits M and N where
M<N");
int M=sc.nextInt(); //taking inputs
int N=sc.nextInt();
if(M<100||M>10000||N>100) //checking for invalid
input
{
System.out.println("INVALID INPIUT");
System.exit(0);
}
int i;
for(i=M+1;i<10000;i++) //loop for finding sum
{
int n=i;
int sum=0;
while (n>0)
{
sum+=n%10; //finding sum
n/=10;
12
}
if(sum==N)
{
System.out.println("The required number="+i);
System.out.println("Total number of
digits="+String.valueOf(i).length());
break; //using jump statement
}
}
}
}
13
VARIABLE DESCRIPTION TABLE-
14
PROGRAM -3
A composite Magic number is a positive integer which is composite
as well as a magic number. Composite number is a number that has
more than two factors. For example: 10 Factors are: 1,2,5,10 A Magic
number is a number in which the eventual sum of the digits is equal
to 1. For example: 28=2+8=10=1+0=1 Accept two positive integers m
and n, where m is less than n as user input. Display the numbers of
Composite magic integers that are in the range between m and n (
both inclusive ) and output them along with the frequency.
ALGORITHM:
15
CODING:
import java.util.*; //importing util package
class CompMagic
{
static int isPrime(int n,int D) //method to check whether a
number is prime or not
{
if(n==D)
return 1;
else if(n%D==0)
return 0;
else
return isPrime(n,D+1);
}
static int isMagic(int n) //to check whether a number is
magic number or not
{
while (n>=10)
{
int sum=0;
while (n>0)
{
sum+=n%10;
n/=10;
}
n=sum;
}
if(n==1)
return 1;
16
else
return 0;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter limits m and n where m<n");
int m=sc.nextInt(); //taking input
int n=sc.nextInt();
if(m>n) //checking for invalid input
System.out.println("INVALID INPIUT");
int i;int c=0;
for(i=m;i<=n;i++)
{
if(isPrime(i,2)==0&&isMagic(i)==1)
{ c+
+;
System.out.print(i+","); //printing result
}
}
System.out.println();
System.out.println("FREQUENCY OF COMPOSITE MAGIC
INTEGERS IS:"+c);
}
}
17
VARIABLE DESCRIPTION TABLE-
18
PROGRAM -4
Design a program which accepts your date of birth in dd mm
yyyy format. Check whether the date entered is valid or not.
If it is valid, display “VALID DATE”, also compute and display
the day number of the year for the date of birth. If it is
invalid, display “INVALID DATE” and then terminate the
program.
ALGORITHM:
19
CODING:
20
s=d;
for(i=1;i<m;i++)
{
s+=D[m]; //finding say number
}
System.out.println(s); //printing result
}
}
21
VARIABLE DESCRIPTION TABLE-
22
PROGRAM -5
A Circular Prime is a prime number that remains prime
under the cyclic shift of
its digits. When the leftmost digit is removed and replaced
at the end of the
remaining string of digits, the generated number is still
prime. The process is
repeated until the original number is reached again.
A number is said to be prime if it has only two factors, 1
and itself.
ALGORITHM:
23
CODING:
24
c=0;
}
System.out.println(num);
int d=num/(int)Math.pow(10,a-1);
num=num%(int)Math.pow(10,a-1);
num=num*10+d;
}
if(c==1)
System.out.println(n+" IS A CIRCULAR PRIME");
//printing result
else
System.out.println(n+" IS NOT A CIRCULAR PRIME");
}
}
25
VARIABLE DESCRIPTION TABLE-
26
PROGRAM -6
Write a program to input a natural number less
than 10000 and then output it in words.
ALGORITHM:
27
CODING:
import java.util.*; //importing util package
class NumToWord
{
static String
U[]={"","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIG
HT","NINE","TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEE
N",
"FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"};
static String T[]={"","","TWENTY","THIRTY","FORTY",
"FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};
28
System.out.print(U[n/100]+" HUNDRED "); //printing hundred's
place
}n
%=100;
if(num>100)
{
System.out.print(" AND");
}
System.out.print("");
if(n<20)
{
System.out.print(" "+U[n]);
}
else
{
System.out.print(" "+T[n/10]); //printing ten's place
System.out.print(" "+U[n%10]); //printing unit's place
}
System.out.println();
}
}
29
VARIABLE DESCRIPTION TABLE-
30
PROGRAM -7
A positive whole number ‘n’ that has ‘d’ number of
digits is squared and split into pieces, a right-hand
piece that has ‘d’ digits and a left- hand piece that has
remaining ‘d’ or ‘d-1’ digits. If the sum of the two pieces
is equal to the number, then ‘n’ is a Kaprekar number.
The few Kaprekar numbers are: 9,45,297…..
ALGORITHM:
31
CODING:
32
VARIABLE DESCRIPTION TABLE-
33
PROGRAM -8
Write a program to declare a square matrix M[][] of
order ‘N’ where ‘N’ must be greater than 3 and less than
10. Allow the user to accept three different characters
from the keyboard and fill the array according to the
instruction given below:
(i) Fill the four corners of the square matrix by character 1
(ii) Fill the boundary elements of the matrix (except
four corners) by character 2
(iii) Fill the non-boundary elements of the matrix by character 3
ALGORITHM:
34
CODING:
import java.util.*; //importing util package
class Pattern
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of matrix");
int N=sc.nextInt(); //taking input of size
if(N<=3||N>=10) //checking for invalid input
{
System.out.println("SIZE OUT OF RANGE");
System.exit(0);
}
int r,c;
System.out.println("ENTER THREE CHARACTERS");
char c1=sc.next().charAt(0); //entering the different
characters
char c2=sc.next().charAt(0);
char c3=sc.next().charAt(0);
char ch[][]=new char[N][N];
for(r=0;r<N;r++)
{
for(c=0;c<N;c++)
{
if((r==0&&c==0)||(r==0&&c==N-1)||(r==N-
1&&c==0)||(r==N-1&&c==N-1))//checking for
corners
ch[r][c]=c1;
35
else if(r==0||c==0||r==N-1||c==N-1)
ch[r][c]=c2;
else ch[r]
[c]=c3;
}
}
for(r=0;r<N;r++)
{
for(c=0;c<N;c++)
{
System.out.print(ch[r][c]+"\t"); //printing the pattern
}
System.out.println();
}
}
}
36
VARIABLE DESCRIPTION TABLE-
37
PROGRAM -9
An Evil Number is a positive whole number which has
even number of 1’s in its binary equivalent. For
example: binary equivalent of 9 is 1001 , which
contains even number of 1’s. Few other Evil numbers
are: 3,5,6,9,10,12,15,.......Write a program to accept a
positive
whole number. Find its Binary equivalent and count
the number of 1’s in the Binary. Display with
appropriate message that the input number is an Evil
number or not.
ALGORITHM:
38
39
CODING:
import java.util.*; //importing util package
class Evil
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Positive Whole Number");
int n=sc.nextInt(); //taking input
String s="";
int x=n;
while(x>0)
{
s=x%2+s; //converting decimal number into binary form
x/=2;
}
int i;
int
f=0;
for(i=0;i<s.length();i++) //loop to find the number of 1's
{
if(s.charAt(i)=='1')
f++;
}
System.out.println("Binary Equivalent:"+s);
System.out.println("No of 1's="+f); if(f
%2==0)
System.out.println("EVIL NUMBER"); //printing result
else
System.out.println("NOT AN EVIL NUMBER");
}
}
40
VARIABLE DESCRIPTION TABLE-
41
PROGRAM -10
An ISBN ( International Standard Book Number) is a ten
digit code that uniquely identifies a book. The first nine
digits represent the group, publisher and title of the
book and the last digit is used to check whether ISBN is
correct or not. Each of the first nine digits of the code
can take a value between 0 to 9. Sometimes it is
necessary to make the last digit to ten. It is done by
writing the last digit, plus 9 times the second digit, plus
8 times the third digit and so on until we add 1 time the
last digit. If the final number leaves no remainder while
divided by 11, the code is a valid ISBN. For example:
0201103311 = 10*0 + 9*2 + 7*1 +6*1 + 5*0 + 4*3 + 3*3
+ 2*1 + 1*1
= 55. Since 55 leaves no remainder when divisible by
11, hence it is a valid ISBN. Design a program to
accept a ten digit code from the user. For an invalid
input, display an appropriate message.
ALGORITHM:
42
CODING:
import java.util.*; //importing util package
class ISBN
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a ISBN"); //taking input of ISBN
String n=sc.nextLine();
String n1=n.substring(0,9);
String n2=n.substring(9);
int a,b;
if(Character.toUpperCase(n2.charAt(0))=='X')
b=10;
else
b=Integer.valueOf(n2); //converting string into integer form
a=Integer.valueOf(n1);
int s=b;
int i;
for(i=0;i<n1.length();i++)
{
s+=(a%10)*(i+2); //finding the total sum,ie,ISBN
a/=10;
}
System.out.println("SUM="+s);
if(s%11==0)
System.out.println("LEAVES NO REMAINDER-VALID ISBN
CODE"); //prints result
else
System.out.println("LEAVES REMAINDER-INVALID ISBN CODE");
}}
43
VARIABLE DESCRIPTION TABLE-
VARIABLE DATA TYPE PURPOSE
44
PROGRAM -11
Consider the sequence of natural numbers
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,
27,………….. Removing every second number
produce the
sequence1,3,5,7,9,11,13,15,17,19,21,23,25,27,
………………..
Removing every third number from the above sequence
produces
1,3,7,9,13,15,19,21,25,27,....This process continues
indefinitely by removing the fourth , fifth, and so
on, till after a
fixed number of steps, certain numbers remain
indefinitely. These are known as Lucky Numbers. Write
a program to generate and print Lucky Numbers less
than a given natural number n where n<=50.
ALGORITHM:
45
46
CODING:
47
System.out.println("Enter elements");
for(i=0;i<n;i++)
{
A[i]=sc.nextInt();
}
int a=2;
while(a<=n)
{
for(i=a-1;i<A.length;i+=a) //loop to remove elements at specific
intervals
{ A[i]=
0;
}
A=ZeroRemover(A);
n=A.length;
a+=1;
}
System.out.println("Lucky elements are:");
for(i=0;i<n;i++) //loop to print lucky elements
{
System.out.print(A[i]+"\t");
}
System.out.println();
}
}
48
VARIABLE DESCRIPTION TABLE
49
PROGRAM -12
A Goldbach number is a positive even integer that can
be expressed as the sum of two odd primes.
Write a program to accept an even integer ‘N’
where N>9 and N<50. Find all the odd prime pairs
whose sum is equal to the number ‘N’.
ALGORITHM:
50
CODING:
51
{
if(isPrime(i)==true&&isPrime(n-i)==true)
System.out.println(i+","+(n-i));
}
}
}
52
VARIABLE DESCRIPTION TABLE
53
PROGRAM -13
Write a program to accept a sentence which may be
terminated by either ‘.’, ‘?’ or ‘!’ only. Any other
character may be ignored. The words may be separated
by one blank space and are in UPPER CASE. Accept a
word from the user which is a part of the sentence along
with its position number and delete the word and
display
the sentence.
ALGORITHM:
54
CODING:
55
}
R=R.trim();
System.out.println("OUTPUT: "+ R+M); //printing final result
}
}
56
VARIABLE DESCRIPTION TABLE
57
PROGRAM -14
Write a program to perform the following tasks:
(i)Pell Series : Print the first 20 terms of Pell series.
This series begins with 1 and 2 where every successive
term is the sum of twice of previous number and the
number previous to the previous number.
1,2,5,12,29……
(ii) Tribonacci Series: Enter a limit and print that many
terms of the Tribonacci series. This series begins with
0, 1,1and every successive term is term is the sum of
previous three terms. 0,1,1,2,4,7,13…..
ALGORITHM:
58
CODING:
import java.util.*; //importing util package
class DoubleSeries
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a=0,b=1,c=1;
int s=0;
System.out.println("Tribonacci Series is:");
System.out.print(a+"\t"+b+"\t"+c+"\t"); for(i=4;i<=n;i+
+)
{
s=a+b+c; //finding successive tribonacci terms
59
System.out.print(s+"\t");
a=b; //swapping
b=c;
c=s;
}
}
}
60
VARIABLE DESCRIPTION TABLE
VARIABLE DATA TYPE PURPOSE
61
PROGRAM -15
Write a program to accept the number of rows desired
and print the following pyramidal pattern.
1
101
10101
1010101
101010101
10101010101
ALGORITHM:
62
CODING:
import java.util.*; //importing util package
class Pyramid
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Series Limit in Number of Rows");
long n=sc.nextLong();
long s=n;
long p=1,i,j,k;
for(i=1;i<=n;i++) //loop for rows
{
for(j=1;j<=s;j++) //loop for spaces
{
System.out.print(" ");
}
for(k=1;k<=p;k++) //loop for printing elements
{
if(k%2==0) //checking whether the position is even or odd
System.out.print(0);
else
System.out.print(1);
}
System.out.println();//changing line
s--;
p+=2;
}
}
}
63
VARIABLE DESCRIPTION TABLE
VARIABLE DATA TYPE PURPOSE
64
PROGRAM -16
Write a program to print a Pascal’s triangle with the
number of rows entered by the user. Each row has same
number of rows as the row number. The formula of each
element of a specific row is given by nCr=𝐧!𝐫!
(𝐧−𝐫)! .From top, n starts from ‘0’ and from right to left
in any row, ‘r’ starts with 0 and continues till ‘n-1’.
1
11
121
1331
14641
1 5 10 10 5
ALGORITHM:
65
CODING:
import java.util.*; //importing util package
class Pascal
{
static int fact(int n) //function for factorial
{
int f=1,i;
for(i=2;i<=n;i++)
{
f*=i;
}
return f;
}
static int nCr(int n,int r) //function to find nCr
{
return fact(n)/(fact(r)*fact(Math.abs(n-r)));
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows of Pascal's Triangle");
int n=sc.nextInt();
int i,j;
for(i=0;i<=n-1;i++) //loop for rows
{
for(j=0;j<n-i;j++) //loop for spaces
{
System.out.print(" ");
}
for(int k=0;k<=i;k++) //loop for printing elements
{
System.out.print(nCr(i,k)+" ");
66
}
System.out.println();
}
}
}
67
VARIABLE DESCRIPTION TABLE
VARIABLE DATA TYPE PURPOSE
68
PROGRAM -17
Simple valid arithmetic expression can be written as
first operand followed by an arithmetic operator and
followed by a second operand. Given that the operator
can be +, -, *, or / for addition, subtraction,
multiplication and division respectively.
Write a program to input an arithmetic expression in
strings form which contains only one operator between
two numeric operands. Print the output in the form of
number. If more than one operator is present, an error
output message “INVALID
INPUT” should appear.
ALGORITHM:
69
CODING:
import java.util.*; //importing util package
class Operations
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter an arithmetic expression");
String n=sc.nextLine(); //taking input
char
ch[]=n.toCharArray(); int
i;
int n1=0;
int n2=0;
char o='\u0000';
int f=0;
for(i=0;i<ch.length;i++)
{
char a=ch[i];
String z=""+a;
if(Character.isDigit(a)==false)
{ f+
+;
o=a;
continue; //using a jump statement
}
int
c=Integer.valueOf(z);
if(f==0)
n1=n1*10+c; //extracting first number
if(f==1)
n2=n2*10+c; //extracting second number
}
70
if(f!=1) //checking for validity of expression
71
{
System.out.println("INVALID EXPRESSION");
System.exit(0);
}
int b=0;
switch(o) //to check for a operator
{
case '+':
b=n1+n2;
break;
case '-':
b=n1-n2;
break;
case '*':
b=n1*n2;
break;
case '/':
b=n1/n2;
break;
default:
System.out.println("INVALID OPERATOR");
System.exit(0);
}
System.out.println("Answer="+b); //printing final result
}
72
VARIABLE DESCRIPTION TABLE
73
PROGRAM -18
Write a program to print the numbers in spiral or
circular form taking all natural numbers from n2 to 1
(n>=1). Let input n=4, then n2 = 16, the output is:
16 15 14 13
5 43 1
2
6 12 1
1
7 89 1
0
ALGORITHM:
74
CODING:
import java.util.*; //importing util package
class Spiral
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Number as a Size");
int n=sc.nextInt(); //taking input
int A[][]=new int[n][n]; //initializing matrix
int r1=0,c1=0;
int r2=n-1,c2=n-1;
int k=n*n;
int i=0,j=0;
while(k>0) //loop for filling matrix in a spiral way
{
for(i=c1;i<=c2;i++)
{
A[r1][i]=k--;
}
for(j=r1+1;j<=r2;j++)
{
A[j][c2]=k--;
}
for(i=c2-1;i>=c1;i--)
{
A[r2][i]=k--;
}
for(j=r2-1;j>=r1+1;j--)
{
A[j][c1]=k--;
}
75
r1++; //changing the counter parameters
c1++;
r2--;
c2--;
}
System.out.println("SPIRAL Matrix");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t"); //printing the final matrix
}
System.out.println();
}
}
}
76
VARIABLE DESCRIPTION TABLE
77
PROGRAM -19
78
ALGORITHM:
79
CODING:
80
String m=ob.bin(M);
String n=ob.bin(N);
int D=m.length()-n.length();
if(D>0)
n=ob.balancer(n,D);
if(D<0)
m=ob.balancer(m,Math.abs(D));
int i;
int f=0;
for(i=0;i<m.length();i++) //loop to find hamming distance
{
if(m.charAt(i)!=n.charAt(i))
f++;
}
System.out.println("Bit Pattern of M:"+m);
System.out.println("Bit Pattern of N:"+n);
System.out.println("Hamming Distance="+f); //printing final
result
}
}
81
VARIABLE DESCRIPTION TABLE
82
PROGRAM -20
Write a program to print a magic square of size ‘n’ where n is
any
odd number greater than 2.
A Magic square have the sum of its each row, columns
and right and left diagonal equal to each other.
ALGORITHM:
83
CODING:
84
c=0;
}
else if(A[r][c]!=0)
{ r+=
2;
c--;
}
}
System.out.println("Magic square Matrix");
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
System.out.print(A[r][c]+"\t"); //printing matrix
}
System.out.println(); //changing line
}
}
}
85
VARIABLE DESCRIPTION TABLE
86
PROGRAM -21
Class Revstr defines a recursive function to reverse a
string and checkwhether it is a Palindrome. The details
of the class are given below:
Class name : Revstr
Data members/instance variables:
Str : stores the string
Revst : stores the reverse of
the string Member
functions/methods: -
void getStr ( ) : to accept the string.
void check( ) : to display the original string, its reverse
whether the string is a
Palindrome or not
void recReverse (int) : to reverse the string
using the RECURSIVETECHNIQUE.
Specify the class Revstr giving the details of the
functions void getStr( ),void recReverse (int) and
void check(). The main function need not be written
87
ALGORITHM:
88
CODING:
89
else
System.out.println("the two strings are not palindrome");
}
public static void main(String args[])
{
Revstr rs=new Revstr(); // creating object
rs.getstr();
rs.check();
}
}
90
VARIABLE DESCRIPTION TABLE
91
PROGRAM -22
Class Armstrong has been defined to check whether a
number is Armstrong or not. [A number is said to be
Armstrong if sum of cubes of each digit of the number
is equal to the original number].Example: Input: N= 153
== 13+53+33=1+125+27=153 so 153 is an Armstrong
number.
The details of the class are given below:
Member Functions/Methods
Armstrong(int x) : constructor to assign x to Num
int RecCubeDigit(int q ) : to find and return q raised to
the power 3 int RecDigitSum(int N) : to extract the
digits of N using Recursive technique and returns the
sum of cubes of digits by invokingfunction
RecCubeDigit()
boolean IsArmstrong() : to invoke recursive function
RecDigitSum() and to check whether num is an
Armstrong number or not and return true or false.void
Display() : by invoking suitable function print whether
Num is Armstrong or not.
92
ALGORITHM:
93
CODING:
class Armstrong
{
int num;int cube;
Armstrong(int x) // parameterized constructor
{
num=x;
}
int RecCubeDigit(int q) // calculating and returning cube of a digit
{
return (int)Math.pow(q,3);
}
int RecDigitSum(int N) // calculating the sum of cubes of digits
{ if(N==
0)
return 0;
else
return RecCubeDigit(N%10)+RecDigitSum(N/10);
}
boolean isArmstrong()
{
cube=RecDigitSum(num);
if(cube==num) // checking if sum of cubes of digits is equal to the
number
return true;
else
return false;
}
void Display()
{
94
if(isArmstrong()==true)
System.out.println("Armstrong Number");else
System.out.println("not an Armstrong number");
}
public static void main(String args[])
{
Scanner sc= new Scanner (System.in);
System.out.println(“enter a
number”); int n1= sc.nextInt();
Armstrong ob=new Armstrong(n1); // creating object
ob.Display();
}
}
95
VARIABLE DESCRIPTION TABLE
96
PROGRAM -23
Design a class MatRev to reverse each element of a
matrix Example some of the members of the class are
given below:
Class name : MatRev
72 371 5
12 6 426
5 123 94
Member function/methods :
MatRev(int mm,int nn) : parameterized constructor to
initialize data membersm=mm and n=nn
Void fillarray() : to enter elements in the array
Int reverse(int x) : returns the reverse of the number
Void revMat(MatRev p) : reverses each element of the
array of the parameterized object and stores it in the array
of the current object Void show() : displays the array
elements in matrix form
Define a class MatRev giving details of the
constructor(),void fillarray(), int reverse(int i), void
revMat(MatRev) and void show(). Define the
main()function to create objects and call the functions
accordingly to enable the task.
97
ALGORITHM:
98
CODING:
import java.util.*; //importing util package
class Matrev
{
int arr[]
[]; int m;
int n;
Matrev(int mm,int nn)// parameterized constructor
{
m=mm;n=nn;
arr=new int[m][n];
}
void fillarray()
{
Scanner sc=new Scanner(System.in);
int i;int j;
System.out.println("enter the elements of the array");
for(i=0;i<m;i++) // loop to fill the elements
{
for(j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();
}
}}
int reverse(int x) // calculates the reverse of the number
{
int rev=0;
while(x!=0)
{
int d=x%10;
rev=rev*10+d;
}
99
return rev; // returns reverse
}
void revMat(Matrev p)
{
int i;int j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
arr[i][j]=reverse(p.arr[i][j]); // storing the reverse in current
object’s arr[][]
}}
}
void show() // function to display the array elements
{
int r;int c;
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
{
System.out.print(arr[r][c]+" ");
}
System.out.println();
}}
public static void main(String args[])
{
Scanner sc= new Scanner (System.in);
System.out.println(“ no of rows and columns ”);
int M1=sc.nextInt();
int N1=sc.nextInt();
Matrev p=new Matrev(M1,N1); // creating object
Matrev q=new Matrev(M1,N1); // creating object
p.fillarray();
q.revMat(p);
100
System.out.println(“MATRIX BEFORE REVESING”);
p.show();
System.out.println(“MATRIX AFTER REVESING”);
q.show();
}
}
101
VARIABLE DESCRIPTION TABLE
102
PROGRAM -24
Queue is an entity which can hold a maximum of 100
integers. The queue enables the user to add integers
from the rear and remove integers from the front.
Define a class Queue with the following details:
Member functions/methods
Queue(int mm) : constructor to initialize the data size = mm,
front = 0, rear = 0
void addele(int v ) : to add integer from the rear if possible else display
themessage “Overflow”
int delele( ) : returns elements from front if present, : otherwise displays
the message “Underflow” and return
-9999 void display () : displays the
array elements
Specify the class Queue giving details of ONLY the
functions void addele(int) and int delele( ). Assume
that the other functions have been
defined.
The main function and algorithm need NOT be written..
103
ALGORITHM:
104
CODING:
import java.util.*;
class Queue
{
int arr[];
int front,rear,size;
Queue(int mm) // parameterized constructor
{
size=mm;front=rear=0;
arr=new int[size];
}
void addele(int v) // adds element from rear
{
if(rear==size)
System.out.println("queue overflow");
else
arr[rear++]=v;
}
int delele() // returns elements from front
{
if(front==rear)
{
System.out.println("queue underflow");
return 9999;
}
else
return arr[front++];
}
void display() // displays the array
{
for(int x=0;x<=rear;x++)
System.out.print(arr[x]+",");
105
}
public static void main(String args[])
{
Queue q=new
Queue(3); q.addele(10);
q.addele(20);
q.addele(30);
q.addele(40);
System.out.println("the values are");
System.out.println(q.delele());
System.out.println(q.delele());
System.out.println(q.delele());
System.out.println(q.delele());
}
}
106
VARIABLE DESCRIPTION TABLE
107
PROGRAM -25
Write a program to print all the two digit prime numbers
with the help of recursive method :- int isPrime(int N,int
D) which returns 1 if prime otherwise returns 0.
ALGORITHM:
108
CODING:
class Prime
{
static int isPrime(int N,int D) // function to check whether number
is prime
{
if(N==D) // base case
return 1;
else if(N%D==0) // base case that checks for remainder
return 0;
else return isPrime(N,D+1); // recursive case
}
public static void main()
{
System.out.print("two digit prime number are");
for(int num=10;num<100;num++)
{
if(isPrime(num,2)==1) // invokes function and checks if number is
prime
System.out.print(num + ","); // prints the prime numbers
}
}
}
109
VARIABLE DESCRIPTION TABLE
VARIABLE DATA TYPE PURPOSE
110
PROGRAM -26
A class telecall calculate the monthly bill of a customer
Data members-
phno : phone number
n : number of phone
calls name : name
of customer amt :
bill amount
Member methods-
telecall() : parameterized constructor
void compute () : to calculate the details
void dispdata() : to display the details
1-100 Rs 500/- rental only
101-200 Rs 1.00/call+R.C
201-300 Rs 1.20/call+R.C
Above 300 Rs 1.50/call+R.C
ALGORITHM:
111
CODING:
import java.util.*; //importing util package
class telecall
{
int phno,n; //declaration of global variables
double amt;
String name;
telecall(int a,int b,String s) //parameterizrd
constructor
{
phno=a;
n=b;
name=s;
}
public void compute() //to calculate the details
{
double m;
if(n<=100)
m=500;
else if(n>100&&n<=300)
m=1000+100+(n-200)*1.2;
else
m=1120+(n-300)*1.5;
amt=m;
}
void dipdata()// to display the details
{
System.out.println("PHONE NUMBER: "+phno);
System.out.println("NAME: "+name);
System.out.println("TOTAL CALLS: "+n);
112
System.out.println("AMOUNT: "+amt);
}
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
System.out.println("ENTER NAME");
String n1=sc.nextLine(); //stores name entered by user
System.out.println("ENTER PHONE NUMBER");
int a1=sc.nextInt();//stores phone no. entered by user
System.out.println("ENTER TOTAL CALLS");
int c1=sc.nextInt();//stores total calls entered by user
telecall obj = new telecall(a1,c1,n1);
obj.compute(); //object calling
obj.dipdata();
} //closing of main()
}// closing of class
113
VARIABLE DESCRIPTION TABLE-
VARIABLE DATA TYPE PURPOSE
114
PROGRAM -27
Q1- Write a program to enter the day in dd mm yy format and print
it as following:-
Input : 23 04 2014
Output : 23rd April ,2014
ALGORITHM:
115
CODING:
import java.util.*; //importing util package
class Date_Format
{
public static void main(String[] args)
{
Scanner st=new Scanner(System.in);
int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //Stores days
System.out.println("Enter Date");
int dd=st.nextInt(); //Stores date entered by user
int mm=st.nextInt(); //Stores month entered by
user int yy=st.nextInt(); //Stores year entered by user
String mon[]={"","January","Febuary","March","April","May","June"
,"July","August","September","October","November","December"};
String suff=""; //Stores suffix
if(dd==2||dd==22)
suff="nd";
else if(dd==1||dd==21)
suff="st";
else if(dd==3||dd==23)
suff="rd";
116
else
suff="th";
System.out.println("Date is "+dd+""+suff+""+mon[mm]+","+yy);
//Prints
} //closing of main()
} //closing of class
117
VARIABLE DESCRIPTION TABLE-
118
PROGRAM -28
Write a program to enter the numbers and sort them using
insertion sort.
ALGORITHM:
119
CODING:
import java.util.*; //importing util package
class Insertion_sort
{
public static void main(String []args)
{
Scanner st=new Scanner (System.in);
System.out.println("Enter the size of the array");
int n= st.nextInt(); //Enter size of the array
int arr[]=new int[n]; //Creating array
int i=0,j=0,val=0;
System.out.println("Enter values");
for(i=0;i<n;i++)
{
arr[i]=st.nextInt(); //Takes input of values
}
for(i=1;i<n;i++)
{
j=i-1;
val=arr[i];
while(j>=0&&val<arr[j])
{
arr[j+1]=arr[j]; //Swapping values
j--;
120
}
arr[j+1]=val;
}
System.out.println("Array after sorting");
for(i=0;i<n;i++)
{
System.out.println(arr[i]+"\t"); //Printing values
}
}
}
121
VARIABLE DESCRIPTION TABLE-
122
PROGRAM -29
Write a program to store n elements in an array and remove the
element if an entered index number.
ALGORITHM:
123
CODING:
import java.util.*; //importing util package
class Removing_positon
{
public static void main(String[] args)
{
Scanner st=new Scanner(System.in);
System.out.println("Enter size");
int n=st.nextInt(); //Enter size of the array
int arr[]=new int[n]; //Creating array
int x;
System.out.println("Enter elements");
for(x=0;x<n;x++)
{
arr[x]=st.nextInt(); //Takes input of values
}
System.out.println("Enter index number from which element has
to be removed");
int pos=st.nextInt(); //Takes input of index number
for(x=pos;x<arr.length-1;x++)
{
arr[x]=arr[x+1]; //Swapping values
}
arr[arr.length-1]=0; //Storing zero at last index number
System.out.println("Array after removing element");
for(x=0;x<arr.length;x++)
{
System.out.println(arr[x]); //Printing values
}
}}
124
VARIABLE DESCRIPTION TABLE-
VARIABLE DATA TYPE PURPOSE
125
PROGRAM -30
Write a program to enter a word and print all the alphabets in
ascending order. Then print the word with all the missing alphabets
in between.
Input : computer
Output : cemoprtu
cdefghijklmnopqrstu
ALGORITHM:
126
CODING:
import java.util.*; //importing util package
class Alphabets
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter String");
String str=sc.nextLine().toUpperCase(); //Enters string
int x=0;
char ch,ch1,ch2;
String st="";
for(ch='A';ch<'Z';ch++)
{
for(x=0;x<str.length();x++)
{
if(str.charAt(x)==ch) //Fetching characters
st=st+str.charAt(x); //Creating new string
}
}
System.out.println("In ascending order"+st); //Prints string in
order
System.out.println("Missing
alphabets:"); ch1=str.charAt(0);
ch2=str.charAt(str.length()-1);
for(ch=ch1;ch<=ch2;ch++) //Loop to print missing string
{
System.out.print(ch+" "); //Printing missing values
}
}
}
127
VARIABLE DESCRIPTION TABLE-
128
BIBLIOGRAPHY
I have prepared this project by using the following books
and files:-
129