ICSE Important Programs

Solved Programs:

javaforschool

A teacher whose aim is to make Java a fun to learn experience for the school students by striving to build clear fundamentals of every concepts and helping students to develop their ability to reason and logically analyze any given programs.

• ashwin

How to do the following pattern
12345
2345
345
45
5

• javaforschool

Here you go:

class Pattern
{
public static void main(String args[])
{
for(int i=1; i<=5; i++)
{
for(int j=i; j<=5; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}

• Shantanu

Input any number and check if it is a Palindrome Number or not. A number that reads the same from both the ends (Hint: 121 = 121)

• javaforschool
import java.io.*;
class Palindrome
{
public static void main(String args[])throws IOException
{
System.out.print("Enter a number : ");
int rev=0,dig;
int copy=n;
while(n>0)
{
dig = n % 10;
rev = rev*10 + dig;
n = n/10;
}
if(rev==copy)
System.out.println("The number is a Palindrome");
else
System.out.println("The number is Not a Palindrome");
}
}

what does int in bracket mean
(int)

• javaforschool

It means we are converting the numerical result forcefully into an integer data type.

Example: (int)3.5 will give me 3

• sandip

input two numbers greater than zero and print which one of them is nearest to 21 without getting over the number 21

• javaforschool

Hello Sandip,

import java.io.*;
class Nearer
{
public static void main(String args[])throws IOException
{

System.out.print("Enter 1st number : ");
System.out.print("Enter 2nd number : ");

int d1 = Math.abs(21-a);
int d2 = Math.abs(21-b);

if(d1 > d2)
System.out.println(b+" is nearer to 21.");
else if(d2 > d1)
System.out.println(a+" is nearer to 21.");
else
System.out.println("Both "+a+" and "+b+" are at equal distance from 21.");
}
}
• prakash

WAP to display the following pattern ?
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

• javaforschool
class PatternStar
{
public static void main(String args[])
{
for(int i=1; i<=10; i++)
{
for(int j=10; j>=i; j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
• mohit

sir how do we find the twin prime numbers till the number entered by the user

• Jacob Samuel

how to make a pattern by accepting a word

• javaforschool

Example:

import java.io.*;
class PatternWord
{
public static void main(String args[])throws IOException
{
System.out.print("Enter any word : ");
int len = s.length();

for(int i=0; i<len; i++)
{
for(int j=0; j<=i; j++)
{
System.out.print(s.charAt(j));
}
System.out.println();
}
}
}
• sandip

input the distance in metres and convert it into miles

• javaforschool

Hello Sandip,
1 meter = (1/1609.34) miles

So, the code can be written as:

import java.util.Scanner;
class MeterToMile
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print(&quot;Enter the distance in meters : &quot;);
int meter = sc.nextInt();

float mile = meter/1609.34f;
System.out.println(meter+&quot; meters = &quot;+mile+&quot; miles&quot;);
}
}
• Saz

Write a program to check a no whether it is a pallindroem or not … USING WHILE LOOP

• Java For School

Here is the program:

import java.io.*;
class Palindrome
{
public static void main()throws IOException
{
System.out.print("Enter a number : ");

int rev = 0, a = n, d = 0;
while( a>0 )
{
d = a % 10;
rev = rev * 10 + d;
a = a / 10;
}

if( rev == n)
System.out.println("The number is a Palindrome");
else
System.out.println("The number is NOT a Palindrome");
}
}

• Dhritiman

wap to check whether a number is an ISBN number or not?

• Abhigyan

Sir, is there any way to find the no. of digits in a double value no.
for eg : Input=42.009
output=: No.of digits=5

• Shaunak

how to write a program to accept 2 numbers and check whether they are twin primes or not??

• Java For School

Shaunak here is the program:

/**
* The class TwinPrime inputs 2 numbers and checks whether they are twin primes or not
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class TwinPrime
{

/* Function isPrime( ) returns 'true' when the number 'x' is Prime and 'false' if it is not. */
boolean isPrime(int x)
{
int count=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
count++;
}
if(count==2)
return true;
else
return false;
}

public static void main(String args[]) throws IOException
{
TwinPrime ob = new TwinPrime();
int m,n;
int c=0;
System.out.print("Enter the 1st number = ");
System.out.print("Enter the 2nd number = ");

boolean a = ob.isPrime(m);
boolean b = ob.isPrime(m);
c = Math.abs(m-n);

if(b == true && b == true && c == 2)
System.out.println("The numbers are Twin Primes");
else
System.out.println("The Numbers are Not Twin Primes");
}
}

• Abhishek

java program for
(1+2)+(1+2+3)+(1+2+3+4)………………….for n terms

• Java For School

Hello Abhishek,

Below is the logic which you need to implement after having taken ‘n’ as input from the user:

int x = 0, sum = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= (i+1); j++)
{
x = x + j;
}
sum = sum + x;
}
System.out.println("Result = "+sum);

We hope this helps.

• Daz

int r=(int)t%10;
s=s+(d*r);
d–;
t=t/10;
explain the working of this statement. if we have to find the sum of the digits or anything such that this kind of logic is often found in the program. how do they work?

• Java For School

The logic used for any operations involving digits is:

int d;
while(n > 0)
{
d = n%10; // This extracts the last digits of the number
.
/* Do the operation you have to do with the digit 'd' here */
.
n = n/10; // Removing that digit from the number
}

For finding sum of every digit the code will be:

int d, sum = 0;
while(n > 0)
{
d = n%10; // This extracts the last digits of the number
sum = sum + d; // Adding the digit to the variable sum
n = n/10; // Removing that digit from the number
}

Suppose the number (n) is 945.

Step 1:
so, 645 > 0 is true
therefore, d = 645 % 10 = 5
so sum = 0 + 5 = 5
and n = 645/10 = 64

Step 2:
so, 64 > 0 is true
therefore, d = 64 % 10 = 4
so sum = 5 + 4 = 9
and n = 64/10 = 6

Step 3:
so, 6 > 0 is true
therefore, d = 6 % 10 = 6
so sum = 9 + 6 = 15
and n = 6/10 = 0

Step 4:
so, 0 > 0 is false
so the loop will end and we have the sum stored in the variable sum

• sukruth gowda

Which are the most-likely programs for 2014 boards?
any guess?
thank you!

• Sanjana

Write a program for foll pattern
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

• Java For School

Here is the program:

import java.io.*;
class Pattern
{
public static void main(String args[])
{
int a = 1;
for(int i = 5; i>=1; i--)
{
for(int j = 1; j<=i; j++)
{
System.out.print(a+"\t");
a++;
}
System.out.println();
}
}
}

• Sanjana

Write a program on following pattern :-
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1

• Java For School

Here is the program:

import java.io.*;
class Pattern
{
public static void main(String args[])
{
int a = 15;
for(int i = 5; i>=1; i--)
{
for(int j = 1; j<=i; j++)
{
System.out.print(a+"\t");
a--;
}
System.out.println();
}
}
}

• Sanjan

Write a program to accept a number and display the frequency of each digit present in the number
SAMPLE INPUT – 341124
SAMPLE OUTPUT – The frequency of 1 = 2
The frequency of 4 = 2

• aishwary

how we can make a program in which we  have to accept a sentence from a user and print those words which starts with a vowel…

• Java For School
/**
* The class Vowel_Word inputs a sentence and prints words which begin with a vowel
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Vowel_Word
{
public static void main(String args[])throws IOException
{
System.out.print("Enter any sentence: ");

s = s+" ";
int l=s.length();
int pos=0;
char ch1, ch2;
String w;

for(int i=0; i<l; i++)
{
ch1 = s.charAt(i);
if(ch1 == ' ')
{
w = s.substring(pos,i); // extracting words one by one
ch2 = w.charAt(0);
if(ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U' ||
ch2=='a' || ch2=='e' || ch2=='i' || ch2=='o' || ch2=='u')
{
System.out.println(w);
}
pos = i+1;
}
}
}
}

thankss…

• nayana

can we convert a string to an array by to char array()and perform operations like append?as we perform on buffer reader.

• Java For School

Yes, you can use the “toCharArray()” function of the String class which converts a String into a character array.
So, if the string is, String s=”JAVA FOR SCHOOL”, then the statement,
char ch [] = s.toCharArray(); will make a character array like
ch[]={‘J’, ‘A’, ‘V’, ‘A’, ‘ ‘, ‘F’, ‘O’, ‘R’, ‘ ‘, ‘S’, ‘C’, ‘H’, ‘O’, ‘O’, ‘L’};

• Manan Jhaveri

Is String Buffer there in portion?

Are functions like .append() and .charToArray() there in the ICSE syllabus?

• Java For School

Difference between a String and StringBuffer object may come. But the detailed concept of StringBuffer class and its functions are not there in the ICSE syllabus.

• akanksha

Is it necessary to use BufferedReader in the programs? Can we use scanner class instead?

• Java For School

No it is not necessary to always use BufferedReader class. You can also use Scanner class for taking inputs.

can complex patterns such as parallelogram and  rhombus using for loop come?

example                 *

* * *

* * * * *

*  * *

*

• Java For School

No kaushik, rhombus and parallelogram won’t come in ICSE.
The example you gave might come. It is quite easy.

• nevin

how to write a program to print the longest word in a sentence

• Java For School

which sorting is in icse course.and how to understand string programs..

• Java For School

Hello Ayush,
You have Selection sorting and Bubble Sorting of one dimensional array in the ICSE course.

Below is an exerpt from the ICSE Scope of Syllabus:

Arrays

• Arrays –storing, retrieving and arranging data
• Arrays and their uses,
• sorting algorithms – selection sort and bubble sort;
• Search algorithms – linear search and binary search
• Example of a composite type.
• Array creation.
• Sorting and searching algorithms should be discussed (single dimensional array only).

Regarding String programs,

You have to think a String to be just like an array of characters. Like you use every element in the array by writing its index, similarly, in order to access the characters of a String, you need to extract the characters through their index using charAt() function.

Think of it this way, and if your concept of array is strong, then the working of Strings will be really easy for you.

• nooraien

what are arrays

how do they work

• Java For School

An array is a container object that holds a fixed number of values of a single data-type. The length of an array is established when the array is created. After creation, its length is fixed.

So, if you have to store names of 50 students, then you would require 50 different variables. It is quite inconvenient to remember the names of so many variables. Instead of doing this, we can create an array having 50 elements for storing 50 names. You only have to remain the name of only a single array.

An array of 10 elements.

Illustration of an array as 10 boxes numbered 0 through 9; an index of 0 indicates the first element in the array.

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.
So if the name of the array is arr[], then to access the 9th element, we have to write arr[8].

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

• anonymous

A program code is divided into various entities which are known as objects of a class.objects have the data and functions which are also known as characterstics and behaviour .object is a set of related data which can share the information and can interact with each other.

• anonymous

how do you define an object?