ICSE - Java Programs
ICSE - Java Programs
Write a program to accept a number and check and display whether it is a spy number or not. (A
number is spy if the sum of its digits equals the product of its digits.)
Example: consider the number 1124,
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1 * 1 * 2 * 4 = 8
import java.util.Scanner;
class SpyNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number:");
int n = sc.nextInt();
int m = n;
int sum=0, prod=1;
while(n>0){
int r = n % 10;
sum = sum + r;
prod = prod * r;
n = n / 10;
}
if(sum == prod){
System.out.println(m+" is a spy number");
}
else{
System.out.println(m+" is not a spy number");
}
}
}
Program 2
Write a program to input forty words in an array. Arrange these words in descending order of
alphabets, using selection sort technique. Print the sorted array.
import java.util.Scanner;
System.out.println("Sorted Names");
for (int i = 0; i < n; i++) {
System.out.println(names[i]);
}
}
}
Program 3
Write a program to input integer elements into an array of size 20 and perform the following
operations:
(i) Display largest number from the array.
(ii) Display smallest number from the array.
(iii) Display sum of all the elements of the array.
import java.util.Scanner;
System.out.println("Enter 20 integers:");
for(int i=0; i<n; i++){
a[i] = sc.nextInt();
}
System.out.println("Largest = "+max);
System.out.println("Smallest = "+min);
System.out.println("Sum = "+sum);
}
}
Program 4
Design a class to overload a function check( ) as follows:
(i) void check (String str , char ch ) - to find and print the frequency of a character in a
string.
Example : Input: str = "success" ch = 's'
Output: number of s present is =3
(ii) void check(String s1) - to display only vowels from string s1, after converting it to
lower case.
Example : Input: s1 ="computer"
Output : o u e
class Overload{
public void check(String str, char ch){
int n = 0;
for(int i=0; i<str.length(); i++){
if(str.charAt(i) == ch){
n++;
}
}
System.out.println("Number of "+ch+
" present is = "+n);
}
public void check(String s1){
s1 = s1.toLowerCase();
for(int i=0; i<s1.length(); i++){
char c = s1.charAt(i);
if(c=='a' || c=='e' || c=='i' ||
c=='o' || c=='u'){
System.out.print(c+" ");
}
}
}
import java.util.Scanner;
class SwitchDemo{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
switch(ch){
case 1:
double sum=0, x=2;
for(int i=1; i<=3; i++){
sum = sum + Math.pow(-1, i+1)*Math.pow(x, i);
}
System.out.println("Sum = "+sum);
break;
case 2:
for(int i=1; i<=5; i++){
for(int j=1; j<=i; j++){
System.out.print("1");
}
System.out.print(" ");
}
break;
default:
System.out.println("Invalid choice:"+ch);
}
}
}
Program 6
Define a class ElectricBill with the following specifications:
class : ElectricBill
Instance variables / data member:
String n – to store the name of the customer
int units – to store the number of units consumed
double bill – to store the amount to be paid
Member methods:
void accept( ) – to accept the name of the customer and number of units consumed
void calculate( ) – to calculate the bill as per the following tariff:
Number of units Rate per unit
First 100 units Rs.2.00
Next 200 units Rs.3.00
Above 300 units Rs.5.00
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
void print ( ) - To print the details as follows:
Name of the customer: ………………………
Number of units consumed: ………………………
Bill amount: ………………………
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;
class ElectricBill{
private String n;
private int units;
private double bill;