Tuesday, 28 February 2017

ICSE 2010 Programming Solution

ICSE 2010 Section II Solution

Question 4

Write a program to perform binary search on a list of integers given below, to search for an element input by the user. If it is found then display element along with its position, otherwise display the message "Search element not found".

                          5,7,9,11,15 20,30,45,89,97
Solution :

import java.util.Scanner;
class Question4
{
    Scanner S = new Scanner(System.in);
    public void main()
    {
        int arr[] = {5,7,9,11,15,20,30,45,89,97};
        System.out.println("Enter the integer to be searched ");
        int x= S.nextInt();
        int start = 0,end=(arr.length-1),mid;
        while(start<=end)
        {
            mid = (start+end)/2;
            if(arr[mid]<x)
            start = mid+1;
            if(arr[mid]>x)
            end = mid - 1;
            else if(arr[mid]==x)
            {
                System.out.println("Search element "+x+" found at "+mid+" position");
                System.exit(0);
            }
        }
        System.out.println("Search element not found");
    }
}




Question 5 

Define a class student described as below:

Data members/instance variables :-


name, age, ml, m2, m3 (marks in 3 subjects), maximum, average

Member methods: 

(i) A parameterized constructor to initialize the data members

(ii) To accept the details of a student

(iii) To compute the average and the maximum out of three marks

(iv) To display the name, age, marks in three subjects. maximum and                  average.

Write a main method to create an object of a class and call the above member methods.

Solution :

import java.util.Scanner;
class student
{
    Scanner S = new Scanner(System.in);
    String name;int age;double m1,m2,m3,max,avg;
    student(String nam,int ag,double a,double b,double c)
    {
        name = nam;
        age = ag;
        a=m1;
        b=m2;
        c=m3;
    }
    void input()
    {
        System.out.println("Enter the name of student and her/his age");
        name = S.next();
        age = S.nextInt();
        System.out.println("Enter the marks of student in three subjects :-");
        m1=S.nextDouble();
        m2=S.nextDouble();
        m3=S.nextDouble();
    }
    void compute()
    {
        max = Math.max(m1,Math.max(m2,m3));
        avg = (m1+m2+m3)/3;
    }
    void display()
    {
        System.out.println("Student Name            :"+name);
        System.out.println("Student Age             :"+age);
        System.out.println("Student's m1            :"+m1);
        System.out.println("Student's m2            :"+m2);
        System.out.println("Student's m3            :"+m3);
        System.out.println("Student's maximum marks :"+max);
        System.out.println("Student's average matks :"+avg);
    }
    void main()
    {
        System.out.println("Enter the name of student and her/his age");
        name = S.next();
        age = S.nextInt();
        System.out.println("Enter the marks of student in three subjects :-");
        m1=S.nextDouble();
        m2=S.nextDouble();
        m3=S.nextDouble();
       
        student ob = new student(name,age,m1,m2,m3);
        ob.input();
        ob.compute();
        ob.display();
    }
}


Question 6 

Shasha Travels Pvt. Ltd. gives the following discount to its customers:

Discount                 Ticket amount

18%                       Above Rs. 70000

16%                       Rs. 55001 to Rs. 70000

12%                       Rs. 35001 to Rs. 55000

10%                       Rs. 25001 to Rs. 35000

2%                         Less than Rs. 25001

Write a program to input the name and ticket amount for the customer and calculate the discount amount and net amount to be paid. Display the output in the following format for each customer:

SI No.       Name       Ticket charges         Discount          Net amount
  1.               -                 -                            -                         -
(Assume that there are 15 customers, first customer is given the serial no (Sl.No.) l, next customer 2
and so on) 

Solution :

import java.util.Scanner;
class Quection6
{
    Scanner S = new Scanner(System.in);
    String Name[]= new String[15];
    int ticket[] = new int[15];
    double discount[] = new double[15];
    double netamt[] = new double[15];
    public void main()
    {
        System.out.println("Enter the Names and ticket Number of :-");
        for(int i = 0;i<15;i++)
        {
            System.out.println("Name of "+(i+1)+"th Customer");
            Name[i] = S.next();
           
            System.out.println("Ticket Cost :");
            ticket[i] = S.nextInt();
            if(ticket[i]<25001)
            discount[i] = 1/50*ticket[i];
            if((ticket[i]>=25001)&&(ticket[i]<35000))
            discount[i] = 1/10*ticket[i];
            if((ticket[i]>=35000)&&(ticket[i]<55000))
            discount[i] = 12/100*ticket[i];
            if((ticket[i]>=55000)&&(ticket[i]<=70000))
            discount[i] = 16/100*ticket[i];
            else if(ticket[i]>70000)
            discount[i] = 18/100*ticket[i];
            netamt[i] = ticket[i]-discount[i];
        }
        System.out.println("Sl.No.\tName\tTicket Charges\t Discount\t Net Ammount");
        for(int i =0;i<15;i++)
        System.out.println("   "+(i+1)+"\t  "+Name[i]+"\t       "+ticket[i]+"\t    "+discount[i]+"\t     "+netamt[i]);
    }
}


        

Question 7 

Write a menu driven program to accept a number and check and display whether it is a prime number or not OR an Automorphic number or not. (Use switch-case statement)

(a) Prime number A number is said to be a prime number if it is divisible only by l and itself and not by any other number

Example 3, 5, 7, 11, 13 etc.

(b) Automorphic number An automorphic number is the number which is contained in the last digit(s) of its square.

Example: 25 is an automorphic number as its square is 625 and 25 is present as the last two digits. 

Solution :


import java.util.Scanner;
class Question7
{
    Scanner S = new Scanner(System.in);
    int choice, number;
    public boolean isPrime(int x)
    {
        for(int i = 2;i<x;i++)
        if((x%i)==0)
        return false;
        return true;
    }
    public boolean isAutomorphic(int x)
    {
        int sq = x*x;
        String num = Integer.toString(x);
        String square = Integer.toString(sq);
        if(square.endsWith(num))
        return true;
        else
        return false;
    }
    public void main()
    {
        System.out.println("Enter the number");
        number = S.nextInt();
        System.out.println("Press 1 to check number for Prime");
        System.out.println("Press 2 to check number for Automorphic");
        System.out.println("Press any other key to exit");
        System.out.println("Enter your choice :-");
        choice = S.nextInt();
        switch(choice)
        {
            case 1: if(isPrime(number))
                    System.out.println("It is a prime number");
                    else
                    System.out.println("It is not a prime Number");
                    break;
            case 2: if(isAutomorphic(number))
                    System.out.println("It is an Automorphic number");
                    else
                    System.out.println("It is not an Automorohic Number");
                    break;
            default: System.exit(0);
                    break;
        }
    }
}



Question 8 

Write a program to store 6 element in an array P, and 4 elements in an array Q and produce a third array R, containing all elements of array P and Q.

Display the resultant array.

Solution :

import java.util.Scanner;
class Question8
{
    Scanner S = new Scanner(System.in);
    int P[] = new int[6];
    int Q[] = new int[4];
    int R[] = new int[10];
    void main()
    {
        System.out.println("Enter the elements of Array 1");
        for(int i =0;i<6;i++)
        {
            P[i] = S.nextInt();
            R[i] = P[i];
        }
        System.out.println("Enter the elements of Array 2");
        for(int i =0;i<4;i++)
        {
            Q[i] = S.nextInt();
            R[i+6]= Q[i];
        }
       
        System.out.println("Resultant Array :-");
        for(int i =0;i<10;i++)
        {
           
            System.out.println(R[i]);
        }
    }
}


   
Question 9 

Write a program to input a string in uppercase and print the frequency of each character.

Solution :
import java.util.Scanner;
class Quection9
{
    Scanner S = new Scanner(System.in);
    public void main()
    {
        System.out.println("Enter the String in UPPERCASE");
        String str = S.nextLine();
        System.out.println("CHARACTERS \t fREQUENCY");
        for(char ch = 'A';ch<='Z';ch++)
        {
            int freq = 0;
            for(int i = 0;i<str.length();i++)
            {
                if(ch==str.charAt(i))
                freq++;
            }
            if(freq>0)
            {
                System.out.println(ch+"          \t    "+freq);
            }
        }
    }
}