Showing posts with label 2005. Show all posts
Showing posts with label 2005. Show all posts

Wednesday, 11 March 2015

[JAVA] [ICSE 2005 Answer 9] Searching An Array By Linear Search Method

Question:
ICSE 2005 Question 9
Write a program to initialize an array of 5 names and initialize another array with their respective telephone numbers. Search for a name input by the users, in the list. If found, display "Search successful" and print the name along with the telephone number, otherwise display "Search unsuccessful. Name not listed."

Source Code:
import java.util.*;
public class ICSE2005_9
{
    public static void main()throws Exception
    {
        String s[]={"Rohan", "Mohon", "Sohan", "Ram", "Shyam"};
        String tel[]={"9852145236", "5784256321", "85632145687", "9889632145", "7896412356"};
        Scanner read=new Scanner(System.in);
        System.out.print("Enter the name to search: ");
        String check=read.next();
        int n=-1;
        for(int i=0; i<5; i++)
            if(check.equals(s[i]))
                n=i;
        if(n<0)
            System.out.println("Search unsuccessful. Not not listed.");
        else
            System.out.println("Search successful.\nName: "+s[n]+"\nTelephone Number: "+tel[n]);
    }
}


Result 1:
OUTPUT:
Enter the name to search: Rohan
Search successful.
Name: Rohan
Telephone Number: 9852145236
Result 2:
OUTPUT:
Enter the name to search: Tanu
Search unsuccessful. Not not listed.

[JAVA] [ICSE 2005 Answer 8] Conditional Sum Finding

Question:
ICSE 2005 Question 8
Write a program to print the sum of negative numbers, sum of positive even numbers and sum of positive odd numbers from a list of numbers(N) entered by the user. The list terminates when the user enters 0.

Source Code:
import java.util.*;
public class ICSE2005_8
{
    public static void main()throws Exception
    {
        Scanner read=new Scanner(System.in);
        int a=1;
        int sumn=0, sumep=0, sumop=0;
        while(a!=0)
        {
            System.out.print("Enter your number: ");
            int n=read.nextInt();
            if(n==0)
                break;
            else if(n<0)
                sumn+=n;
            else if(n%2==0)
                sumep+=n;
            else
                sumop+=n;
        }
        System.out.println("The sum of all negative numbers is: "+sumn);
        System.out.println("The sum of all positive even numbers is: "+sumep);
        System.out.println("The sum of all positive odd numbers is: "+sumop);
    }
}


Result:
OUTPUT:
Enter your number: -52
Enter your number: 68
Enter your number: 52
Enter your number: 51
Enter your number: 45
Enter your number: -101
Enter your number: 0
The sum of all negative numbers is: -153
The sum of all positive even numbers is: 120
The sum of all positive odd numbers is: 96

[JAVA] [ICSE 2005 Answer 7] Sorting By Bubble Sort Method

Question:
ICSE 2005 Question 7
Write a program to bubble sort the following set of values in ascending order:-
5,3,8,4,9,2,1,12,98,16
output:
1
2
3
4
5
8
9
12
16
98

Source Code:
public class ICSE2005_7
{
    public static void main() throws Exception
    {
        int a[]={5,3,8,4,9,2,1,12,98,16};
        int l=a.length;
        for(int i=0; i<l; i++)
            for(int j=0; j<(l-i-1); j++)
                if(a[j]>a[j+1])
                {
                    int c=a[j];
                    a[j]=a[j+1];
                    a[j+1]=c;
                }
        System.out.println("The sorted array is:");
        for(int i=0; i<l; i++)
            System.out.println(a[i]);
    }
}


Result:
OUTPUT:
The sorted array is:
1
2
3
4
5
8
9
12
16
98

[JAVA] [ICSE 2005 Answer 6] Calculating Area By Function Overloading

Question:
ICSE 2005 Question 6
Write a program using a function called area() to compute the area of a:
(i)      Circle(II * r)where II = 3.14
(ii)     Square(side*side)
(iii)    Rectange(length * breadth)
Display the menu to output the area as per user's choice

Source Code:
import java.util.*;
public class ICSE2005_6
{
    public static void main(String args[])throws Exception
    {
        Scanner read=new Scanner(System.in);
        System.out.print("What do you want to find?\nEnter:\n1 for area of circle\n2 for area of square\n3 for area of rectange\nEnter your choice:");
        int choice=read.nextInt();
        double a;
        switch(choice)
        {
            case 1:
            System.out.print("Enter the length of radius of the circle:");
            double r=read.nextDouble();
            a=areac(r);
            System.out.println("Area of the circle is "+a+".");
            break;
            case 2:
            System.out.print("Enter the length of side of the square:");
            double s=read.nextDouble();
            a=areas(s);
            System.out.println("Area of the square is "+a+".");
            break;
            case 3:
            System.out.print("Enter the length of first side of the rectange:");
            double l=read.nextDouble();
            System.out.print("Enter the length of second side of the rectange:");
            double b=read.nextDouble();
            a=arear(l, b);
            System.out.println("Area of the rectangle is "+a+".");
            break;
            default:
            System.out.println("Invalid option entered!:(");
        }
    }

    private static double areac(double r)
    {
        return ((3.14)*r*r);
    }

    private static double areas(double s)
    {
        return (s*s);
    }

    private static double arear(double l, double b)
    {
        return (l*b);
    }
}


Result 1:
OUTPUT:
What do you want to find?
Enter:
1 for area of circle
2 for area of square
3 for area of rectange
Enter your choice:1
Enter the length of radius of the circle:4.67
Area of the circle is 68.479946.
Result 2:
OUTPUT:
What do you want to find?
Enter:
1 for area of circle
2 for area of square
3 for area of rectange
Enter your choice:2
Enter the length of side of the square:98
Area of the square is 9604.0.
Result 3:
OUTPUT:
What do you want to find?
Enter:
1 for area of circle
2 for area of square
3 for area of rectange
Enter your choice:3
Enter the length of first side of the rectange:5.4
Enter the length of second side of the rectange:6.87
Area of the rectangle is 37.098000000000006.
Result 4:
OUTPUT:
What do you want to find?
Enter:
1 for area of circle
2 for area of square
3 for area of rectange
Enter your choice:4
Invalid option entered!:(

[JAVA] [ICSE 2005 Answer 5] Calculating Number Of Vowels & Reverse String

Question:
ICSE 2005 Question 5
Write a program to input any given string to calculate the total number of characters and vowels present in the string and also reverse the string.
Example:
INPUT
Enter string : SNOWY
OUTPUT
Total number of characters :  05
Name of vowels             :  01
Reverse string             :  YWONS

Source Code:
import java.util.*;
public class ICSE2005_5
{
    public static void main(String args[])
    {
        Scanner read=new Scanner(System.in);
        System.out.print("Enter string : ");
        String n=read.next();
        int length=n.length();
        String reverse="";
        int vowel=0;
        for(int i=(length-1); i>=0; i--)
        {
            char c=n.charAt(i);
            if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
                vowel++;
            reverse+=c;
        }
        System.out.println("Total number of characters : \t"+length+"\nName of vowels : \t\t"+vowel+"\nReverse string : \t\t"+reverse);
    }
}


Result:
OUTPUT:
Enter string : Pneumonoultramicroscopicsilicovolcanoconiosis
Total number of characters : 45
Name of vowels :                    20
Reverse string :                       sisoinoconaclovociliscipocsorcimartluonomuenP

Saturday, 7 February 2015

[JAVA] [ICSE 2005 Answer 4] Finding Salary

Question:
ICSE 2005 Question 4
Write a class with name 'employee' and basic as its data member, to find the gross pay of an employee of the following allowances and deduction. Use meaning ful variable.
Dearness Allowance = 25% of Basic pay
House Rent Allowance = 15% of Basic pay
Provident Fund = 8.33% of Basic pay
Net Pay = Basic Pay + Dearness Allowance + House Rent Allowance
Gross Pay = Net Pay - Provident Fund

Source Code:
class employee
{
    double basic;
    public void find(double basic)throws Exception
    {
        this.basic=basic;
        double da=25.0/100.0*basic;
        double hra=15.0/100.0*basic;
        double pf=8.33/100.0*basic;
        double np=basic+da+hra;
        double gp=np-pf;
        System.out.println("Dearness Allowance = Rs. "+da+"\nHouse Rent Allowance = Rs. "+hra+"\nProvident Fund = Rs. "+pf+"\nNet Pay = Rs. "+np+"\nGross Pay = Rs. "+gp);
    }

}

Result:
INPUT:
20810
OUTPUT:
Dearness Allowance = Rs. 5202.5
House Rent Allowance = Rs. 3121.5
Provident Fund = Rs. 1733.473
Net Pay = Rs. 29134.0
Gross Pay = Rs. 27400.527000000002