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




Sunday, 19 February 2017

Solution to Checking Nos.

Solution to check numbers asked in ICSE

Program containing all the booleans of different Numbers :-

This program is the solution to my previous blog in which gives information about different types of numbers to be checked while solving ICSE-practical based questions.

Paste this program in a class editor source code of a Blue J File :-
___________________________________________________________________________________

import java.util.Scanner;
class CheckNOS
{
    Scanner S = new Scanner(System.in);
    int digitFactorial(int x)
    {
        int sum =0;
        while(x>0)
        {
            int d = x%10;
            int su = 1;
            for(int i = 1;i<=d;i++)
            su = su*i;
            sum = sum+su;
            x = x/10;
        }
        return sum;
    }
    public void main()
    {
        CheckNOS on = new CheckNOS();
        System.out.println("Enter a number");
        int no = S.nextInt();
        System.out.println("              Press 1 to check for Harshad/niven No.");
        System.out.println("              Press 2 to check for Disarum No.");
        System.out.println("              Press 3 to check for Palindrome No.");
        System.out.println("              Press 4 to check for Prime No.");
        System.out.println("              Press 5 to check for Special No.");
        System.out.println("              Press 6 to check for Automorphic No.");
        System.out.println("              Press 7 to check wether no. belongs to Fibonacci Series or not.");
        System.out.println("              Press 8 to check for Armstrong No.");
        System.out.println("              Press 9 to check for Prime Palindrome No.");
        System.out.println("              Press 10 to check for Duck No.");
        System.out.println("              Press 11 to check for Smith No.");
        System.out.println("              Press 12 to check for Pronic No.");
        System.out.println("              Press 13 to check for Emirp No.");
        System.out.println("              Press 14 to check for Amicable No.");
        System.out.println("              Press 15 to check for ISBN No.");
        System.out.println("              Press 16 to check for Composite No.");
        System.out.println("              Press 17 to check for Circular Prime.");
        System.out.println("              Press 18 to check for Happy Number");
        System.out.println("              Press 19 to check for Kaprekar Number");
        System.out.println("              Press 20 to exit.");
        System.out.println("Enter Your Choice :-");
        int ch = S.nextInt();
        switch(ch)
        {
            case 1 : if(on.isHarshad(no))
            System.out.println("It is a Harshad No.");
            else
            System.out.println("Not a Harshad No.");
            on.main();
            break;
            case 2 : if(on.isDisarum(no))
            System.out.println("It is a Disarum No.");
            else
            System.out.println("Not a Disarum No.");
            on.main();
            break;
            case 3 : if(on.isPalindrome(no))
            System.out.println("It is a Palindrome No.");
            else
            System.out.println("Not a Palindrome No.");
            on.main();
            break;
            case 4 : if(on.isPrime(no))
            System.out.println("It is a Prime No.");
            else
            System.out.println("Not a Prime No.");
            on.main();
            break;
            case 5 : if(on.isSpecial(no))
            System.out.println("It is a Special No.");
            else
            System.out.println("Not a Special No.");
            on.main();
            break;
            case 6 : if(on.isAutomorphic(no))
            System.out.println("It is a Automorphic No.");
            else
            System.out.println("Not a Automorphic No.");
            on.main();
            break;
            case 7 : if(on.isFibonacci(no))
            System.out.println("It is a Fibonacci No.");
            else
            System.out.println("Not a Fibonacci No.");
            on.main();
            break;
            case 8 : if(on.isArmstrong(no))
            System.out.println("It is a Armstrong No.");
            else
            System.out.println("Not a Armstrong No.");
            on.main();
            break;
            case 9 : if(on.isPrimePalindrome(no))
            System.out.println("It is a Prime Palindrome No.");
            else
            System.out.println("Not a Prime Palindrome No.");
            on.main();
            break;
            case 10 : if(on.isDuck(no))
            System.out.println("It is a Duck No.");
            else
            System.out.println("Not a Duck No.");
            on.main();
            break;
            case 11 : if(on.isSmith(no))
            System.out.println("It is a Smith No.");
            else
            System.out.println("Not a Smith No.");
            on.main();
            break;
            case 12 : if(on.isPronic(no))
            System.out.println("It is a Pronic No.");
            else
            System.out.println("Not a Pronic No.");
            on.main();
            break;
            case 13 : if(on.isEmirp(no))
            System.out.println("It is a Emirp No.");
            else
            System.out.println("Not a Emirp No.");
            on.main();
            break;
            case 14: System.out.println("Enter the two numbers ");
            System.out.println("Enter the first number ");
            int a = S.nextInt();
            System.out.println("Enter the second number ");
            int b = S.nextInt();
            if(isAmicable(a,b))
            System.out.println("They are Amicable No.");
            else
            System.out.println("They are not Amicable No.");
            break;
            case 15 : if(isISBN(no))
            System.out.println("It is a legal ISBN No.");
            else
            System.out.println("It is not a legal ISBN No.");
            break;
            case 16 : if(isPrime(no))
            System.out.println("Not a Composite Number");
            else 
            System.out.println("It is a Composite Number");
            break;
            case 17 : if(isCircularPrime(no))
            System.out.println("It is a Circular Prime Number");
            else 
            System.out.println("It not a Circular Prime Number");
            on.main();
            break;
            case 18 : if(isHappy(no))
            System.out.println("It is a Happy Number");
            else 
            System.out.println("It not a Happy Number");
            on.main();
            break;
            case 19 : if(isKaprekar(no))
            System.out.println("It is a Happy Number");
            else 
            System.out.println("It not a Happy Number");
            on.main();
            break;
            case 20 : System.exit(0);
            break;
            default : System.out.println("Wrong choice Entered ! Try again ");
            on.main();
            break;
        }
    }
    public boolean isISBN(long x)
    {
        long c = x;
        long sum = 0;
        for(int i = 1;i<=10;i++)
        {
            long d = c%10;
            c=c/10;
            sum = sum + (d*i);
        }
        if((sum%11)==0)
        return true;
        else 
        return false;
    }
    public boolean isHarshad(int xi)
    {
        int sum = 0,x=xi,d;
        while(x>0)
        {
             d = x%10;
            sum = sum+d;
            x=x/10;
            
        }
        if(xi%sum==0)
        return true;
        else 
        return false;
    }
    public boolean isDisarum(int x)
    {
        double sum = 0;
        int x1 = x;
        int power = 1;
        while(x>0)
        {
            int d= x%10;
            sum = sum + (Math.pow(d,power));
            power++;
            x = x/10;
        }
        if(x1==(int)sum)
        return true;
        else
        return false;
    }
    public boolean isPalindrome(int x)
    {
        int sum = 0;
        int y = x;
        while(y>0)
        {
            sum = sum*10;
            sum = sum+(y%10);
            y= y/10;
        }
        if(sum==x)
        return true;
        else
        return false;
    }
    public boolean isPrime(int x)
    {
        int killme = 0;
        for(int i = 2;i<x;i++)
        {
            if(x%i==0)
            killme++;
            else
            continue;
        }
        if(killme>0)
        return false;
        else
        return true;
    }
    public boolean isSpecial(int x)
    {
        int prod = 1;
        int r = x;
        int sum = 0;
        while(x!=0)
        {
            int d = x%10;
            prod = prod*d;
            sum = sum +d;
            x=x/10;
        }
        if((prod+sum)==r)
        return true;
        else 
        return false;
    }
    public boolean isAutomorphic(int x)
    {
        int u = x;
        int sq = x*x;
        String square = Integer.toString(sq);
        String u1 = Integer.toString(u);
        int len = square.length();
        int length = u1.length();
        if((square.charAt(len-1)==u1.charAt(length-1))&&(square.charAt(len-2)==u1.charAt(length-2)))
        return true;
        else
        return false;
    }
    public boolean isSmith(int x)
    {
        int y = x;
        CheckNOS ob = new CheckNOS();
        int sum =0;
        int sum1 =0;
        while(y>1)
        {
            for(int i= 2;i<=y;i++)
            {
                int s2 = 0;
                if(((y%i)==0)&&(ob.isPrime(i)))
                {
                    String st = Integer.toString(i);
                    int len = st.length();
                    if(len!=1)
                    {
                        int k = i;
                        while(k>0)
                        {
                            s2 = s2 + (k%10);
                            k = k/10;
                        }
                    }
                    else
                    s2 = s2+i;
                    y=y/i;
                }
                sum = sum + s2;
            }
        }
        y=x;
        while(y>0)
        {
            sum1 = sum1 + (y%10);
            y=y/10;
        }
        if(sum==sum1)
        return true;
        else
        return false;
    }
    public boolean isArmstrong(int x)
    {
        int y = x;
        int sum = 0;
        while(y>0)
        {
            int d = y%10;
            y=y/10;
            sum = sum +(d*d*d);
        }
        if(sum==x)
        return true;
        else
        return false;
    }
    public boolean isPrimePalindrome(int x)
    {
        int y = x;
        if(isPrime(y))
        {
            String str = Integer.toString(y);
            String ns = "";
            for(int i = 0;i<str.length();i++)
             ns = str.charAt(i)+ns;
            if(ns.equalsIgnoreCase(str))
            return true;
        }
        return false;
    }

    Scanner S = new Scanner(System.in);
    public boolean isKaprekar(int x)
    {
        int x2=x*x;
        int nod= numberOfDigits(x2);
        int n1,n2;
        n1=x2/((int)Math.pow(10,nod/2));
        n2=x2%((int)Math.pow(10,nod/2));
        if((n1+n2)==x)
        {
            System.out.println(x+"x"+x+" = "+(x*x)+", right hand piece of "+(x*x)+" = "+n2+" and left hand piece of "+(x*x)+" = "+n1+"\nSum = "+n1+" + "+n2+" = "+(n1+n2)+", i.e. equal to the number");
            return true;
        }
        else
        return false;
    }
    public int numberOfDigits(int x)
    {
        int y=x;
        int result=0;
        while(y>0)
        {
            y=y/10;
            result++;
        }
        return result;
    }
    public boolean isDuck(int x)
    {
        String str = Integer.toString(x);
        String ns = "";
        if(str.charAt(0)=='0')
        return false;
        else
        {
            for(int i =1;i<str.length();i++)
            {
                if(str.charAt(i)=='0')
                return true;
            }
        }
        return false;
    }
    public boolean isPronic(int x)
    {
        for(int i = 0; i<x;i++)
        {
            if((i*(i+1))==x)
            return true;
        }
        return false;
    }
    public boolean isFibonacci(int x)
    {
        int n1 = 1;
        int n2 = 1;
        while(n2<=x)
        {
            n1 = n2;
            n2 = n1+n2;
            if(n2==x)
            return true;
        }
        return false;
    }
    public boolean isEmirp(int x)
    {
        if(isPrime(x))
        {
            int y=x;
            int rev = 0;
            while(y>0)
            {
                 int d=y%10;
                 rev=rev*10+d;
                 y=y/10;
            }
            if(isPrime(rev))
            return true;
        }
        return false;
    }
    boolean isAmicable(int a,int b)
    {
        int s=0,i;
        for(i=1;i<a;i++)
        {
            if(a%i==0)
            {
                s=s+i;
            }
        }
        if(s==b)
        {
            s=0;
            for(i=1;i<b;i++)
            {
                if(b%i==0)
                {
                    s=s+i;
                }
            }
            if(s==a)
            return true;
            else
            return false;
        }
        return false;
    }
    public int no_of_digits(int x)
    {
        int y = x;
        int nod=0;
        while(y>0)
        {
            y=y/10;
            nod++;
        }
        return nod;
    }

    public boolean isCircularPrime(int x)
    {
        int nod = no_of_digits(x);
        int y = x;
        while(isPrime(y)==true)
        {
            int c = y%10;
            y = y/10;
            y=(c*(power(10,(nod-1))))+y;
            if(x==y)
            return true;
        }
        return false;
    }
    public int power(int x, int y)
    {
        int result = 1;
        while(y>0)
        {
            result = result*x;
            y--;
        }
        return result;
    }
    public boolean isHappy(int x)
    {
        int y = x;
        int sum = sumofdigitsquare(y);
        int u=0;
        while(sum>9)
        sum = sumofdigitsquare(sum);
        if(sum==1)
        return true;
        else
        return false;
    }
    public int sumofdigitsquare(int x)
    {
        int sum = 0;
        while(x>0)
        {
            sum = sum +((x%10)*(x%10));
            x=x/10;
        }
        return sum;
    }
}





------x-----------x-----------x-----------x-----------x-----------x-----------x-----------x-----------x-----------x----------x-----------x-----------x-----------x-----------x--
Note : If any one of you guys want me to upload a video on how the logic of checking different numbers works, leave a comment below and I would be uploading that shortly...