di
di
Divyansh Nigam
15, October,2024
St. Lawrence School
10- ‘C’
2
Tabel of Content
Page no Ques ons
1) Title page
2) Tabel of Content
3) Acknowledgement
4) Q1 Superspy number
5) Q2 Duck number
6) Q3 (i)Binary number to Decimal
7) (ii)Decimal number to Binary
8) Q4 Special Armstrong number
9) Q5 Overload func on sumseries(int n , double x)(1)
10) (2) sumseries(int start, int end)
11) Q6 Overload func on joystring(String s)(1)
12) (2)void joystring(String str, int p)
13) Q7 (i)pa ern 1
14) (ii)pa ern 2
15) Q8 Maximum number of characters
16) Q9 Convert to uppercase
17) Q10 To print the frequency of the given word in the sentence
18) Q11 To Display the words which begin and ends with a same character
19) Q12 To display the names in ascending order using the Bubble sort technique
20) Q13 To display obtained in descending order using Selec on sort technique
21) Q14 To search for an integer value input by the user using Binary search technique
22) Q15 To display the sum of each row in 4x4 array
23) Q16 To accept the names of 10 students in an array and check for the existence of the
given name in the array using “Linear search technique”
24) Q17 Special array for 3x3 array
25) Q18 To store numbers in 4×4 array in double dimensional array and display the largest
and the smallest value within the array
26) Q19 class Stock
27) …………..
28) Q20 class SmartShop
29) ………….
30) References
3
ACKNOWLEDGEMENT
I would like to take this opportunity to express my hear elt apprecia on to
everyone who contributed to the successful comple on of my computer project. This
project has been a significant learning experience for me, and I am grateful for the
support and guidance I have received along the way.
First and foremost, I want to extend my sincere thanks to my Computer
Teacher, Mrs. Farheen Fa ma Your exper se, encouragement, and willingness to
answer my ques ons have been invaluable throughout this project. Your dedica on
to teaching and your passion for the subject have inspired me to delve deeper into
the world of computer science.
I would also like to acknowledge the role of our school principal, Fr. Olwin
J.Moras. Your leadership and vision have created an educa onal environment that
encourages students to pursue their interests and excel in their chosen fields. Your
support for our endeavors has been a driving force behind our success.
While this project was an individual effort, the collec ve support of my
teacher and principal, along with the encouragement from my loved ones, has made
it possible. I am grateful to each and every one of you for being a part of my journey
and for helping me achieve this milestone.
Thank You.
Divyansh Nigam
4
Ques on 1: Define a class to accept a number and check whether it is a SUPERSPY number
or not. (A number is called SUPRSPY if the sum of the digits equals the number of the
digits.)
Solu on: -
import java.u l.Scanner;
public class $1
{
public sta c void main(String args[])
{ int s=0;int x=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");==
int n=sc.nextInt();
int d=n;
while(n>0)
{int l=n%10;
s=s+l;
n=n/10;
x++;
}
if(x==s)
{
System.out.println("SUPERSPY number");
}
else
{ System.out.println(": Not an SUPERSPY number"); } } }
Variable descrip on: -
Ques on 2: Define a class to accept a 3-digit number and check whether it is a duck number
or not.
Solu on: -
import java.u l.Scanner;
public class $2 {
public sta c void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n=sc.nextInt();int s=0;
if((n>99)&&(n<1000))
{ while(n>0){
int l=n%10;
if(l==0)
{ s=1;break;}
else
{ s=0; }
n=n/10;
}
if(s==1)
{ System.out.println("Duck number"); }
else
{ System.out.println("Invalid"); }
else
{ System.out.println("Invalid"); }
}
}
Variable descrip on: -
Ques on 3: Write a menu driven class (using switch case) to perform the following:
(i) Input a Binary number (base 2) and convert it into its decimal equivalent (base 10)
(ii) Input a Decimal number (base 10) and convert it into its Binary equivalent (base 2)
Solu on: -
import java.u l.Scanner;
public class $3
{
public sta c void main(String args[])
{ Scanner sc= new Scanner(System.in);
System.out.println("Enter 1 for binary to decimel conversion");
System.out.println("Enter 2 for decimel to binary conversion");
int x=sc.nextInt();
switch(x)
{
case 1:
System.out.println("Enter the number");
int n=sc.nextInt();
int i=0;
int s=0;
while(n>0)
{
x=n%10;
s=s+x*(int)Math.pow(2,i);
i++;
n=n/10;
}
break;
//(ii) Input a Decimal number (base 10) and convert it into its Binary equivalent (base 2)
case 2:
System.out.println("Enter the number");
int q=sc.nextInt();
int p;
String l=" ";
while(q>0)
{
p=q%2;
l=String.valueOf(p)+l;
q=q/2;
}
System.out.println("The converted number is "+l);
break;
}
}
}
Variable descrip on: -
Ques on 4: A number is said to be Special Armstrong if sum of its digits raised to the power
of length of the number is equal to the number. Write a program to generate and print all
the Special Armstrong numbers between 500 and 2000.
Solu on: -
public class $4 {
public sta c void main(String args[])
{
for(int i =500 ; i<=2000; ++i )
{
int n =i, num=i , sum=0 ; int count = 0 ; int rem = 0;
while (n >0)
{
n = n / 10;
count ++;
}
while (num>0)
{ rem = num %10;
sum = sum + (int)Math.pow(rem , count );
num = num/10;
}
if (i == sum ) { System.out.println(i);
}
}
}
Variable descrip on: -
double su=1/Math.pow(x,l);
for(int k=1;k<n;k++)
{
su=su+(r+3)*(1.0*s)/Math.pow(x,(l+3));
s=s*-1;
r=r+3;
l=l+3;
}
System.out.println("Sum "+su);
}
11
//(2)
public void sumseries(int start,int end)
{ int sum = 0;
for (int i = start; i <= end; i++)
{
int count = 0;
if (i > 1) { for (int j = 2; j <= Math.sqrt(i); j++)
{
if (i % j == 0) {count++;break; }
}
if (count == 0) { sum += i;
}
} } System.out.println("The sum of all prime numbers between " + start+ " and " + end + "
is: " + sum);
}
}
Variable descrip on: -
(1) void joystring(String s) with one string argument in lowercase, change the first le er of
every word to uppercase. Display the new string.
(2) void joystring(String str, int p) with one string argument and one integer argument. It
displays all the uppercase character if p is 1(one) otherwise it displays all the lowercase
characters
Solu on:-
public class $6 {
public void joystring(String s)
{
String[] words = s.split(" ");
.append(word.substring(1)).append(" ");
}
System.out.println(capitalizedSentence.toString().trim());
}
13
//(2)
Ques on 7: Write a menu driven program to print the given pa ern accos ng to the user’s
choice.
(i) 1
**
23
***
456
Solu on: -
}
}
//(ii)A B C D E
// A B C D
// A B C
// A B
// A
if(x==2)
{
char startChar = 'A';
System.out.println();
}
}
else
{ System.out.println("Invalid input");
}}}
Variable descrip on: -
Ques on 8: Write a program to input a sentence and display the word in the sentence that
contains the maximum number of characters
Solu on: -
import java.u l.Scanner;
public class $8
{
public sta c void main(String args[])
{
Scanner sc= new Scanner (System.in);
System.out.println(" Enter the sentence: ");
String l=sc.nextLine();
String[] words = l.split(" ");
String longest = " ";
int maxLength = 0;
for (int i = 0; i < words.length; i++) {
if (words[i].length() > maxLength) {
maxLength = words[i].length();
longest = words[i];
} } System.out.println("The word with the maximum characters is: " + longest); } }
Variable descrip on: -
String[] words To store the words split from the input sentence
Ques on 9: Define a class to accept a string and convert the same to uppercase,
create and display the new string by replacing each vowel by immediate next
character and every consonant by the previous character. The other characters
remain the same.
Solu on: -
Ques on 10: Write a program in Java to accept a sentence and a word separately. Find and
print the frequency of the given word in the sentence.
Solu on: -
import java.u l.Scanner;
public class $10{
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = sc.nextLine();
System.out.print("Frequency of the word to be searched: ");
String w= sc.nextLine();
String[] wo = s.split(" ");
int fre = 0;
for (int i = 0; i < wo.length; i++) {
if (wo[i].equals(w)) {
fre++;
}
}
System.out.println(" The frequency of the word the is: " + fre);
}
}
Variable descrip on: -
int i An index used in the loop to iterate through the words array
String This variable holds the input sentence provided by the user.
s
String This variable holds the word whose frequency is to be found in the sentence.
w
String[] An array that stores individual words from the input sentence, created by spli ng the sentence on spaces.
wo
int fre An integer variable that keeps track of the number of mes the given word appears in the sentence.
19
Ques on 11: Write a program to input a sentence in uppercase. Display the words which
begin and ends with a same character.
Solu on: -
import java.u l.Scanner;
public class $11 {
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence in uppercase");
String s = sc.nextLine();
String[] wo = s.split(" ");
for (int i = 0; i < wo.length; i++) {
if (wo[i].length() > 1 && wo[i].charAt(0) == wo[i].charAt(wo[i].length() - 1)) {
System.out.print(wo[i]+",");
}
}
}
}
Variable descrip on: -
int i An index used in the loop to iterate through the words array.
String[] An array that stores individual words from the input sentence, created by spli ng the sentence on spaces.
wo
String This variable holds the input sentence provided by the user in uppercase.
s
20
Ques on 12: Define a class to accept the names of 20 students in a one-dimensional array.
Sort the names in ascending order using the Bubble sort technique, and display the
students' names in sorted order.
Solu on: -
import java.u l.Scanner;
public class $12 {
public sta c void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] names = new String[20];
System.out.println("Enter the names of 20 students:");
for (int i = 0; i < 20; i++) {
names[i] = scanner.nextLine(); }
for (int i = 0; i < names.length - 1; i++) {
for (int j = 0; j < names.length - 1 - i; j++) {
if (names[j].compareTo(names[j + 1]) > 0) {
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;}}}
System.out.println("Names of students in ascending order:");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);}}}
Variable descrip on: -
Ques on 13: Define a class to accept the marks secured in “Computer Applica ons” by 20
students of a class in a single dimensional array. Sort the marks obtained in descending
order using Selec on sort technique, and display the marks of students in sorted order.
Solu on: -
import java.u l.Scanner;
public class $13 {
public sta c void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] marks = new int[20];
System.out.println("Enter the marks of 20 students in Computer Applica ons:");
for (int i = 0; i < 20; i++) {
marks[i] = scanner.nextInt(); }
for (int i = 0; i < marks.length - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < marks.length; j++) {
if (marks[j] > marks[maxIndex]) {
maxIndex = j;} }
int temp = marks[maxIndex];
marks[maxIndex] = marks[i];
marks[i] = temp; }
System.out.println("Marks of students in descending order:");
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]); } }}
int i An index used in the outer loop of the Selec on sort to iterate through the array elements.
int temp A temporary variable used to swap elements during the sor ng process.
int j An index used in the inner loop of the Selec on sort to compare elements in the array
int[] A one-dimensional array that stores the marks of 20 students.
marks
int An integer variable used to keep track of the index of the maximum element found
maxIndex in the unsorted part of the array during the Selec on sort process.
22
Ques on 14: Write a program to search for an integer value input by the user in the sorted
list given below using Binary search technique. If found display “Search successful” and the
element, otherwise display “Search Unsuccessful” {31, 36, 45, 48, 50, 51, 67, 78, 89, 90}
Solu on: -
import java.u l.Scanner;
public class $14 {
public sta c void main(String[] args) {
int[] list = {31, 36, 45, 48, 50, 51, 67, 78, 89, 90};
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number to search for: ");
int target = sc.nextInt();
int start = 0;int end = list.length – 1; int index = -1;
while (start <= end) {
int middle = start + (end - start) / 2;
if (list[middle] == target) {
index = middle;
break;
} if (list[middle] < target) {
start = middle + 1; }
else {end = middle - 1;}}
if (index == -1) {
System.out.println("Search Unsuccessful");
} else { System.out.println("Search successful: Element found at index " + index) }}}
int[] list This array stores the given sorted list of integers.
int target This variable holds the integer value input by the user that needs to be searched in the list.
int start This variable represents the star ng index of the current search range in the binary search
int end This variable represents the ending index of the current search range in the binary search
int middle This variable calculates the middle index of the current search range in the binary search
int index This variable stores the index where the target value is found. It is ini alized to -
1 indicate that the target is not found ini ally.
23
Ques on 15: Define a class to accept values into 4×4 array and find and display the sum of
each row.
Solu on: -
import java.u l.Scanner;
public class $15{
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[4][4];
System.out.println("Enter the values into the 4x4 array:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = sc.nextInt(); } }
for (int i = 0; i < 4; i++) {
int sum = 0;
for (int j = 0; j < 4; j++) {
sum += a[i][j];}
System.out.println("The Sum of row " + (i + 1) + " :" + sum); } }}
int i An index used in the outer loops to iterate through the rows of the array.
int j An index used in the inner loops to iterate through the columns of the array.
int sum A variable used to store the sum of the elements in the current row.
int[][] a A two-dimensional array that stores the values of the 4x4 grid.
24
Ques on 16: Define a class to accept the names of 10 students in an array and check for the
existence of the given name in the array using “Linear search technique”, if found print the
posi on of the name, if not found print the appropriate message. Also print the names
which begins with the word “Mr”
Solu on: -
import java.u l.Scanner;
public class $16 {
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] names = new String[10];
System.out.println("Enter the names of 10 students:");
for (int i = 0; i < 10; i++) {
names[i] = sc.nextLine(); }
System.out.print("Enter the name to search: ");
String N = sc.nextLine();
int p = -1;
for (int i = 0; i < names.length; i++) {
if (names[i].equals(N)) {
p= i; break; } }
if (p== -1) { System.out.println("Search Unsuccessful: " + N + " not found");}
else { System.out.println("Search successful: " + N + " found at posi on " + (p+ 1)); }
System.out.println("Names beginning with 'Mr':");
for (int i = 0; i < names.length; i++) {
if (names[i].startsWith("Mr")) {
System.out.println(names[i]);}}}}
String N This variable holds the name input by the user that needs to be searched.
int p This variable stores the index where the searchName is found, ini alized to -1.
int i An index used in the loop to iterate through the names array.
25
Ques on 17: Define a class to accept values into a 3×3 array and check if it is a special array.
An array is a special array if the sum of the even elements = sum of the odd elements
Solu on: -
import java.u l.Scanner;
public class $17{
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[3][3];
int sE = 0;
int sO = 0;
System.out.println("Enter the values into the 3x3 array:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = sc.nextInt();
if (a[i][j] % 2 == 0) {
sE += a[i][j];
} else {
sO += a[i][j];}} }
if (sE == sO) {System.out.println("The array is a special array.");}
else {System.out.println("The array is not a special array."); }}}
Variable descrip on: -
int[][] a A two-dimensional array that stores the values of the 3x3 grid.
int sE An integer variable used to store the sum of even elements in the array.
int sO An integer variable used to store the sum of odd elements in the array.
int i An index used in the outer loop to iterate through the rows of the array.
int j An index used in the inner loop to iterate through the columns of the array.
26
Ques on 18: Write a program to store the numbers in 4×4 array in double dimensional array
and display the largest and the smallest value within the array.
Solu on: -
import java.u l.Scanner;
public class $18 {
public sta c void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a= new int[4][4];
System.out.println("Enter the values into the 4x4 array:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = sc.nextInt();} }
int min = a[0][0];
int max = a[0][0];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (a[i][j] < min) {min = a[i][j]; }}}
System.out.println("The smallest value in the array is: " + min);
System.out.println("The largest value in the array is: " + max); }}
Variable descrip on: -
int a A two-dimensional array that stores the values of the 4x4 grid.
int min An integer variable used to store the smallest value in the array.
int max An integer variable used to store the largest value in the array.
int i An index used in the outer loop to iterate through the rows of the array.
int j An index used in the inner loop to iterate through the columns of the array.
27
Ques on 19: A book seller maintains records of books belonging to the various
publishers.He uses a class with the specifica ons given below:
Data Members
void getdata() To accept tle, author, publisher’s name, and the number of copies.
Solu on: -
import java.u l.Scanner;
public class Stock {
String tle;
String author;
String pub;
int noc;
public Stock() {
tle = "";author = ""
pub = "";noc = 0;}
28
Ques on 20: Define a class SmartShop with the specifica ons given below
double netamount To store the net amount paid by the customer a er the discount
SmartShop() Default constructor to ini alize name = "" and price = 0.0
void accept() To input the name and the price of the item using the method of Scanner class
To calculate the net amount to be paid by the customer, based on the discount criteria provided
Solu on: -
import java.u l.Scanner;
public class SmartShop {
String name;
double price;
double netamount;
public SmartShop() {name = ""; price = 0.0;}
public void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of the item: ");
name = scanner.nextLine();
double discount;
if (price <= 15000)
double netamount Stores the net amount paid by the customer a er the discount
31
References