ICSE Board Examp Java Programs
Question Paper Section - B
ARRAYS
[BINARY SEARCH][M.M.IMP for 2025 Board Exam]
Program 1:Define a class to perform binary search on a list of integers given below, to search for an element input by the user, if it is found display the element along with its position, otherwise display the message “Search element not found”. [ICSE 2020]
2, 5, 7, 10, 15, 20, 29, 30, 46, 50
Solution:-
import java.util.*;
public class BSearch
{
public static void main(String args[])
{
int x[]={2, 5, 7, 10, 15, 20, 29, 30, 46, 50};
int srch;
int l=0,m=0,u=x.length;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Value to Search : ");
srch=sc.nextInt();
while(l<u)
{
m=(l+u)/2;
if(x[m]>srch)
{
u=m-1;
}
else if(x[m]<srch)
{
l=m+1;
}
else
{
pos=m;
flag=1;
break;
}
}
if(flag==1)
{
System.out.println("Value Found at position "+(pos)+" position");
}
else
{
System.out.println("Value Not Found!");
}
}
}
[LINEAR SEARCH]
Program 2: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, if found print the position of the name,
if not found print the appropriate message. Also print the names which begins with the word "SRI".[ICSE 2013 Specimen]
import java.util.*;
public class BSearch
{
public static void main(String args[])
{
String names[]=new String[10];
String srch;
int l=0,m=0,u=x.length;
Scanner sc=new Scanner(System.in);
for(i=0;i<10;i++
{
System.out.println("Enter Name for list");
names[i]=sc.nextLine();
}
System.out.println("Enter Name to Search : ");
srch=sc.nextLine();
for(i=0;i<10;i++)
{
if(names[i].compareToIgnoreCase(srch)==true)
{
pos=i;
flag=1;
break;
}
}
if(flag==1)
System.out.println("Value Found at position "+(pos)+" position");
else
System.out.println("Value Not Found!");
}
}
}
[BUBBLE SORT]
Program 3:Write a program to input 15 integer elements in an array and sort them in ascending order using the bubble sort technique.[ICSE 2019]
Solution:-
import
java.util.Scanner;
public class BubbleSort
{
public
static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = 15;
int arr[] = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
//Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
[SELECTION SORT]
Program 4:Define a class to accept values in integer array of size 10. Sort them in an ascending order using selection sort technique. Display the sorted array.[ICSE 2024]
Solution:-
import java.util.Scanner;
public class SelectionSort
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int arr[] = new int[10];
System.out.println("Enter 10 integers: ");
for (int i = 0; i < 10; i++)
{
arr[i] = in.nextInt();
}
for (int i = 0; i < 9; i++)
{
int idx = i;
for (int j = i + 1; j < 10; j++)
{
if (arr[j] < arr[idx])
idx = j;
}
int t = arr[i];
arr[i] = arr[idx];
arr[idx] = t;
}
System.out.println("Sorted Array:");
for (int i = 0; i < 10; i++)
{
System.out.print(arr[i] + " ");
}
}
}
[Type of Numbers]
1. Armstrong Number
Note:- The Number whose sum of its cube of a digit is the number is an Arm Strong Number
153=1^3(Cube of 1)+5^3(Cube of 5)+3^3(Cube of 3)=153
Solution:-
public class ArmstrongNumber
{
public static void main(String args[])
{
int no=153;
int actno,p=0,sum=0;
while(no>0)
{
p=n%10;
sum=sum+p*p*p;
no=no/10;
}
if(sum==actno)
System.out.println("Its an Armstrong
Number ");
else
System.out.println("Its not an Armstrong
Number ");
}
}
2. Automorphic Number
Note:- A number is called an Automorphic number if and only if its square ends in the same digits as the number itself.
public class AutomrphicNo
{
public static void main(String args[])
{
int numCopy = num;
int sq = num * num;
int d = 0;
/*
* Count the number of
* digits in num
*/
while(num > 0)
{
d++;
num /= 10;
}
/*
* Extract the last d digits
* from square of num
*/
int ld = (int)(sq % Math.pow(10, d));
if (ld == numCopy)
System.out.println(numCopy +
" is Automorphic");
else
System.out.println(numCopy +
" is not Automorphic");
}
}
3. Magic Number
Note:- For example, 325 is a magic number because the sum of its digits (3+2+5) is 10,
public class MagicNo
{
public static void main(String args[])
{
int num=190;
int numCopy = num;
int sum;
int d = 0;
/*
* Count the number of
* sum of digits is 10
*/
while(num > 0)
{
d=num%10;
sum=sum+d;
num=num/10;
}
if (sum==10)
System.out.println(numCopy +
" is Magic No");
else
System.out.println(numCopy +
" is not Magic No");
}
}
4. Niven Number
Note:- A Niven number (also known as a Harshad number) is a positive integer that is divisible by the sum of its digits.
public class NivenNumber
{
public static void main(String args[])
{
int sumOfDigits =
0;
int temp = number;
while (temp >
0) {
sumOfDigits += temp % 10;
temp
/= 10;
}
if(x=number%sumOfDigits == 0)
System.out.println("It is a Niven Numnber!");
else
System.out.println("Not a Niven Numnber!");
}
}
5. Palinprime Number
Note:- PalinPrime is number is palindrome as well as prime also.
public class PalinprimeNumber
{
public static void main(String args[])
{
//Test for Palindrome:-
// Checks its reverse if its is equal to number
int no,rev=0;
int actno = number;
while (no > 0)
{
p=no%10;
rev =rev+p;;
no=no/10;
}
if(rev==actno)
System.out.println("It is a Palindrome Number!");
else
System.out.println("Not a Palindrome Numnber!");
}
//Test for Prime:-
// Checks its factors are 1 and number itself
int no;
int count=0;
for(int i=1'i<=no;i++)
{
if(no%i==0) count++;
}
if(count<=2)
System.out.println("Its a Prime Number!");
else
System.out.println("Its not a Prime Number!");
6. Perfect Number:
Note:- The Number whose sum of its factors are equal to number is a perfect number
6=3x2x1=3+2+1
public class PerfectNumber
{
public static void main(String args[])
{
int no=0;
int temp = number;
while (temp > 0) {
sumOfDigits += temp % 10;
temp /= 10;
}
if(x=number%sumOfDigits == 0)
System.out.println("It is a Perfect Numnber!");
else
System.out.println("Not a Perfect Numnber!");
}
}
7. Pronic Number
Note:-A pronic number is a number that is the product of two consecutive integers
public class PronicNumber
{
public static void main(String args[])
{
for (int i = 0;i <= (int)(Math.sqrt(x));i++)
{
// Checking
Pronic Number
// by
multiplying consecutive
// numbers
if (x == i * (i
+ 1))
System.out.println("It is a Pronic No");
else
System.out.println("It
is not a Pronic No");
}
}
8. Spy Number
Input : 1412
Output : Spy Number
Explanation :
sum = (1 + 4 + 1 + 2) = 8
product = (1 * 4 * 1 * 2) = 8
since, sum == product == 8
Input : 132
Output : Spy Number
Explanation :
sum = (1 + 3 + 2) = 6
product = (1 * 3 * 2) = 6
since, sum == product == 6
public class SpyNumber
{
public static void main(String args[])
{
// boolean function to
// check Spy number
int digit, sum = 0,
product = 1;
while (input > 0)
{
digit = input % 10;
// getting the
// sum of digits
sum += digit;
// getting the product
// of digits
product *= digit;
input = input / 10;
}
// Comparing the
// sum and product
if (sum == product)
System.out.println("It is a Spy Numnber!");
else
System.out.println("It is not a Spy Numnber!");
}
}
No comments:
Post a Comment