ICSE Important Programs

Solved Programs:

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.

  • input the distance in metres and convert it into miles

    • 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("Enter the distance in meters : ");
                  int meter = sc.nextInt();
                  
                  float mile = meter/1609.34f;
                  System.out.println(meter+" meters = "+mile+" miles");
              }
          }
  • Write a program to check a no whether it is a pallindroem or not … USING WHILE LOOP

    • Here is the program:

      import java.io.*;
      class Palindrome
      {
        public static void main()throws IOException
        {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          System.out.print("Enter a number : ");
          int n = Integer.parseInt(br.readLine());
      
          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");
        }
      }
      
  • 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

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

    • 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();
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      int m,n;
      int c=0;
      System.out.print("Enter the 1st number = ");
      m=Integer.parseInt(br.readLine());
      System.out.print("Enter the 2nd number = ");
      n=Integer.parseInt(br.readLine());
       
      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");
      }
      }
      
  • java program for
    (1+2)+(1+2+3)+(1+2+3+4)………………….for n terms

    • 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.

  • 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?

    • 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

  • Which are the most-likely programs for 2014 boards?
    any guess?
    if so, please tell
    thank you!

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

    • 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();
              }
          }
      }
      
  • Write a program on following pattern :-
    15 14 13 12 11
    10 9 8 7
    6 5 4
    3 2
    1

    • 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();
              }
          }
      }
      
  • 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

  • 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…

    • /**
      * 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
              {
                  BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
                  System.out.print("Enter any sentence: ");
                  String s=br.readLine();
      
                  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…

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

    • 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’};

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

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

    example                 *

    * * *

    * * * * *

    *  * *

    *

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

    • 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.

  • what are arrays

    how do they work

    • 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.

  • 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.

     

  • how do you define an object?

Your Turn To Talk

Your email address will not be published.