Computer Project
Computer Project
2023-24
SUBMITTED BY
SOHAM
SUBMITTED TO
MAM GANGA DEVI
ACKNOWLWDGEMENT
First of all, I would like to thank my
parents for helping me out with the project.
Output:
PROGRAM 2: To check if 2D array is symmetric or not
ALGORITHM
STEP 1: START
STEP 2: Accept value of n
STEP 3: for i=0 to n, repeat STEP 4
STEP 4: for j=0 to n, repeat STEP 5
STEP 5: read a[i,j]
STEP 6: for i=0 to n, repeat STEP 7
STEP 7: for j=0 to n-1, repeat STEP 8
STEP 8: if a[i,j] ≠a[j,i] is true, c=c+1
STEP 9: for i=0 to n, repeat STEP 10
STEP 10: for j=0 to n, repeat STEP 11
STEP 11: Display c[i,j]
STEP 12: if c=0 is true then display Array is symmetric is not symmetric
STEP 13: END
VARIABLE DESCRIPTION:
Sl. no Variable Datatype Purpose
Name
1 N int To store
array size
2 a char[][] To store
character
3 c,i,j int Counter
OUTPUT:
PROGRAM 3: To find product of two (n*n)matrix
ALGORITHM
STEP 1: START
STEP 2: Accept value of n
STEP 3: for i=0 to n, repeat STEP 4
STEP 4: for j=0 to n, repeat STEP 5
STEP 5: read a[i,j]
STEP 6: for i=0 to n, repeat STEP 7
STEP 7: for j=0 to n, repeat STEP 8
STEP 8: read b[i,j]
STEP 9: for i=0 to n, repeat STEP 10
STEP 10: for j=0 to n, repeat STEP 11
STEP 11: for k=0 to n, repeat STEP 12
STEP 12: c[i,j] = a[i,j]* b[i,j] + c[i,j]
STEP 13: for i=0 to n, repeat STEP 14
STEP 14: for j=0 to n, repeat STEP 15
STEP 15: Display c[i,j]
STEP 16: END
SOLUTION:
VARIABLE DESCRIPTION:
Sl. no Variable Datatype Purpose
Name
1 n int To store
size of
matrix
2 a,b,c int[][] To store
number
3 i,j,k int Counter
OUTPUT:
PROGRAM 4: Magic number by using recursion
ALGORITHM
main()
STEP 1: Accept number and store in variable a
STEP 2: Repeat STEP 3 until a>9
STEP 3: Calculate magic number
STEP 4: Check for magic number
STEP 5: If STEP 4 is true, display Magic number OTHERWISE display
not a magic number
sum_digit(int n)
STEP 1: Extract digits from the number and store in variable r
STEP 2: if (n=0) is true then return 0
STEP 3: Otherwise return (r+sum_digit(n/10))
SOLUTION:
VARIABLE DESCRIPTION:
OUTPUT:
PROGRAM 5:Write a program to declare a single-dimensional array
a[] and a square matrix b[][] of size N, where N > 2 and N < 10. Allow the
user to input positive integers into the single dimensional array.
2. Fill the square matrix b[][] in the following format: If the array a[] = {5,
2, 8, 1} then, after sorting a[] = {1, 2, 5, 8} Then, the matrix b[][] would fill as
below:
Test your program for the following data and some random data:
Example 1
INPUT: N = 3 ENTER ELEMENTS OF SINGLE DIMENSIONAL
ARRAY: 3 1 7
OUTPUT: SORTED ARRAY: 1 3 7 FILLED MATRIX
Example 2
INPUT: N = 13
OUTPUT: MATRIX SIZE OUT OF RANGE
Example 3
INPUT: N = 5 ENTER ELEMENTS OF SINGLE DIMENSIONAL
ARRAY: 10 2 5 23 6
OUTPUT: SORTED ARRAY: 2 5 6 10 23 FILLED MATRIX
SOLUTION:
OUTPUT:
PROGRAM: 6
Armstrong Number by Using Recursion
(153 =>13+53+33=153)
ALGORITHM
main()
STEP 1: Accept number and store in variable a
STEP 2: Repeat STEP 3 until a>9
STEP 3: Calculate magic number
STEP 4: Check for magic number
STEP 5: If STEP 4 is true, display Magic number OTHERWISE display not a
magic number
checknum(int n)
STEP 1: Receive actual parametric value in variable n
STEP 2: if (n=0) is true then return 0
STEP 3: Otherwise (int)Math.pow(n%10,3)+ checknum(n/10);
SOLUTION:
VARIABLE DESCRIPTION:
OUTPUT:
PROGRAM 7: Decimal To Binary Conversion Using Recursion
(10=>1010)
ALGORITHM:
main()
STEP 1: Accept number
task(int n)
STEP 1: Proceed if n>2
STEP 2: store remainder in variable d when n is divided by 2
STEP 3: task(n/2)
STEP 4: Display d backwards in horizontal line
SOLUTION:
VARIABLE DESCRIPTION:
Sl. no Variable Name Datatype Purpose
1 a int To get number
2 d int Binary
calculation
3 n int User iput
OUTPUT:
A Prime-Adam integer is a positive integer (without leading zeros) which is a prime as
well as an Adam number.
Prime number: A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 … etc.
Adam number: The square of a number and the square of its reverse are reverse to each
other.
Example: If n = 13 and reverse of 'n' = 31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169
thus 13, is an Adam number.
PROGRAM 8:Accept two positive integers m and n, where m is less than n as user input.
Display all Prime-Adam integers that are in the range between m and n (both inclusive)
and output them along with the frequency, in the format given below:
Test your program with the following data and some random data:
Example 1
INPUT:
m=5
n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2
INPUT:
m = 100
n = 200
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3
INPUT:
m = 50
n = 70
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0
Example 4
INPUT:
m = 700
n = 450
OUTPUT:
INVALID INPUT
SOLUTION:
import java.util.Scanner;
public class PrimeAdam
{
public static int reverse(int num) {
int rev = 0;
while (num != 0) {
int d = num % 10;
rev = rev * 10 + d;
num /= 10;
}
return rev;
}
return c == 2;
}
if (count == 0) {
System.out.print("NIL");
}
System.out.println();
System.out.println("FREQUENCY OF PRIME-ADAM INTEGERS IS: " + count);
}}
OUTPUT:
A Goldbach number is a positive even integer that can be expressed as the sum
of two odd primes.
Note: All even integer numbers greater than 4 are Goldbach numbers.
Example:
6=3+3
10 = 3 + 7
10 = 5 + 5
Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime
pairs, i.e. 3 and 7, 5 and 5.
PROGRAM 9:
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'.
Test your program with the following data and some random data:
Example 1
INPUT:
N = 14
OUTPUT:
PRIME PAIRS ARE:
3, 11
7, 7
Example 2
INPUT:
N = 30
OUTPUT:
PRIME PAIRS ARE:
7, 23
11, 19
13, 17
Example 3
INPUT:
N = 17
OUTPUT:
INVALID INPUT. NUMBER IS ODD.
Example 4
INPUT:
N = 126
OUTPUT:
INVALID INPUT. NUMBER IS OUT OF RANGE.
SOLUTION:
OUTPUT:
PROGRAM 10:
Input a paragraph containing 'n' number of sentences where (1 < = n < 4). The words are
to be separated with a single blank space and are in UPPERCASE. A sentence may be
terminated either with a full stop '.' Or a question mark '?' only. Any other character may
be ignored. Perform the following operations:
Accept the number of sentences. If the number of sentences exceeds the limit,
an appropriate error message must be displayed.
Find the number of words in the whole paragraph.
Display the words in ascending order of their frequency. Words with same
frequency may appear in any order.
Example 1
Input:
Enter number of sentences.1
Enter sentences.
TO BE OR NOT TO BE.
Output:
Total number of words: 6
Word Frequency
OR 1
NOT 1
TO 2
BE 2
Example 2
Input:
Enter number of sentences.3
Enter sentences.
THIS IS A STRING PROGRAM. IS THIS EASY? YES IT IS.
Output:
Total number of words: 11
Word Frequency
A 1
STRING 1
PROGRAM 1
EASY 1
YES 1
IT 1
THIS 2
IS 3
Example 3
Input:
Enter number of sentences. 5:
Output : Invalid Entry
import java.util.Scanner;
if (n < 1 || n > 3) {
System.out.println("Invalid Entry");
return;}
System.out.println("Enter sentences.");
String ipStr = sc.nextLine();
ipStr = ipStr.toUpperCase();
System.out.println();
System.out.println("Total number of words: " + wordCount);
Specify the class Transarray giving the details of the constructors, and fuctions void
fillarray(), void transpose(Transarray) and void disparray().
SOLUTION:
OUTPUT:
PROGRAM 12:A superclass Product has been defined to store the details of a product
sold by a wholesaler to a retailer. Define a subclass Sales to compute the total amount
paid by the retailer with or without fine along with service tax.
Some of the members of both classes are given below:
Class name: Product
Data members/instance variables:
name: stores the name of the product
code: integer to store the product code
amount: stores the total sale amount of the product (in decimals)
Member functions/methods:
Product (String n, int c, double p): parameterized constructor to assign data members: name =
n, code = c and amount = p
void show(): displays the details of the data members
OUTPUT:
PROGRAM 13: A library issues books on a rental basis at a 2% charge on the cost price
of the book per day. As per the rules of the library, a book can be retained for 7 days
without any fine. If the book is returned after 7 days, a fine will also be charged for the
excess days as per the chart given below:
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 and another class Compute to perform
the task. The details of the 2 classes
are given below:
Class name: Library
Data members:
name : name of the book
author : author of the book
p : price of the book in decimals
Member functions
Library(...) : parameterized constructor to assign values to data members.
void show() : display the book details
Class name: Compute
Data members:
d : number of days taken in returning the book.
f : to store the fine.
Member functions
Compute(...) : parameterized constructor to assign values to data members of both class
void fine() : calculate the fine for excess days.
void display() : displays the book details along with the number of days, fine and total
amount to be paid. The amount is calculated as: (2% of price of book*total no. of days)+
fine.
Specify the class Library giving details of the constructor all the member function of
both the classes
ALGORITHM:
1.Start.
2.A base class library is created with given parameter of details.
3.All the parameters are initialized in the constructor.
4.Another class named compute is created which inherits its properties from the base class
library.
5.The derived class gets it’s variables initialized alongwith the variables of the super class.
6.The keyword super is used to call the constructor of the super class and pass values to the
respective variables.
7.The calculation of the fine is done in void fine() function with the provided conditions.
8.Then the final amount is printed with all the information in void show().
9.Another class is created to create main for ease of access.
10.Object of class compute is created and the required functions are called.
11.Stop
PARENT CLASS
CHILD CLASS
OUTPUT:
VARIABLE DESCRIPTION:
Variable Type Description
name String To store name of the book
author String To store author of the book
p Double To store price of the book
in decimals
f Double To store the calculated fine
d Int To store number of days
taken in returning the
book.
sc Object variable To accept a value from
console
nr String To store name of the book
pr Double To store price of the book
in decimals
ar String To store author of the book
dr Int To store number of days
taken in returning the
book.
PROGRAM 14:A class Student defines the personal data of a student while another
class Marks defines the register number, name of subject and marks obtained by the
student. The details of both the classes are given below:
Class name : Student
Data members / instance variables:
name : variable to store the name
sex : character variable to store sex ‘F’ or ‘M’
age : integer variable to store the age
Member functions/methods
Student(……) : parameterised constructor
void inpdetailS() : to accept values for data members
void studDisplay () : to display personal data of student
OUTPUT:
PRORAM 15:A super class Stock has been defined to store the details of the
stock of a retail store. Define a subclass Purchase to store the details of the
items purchased with the new rate and updates the stock. Some of the
members of the classes are given below
Class name: Stock
Data members/instance variables:
item : to store the name of the item
qt : to store the quantity of an item in stock
rate : to store the unit price of an item
amt : to store the net value of the item in stock
Member functions:
Stock (…) : parameterized constructor to assign values to the data
members
void display() : to display the stock details
CHILD CLASS:
PROGRAM 16:Given two positive numbers M and N, such that M 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 the 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 user and print the smallest
required number whose sum of the 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. Test your program with
the sample data and some random data:
Example 1 Example 3
Input: M = 100 Input: M=99
N = 11 N = 11
Output: The required number = 119 Output: Invalid Input
Total number of digits = 3
Example 2 Example 4
Input: M = 1500 Input: M=112
N = 25 N=130
Output: The required number = 1699 Output: Invalid Input
Total number of digits = 4
SOLUTION:
OUTPUT:
PROGRAM 17: Write a program to declare a matrix A [][] of order (MXN) where 'M' is
the number of rows and 'N' is the number of columns such that both M and N must be
greater than 2 and less than 20. Allow the user to input integers into this matrix.
Perform the following tasks on the matrix:
(a)Display the input matrix
(b)Find the maximum and minimum value in the matrix and display them along with
their position.
(c)Sort the elements of the matrix in ascending order using any standard sorting
technique and rearrange them in the matrix.
(d)Output the rearranged matrix.
Test your program with following data values and also some random data:
Example 1
INPUT:
M=3
N=4
Original matrix:
8 7 9 3
-2 0 4 5
1 3 6 -4
Largest Number: 9
Row: 0
Column: 2
Smallest Number: -4
Row=2
Column=3
Rearranged matrix:
-4 -2 0 1
3 3 4 5
6 7 8 9
SOLUTION:
OUTPUT:
PROGRAM 18:
Write a program to accept a sentence which may be terminated by either'.' or '?' only.
The words are to be separated by a single blank space. Print an error message if the input
does not terminate with '.' or '?'. You can assume that no word in the sentence exceeds 15
characters, so that you get a proper formatted output.
(ii)Find the number of vowels and consonants in each word and display them with proper
headings along with the words.
Example 1:
Example 2:
Example 3
OUTPUT:
PROGRAM 20: Write a program to accept a sentence as input. The words in the string
are to be separated by a blank. Each word must be in upper case. The sentence is
terminated by either '.' , '!' or '?' . Perform the following tasks:
Example 2
SOLUTION:
OUTPUT:
POGRAM 21: A class Recursion has been defined to find the Fibonacci series upto a
limit. Some of the members of the class are given below:
Class Name :Recursion
DataMembers/instance variables : a, b, c, limit (all integers)
Member functions/methods :
Recursion ( ) : constructor to assign a,b,c with appropriate values.
void input ( ) : to accept the limit of the series.
int fib(int n) : to return the nth Fibonacci term using recursive technique.
void generate_fibseries() : to generate the Fibonacci series upto the given limit, using
recursive function int fib( ).
Specify the class Recursion giving details of constructor, fib( ), voidgenerate_fibseries( ).
SOLUTION:
OUTPUT:
PROGRAM 22: A super class Detail has been defined to store the details
of a customer. Define a subclass Bill to compute the monthly telephone
charge of the customer as per the chart given below:
Number Rate
Of Calls
1 – 100 Only Rental charge
101 – 200 60 paisa per call + rental charge
201 – 300 80 paisa per call + rental charge
Above 300 1 rupee per call + rental charge
CHILD CLASS:
PROGRAM 23: A line on a plane can be represented by coordinates of the
two-end points p1 and p2 as p1(x1,y1) and p2(x2,y2).
A super class Plane is defined to represent a line and a subclass Circle to find
the length of the radius and the area of Circle by using the required data
members of super class.
Some of the members of both the classes are given below:
Class name : Plane
Data members/instance variables:
x1 : to store the x-coordinate of the first end point
y1 : to store the y-coordinate of the first end point
Member functions/methods:
Plane(int nx,int ny) : parameterized constructor to assign the data
members x1=nx
and y1=ny
void Show() : to display the coordinates
Class name : Circle
Data members/instance variables:
x2 : to store the x-coordinate of the second end point
y2 : to store the y-coordinate of the second end point
radius : double variable to store the radius of the circle
area : double variable to store the area of the circle
Member functions / methods:
Circle(…) : parameterized constructor to assign values to data members of
both the
classes
void findRadius( ) : to calculate the length of radius using the formula:
(𝑥2−𝑥1) 2 + (𝑦2−𝑦1) 2
assuming that x1, x2, y1, y2 are
2
the coordinates of the two ends of the diameter of a circle.
void findArea( ) : to find the area of circle using formula: π𝑟 2 .
22
The value of π is or 3.14
7
void Show( ) : to display both the coordinates along with the length of the
radius and
area of the circle.
Specify the class Plane giving details of the constructor and void Show().
Using the concept of inheritance, specify the class Circle giving details of the
constructor, void findRadius(), void findArea() and void Show().
The main function and algorithm need not be written.
PARENT CLASS:
CHILD CLASS:
PROGRAM 24: Design a program to accept a day number (between 1 and 366), year (in 4
digits) from the user to generate and display the corresponding date. Also accept 'N'
(1<=N<=100) from the user to compute and display the future date corresponding to 'N' days
after the generated date. Display error message if the value of the day number, year and N are
not within the limit or not according to the condition specified.
Test your program for the following data and some random data.
Example 1
INPUT:
DAY NUMBER: 255
YEAR: 2018
DATE AFTER (N DAYS): 22
OUTPUT:
DATE: 12TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4TH OCTOBER, 2018
Example 2
INPUT:
DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45
OUTPUT:
DATE: 26TH DECEMBER, 2018
DATE AFTER 45 DAYS: 9TH FEBRUARY, 2019
Example 3
INPUT:
DAY NUMBER: 500
YEAR: 2018
DATE AFTER (N DAYS): 33
OUTPUT: DAY NUMBER OUT OF RANGE
SOLUTION:
OUTPUT:
PROGRAM 25:
To accept elements in a Double-Dimensional Array of N x N order and print
the sum of the elements of Left as well as Right Diagonal.
Output:
Left Diagonal Sum =15
Right Diagonal Sum =10
SOLUTION:
OUTPUT:
PROGRAM 26: A double dimensional array is defined as N[4][4] to store
numbers. Write a program to find the sum of all even numbers and product of
all odd numbers of the elements stored in Double Dimensional Array (DDA).
Sample Input:
12 10 15 17
30 11 32 71
17 14 29 31
41 33 40 51
Sample Output:
Sum of all even numbers: ……….
Product of all odd numbers: ……….
SOLUTION:
OUTPUT:
PROGRAM 27: The coordinates of a point P on a two-dimensional plane can be
represented by P(x, y) with x as the x-coordinate and y as the y-coordinate. The
coordinates of the midpoint of two points P1(x1, y1) and P2(x2, y2) can be calculated as
P(x, y) where:
Sample Input
10 15 16 18
15 14 12 11
11 12 16 17
12 10 14 16
Sample output:
10 15 16 18 59
15 14 12 11 52
11 12 16 17 56
12 10 14 16 52
48 51 58 62
SOLUTION:
OUTPUT
PROGRAM 29:Write a program in Java to create a 4 x 4 matrix. Now, swap the
elements of 0th row with 3rd row correspondingly. Display the result after swapping.
Sample Input
55 33 26 14
81 86 31 10
58 64 17 12
22 14 23 25
Sample Output
22 14 23 25
81 86 31 10
58 64 17 12
55 33 26 14
SOLUTION:
OUTPUT:
PROGRAM 30: Write a program in Java to enter natural numbers in a double
dimensional array m x n (where m is the number of rows and n is the number
of columns). Display the new matrix in such a way that the new matrix is the
mirror image of the original matrix.
Sample Input
8 15 9 18
9 10 7 6
10 8 11 13
12 16 17 19
Sample Output
18 9 15 8
6 7 10 9
13 11 8 10
19 17 16 12
SOLUTION
OUTPUT:
BIBLIOGRAPHY
For completing this project, I have taken help from :
Understanding I.S.C. Computer Science (Java with Blue J)
Class- XII