Showing posts with label ICSE Exam. Show all posts
Showing posts with label ICSE Exam. Show all posts
Program to print Floyd's triangle in Java

Program to print Floyd's triangle in Java

 

Program to print Floyd's triangle in Java

Program to print Floyd's triangle in Java

The Floyd’s triangle is a triangle of natural consecutive numbers and it is starting with a 1 to n. it is named after Robert Floyd an American computer scientist.

It looks like the following pattern 

1

2 3

4 5 6

7 8 9 10

11 12 113 14 15

import java.util.*;

public class FloydTriangle

{

   public static void main(String args[])

   {

       int rows;  // input number of rows

       int i, j;  // i and j used as loop

       int k = 1; // k generate the floyd's Triangle

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter number of rows to generate Floyd's Triangle:");

       rows = sc.nextInt();

       // loop

       for(i=1; i<=rows; i++)

       {

           for(j=1; j<=i; j++)

           {

               // print the values of triangle

               System.out.print(k + " ");

               k++;

            }

            System.out.println();

        }

    }

}

Output:

Enter number of rows to generate Floyd's Triangle: 5

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

Here another output with 10 rows

Enter the number of rows to generate Floyd's Triangle: 10

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31 32 33 34 35 36

37 38 39 40 41 42 43 44 45

46 47 48 49 50 51 52 53 54 55

 

We can also print with 5 rows Floyd’s triangle as

15

14 13

12 11 10

9   8    7   6

5   4   3   2    1

By just changing k=1 to k=15 and in the loop k++ to k—as below

for(i=1; i<=rows; i++)

       {

           for(j=1; j<=i; j++)

           {

               // print the values of triangle

               System.out.print(k + " ");

               k--;

            }

            System.out.println();

        }

 

More Java programs 

Binary search in java with bubble sort

Binary search in java with bubble sort

Binary Search in java with Bubble sort

Binary search in java with bubble sort 

Binary Search with Bubble sort program has been defining as follows. First, we have taken 10 elements in the array name[] and then perform the bubble sort. After sorting we have done binary sort.

What is Binary search?

Binary search is a search algorithm that is faster than linear search and the time the complexity of the above algorithm is O(n). In this search, data must be in sorted order and it perform divide the array length by 2 and find the middle value and if the middle value less than the search value then it performs towards array lesser value till it finds the match and if the search element greater the middle value then it performs a search towards the array’s upper part of its mid value.

Java program is as follows   

import java.io.*;

public class BinarySearchWithSorting

{

    public static void main(String args[]) throws IOException

    {

        String name[] = new String[20];

        String s, t;

        int i,f=0,j;

        int low, high, mid, pos;

        InputStreamReader in=new InputStreamReader(System.in);

        BufferedReader br=new BufferedReader(in);

        System.out.println("Enter 10 Names in array:");

        for(i=0;i<10;i++)

        {

            name[i] = br.readLine();

        }

        System.out.println("Enter name to Search:");

        s = br.readLine();

        // Bubble Sorting of array

        for(i=0;i<9;i++)

        {

            for(j=0;j<9-i;j++)

            {

                if(name[j].compareTo(name[j+1])>0)

                {

                    t = name[j];

                    name[j] = name[j+1];

                    name[j+1] = t;

                }

            }

        } // end of sorting

        System.out.println ("Sorted array :");

        for(i=0;i<10;i++)

        {

            System.out.print (name[i] + " ");

        }

        // Binary Search

        high = 10;

        low = 0;

        pos = 0;

        while(low <= high)

        {

            mid = (low + high) / 2;

            if(s.compareTo(name[mid])<0)

            {

                high = mid - 1;

            }

            else if(s.compareTo(name[mid])>0)

            {

                low = mid + 1;

            }

            else if(s.compareTo(name[mid])==0)

            {

                pos = mid + 1;

                break;

            }

        }

        System.out.println("\n Search result");

        System.out.println(s+" is located at " + pos);

    }

}

Output

Enter 10 Names in the array:

Ramesh

Akbar

Sunil

Binay

Cindy

Umesh

Junaid

Danny

Kalim

Nagma

Enter name to Search:

Sunil

Sorted array:

Akbar Binay Cindy Danny Junaid Kalim Nagma Ramesh Sunil Umesh

 Search result

Sunil is located at 9


More Java programs 

 

simple interest in java | what is simple interest | ICSE computer science question

simple interest in java | what is simple interest | ICSE computer science question

simple interest in java

What is simple interest?

We do calculate simple interest with principal, rate, and time or term with this formula p*t*r/100. It is not compounded as simple interest only calculated with the principal amount.

In the formula p*r*t/100

  • p is Principal
  • t is time for term
  • r is rate

Simple interest in java | what is simple interest 

import java.util.*;

public class SimpleInterest1

{

    public static void main()

    {

        double p, r, t, si;

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter Principal, Time and Rate--->");

        p=sc.nextDouble();

        t=sc.nextDouble();

        r=sc.nextDouble();

        si=(p*t*r)/100.0;

        System.out.println("Simple Interest=" + si);

    }       

}

In the above program, we have taken Scanner class for inputting principal, rate, and time or term from the user. After taking values of p, t, r then we will calculate simple interest in the si variable.

 

Second program using methods.

import java.util.*;

public class SimpleInterest

{

    double p, t, r, si; //Instance Variable

    public double simpleInterest(double p1, double t1, double r1) //Formal parameter

    {

        p=p1;

        t=t1;

        r=r1;

        si=(p1*t1*r1)/100.0;   

        return si;

    }

    public static void main(String args[])

    {

        SimpleInterest ob=new SimpleInterest();

        double pr, time, rate, s; // Local variable

        Scanner sc= new Scanner(System.in);

        System.out.println("Enter Principal, Time and rate :");

        pr= sc.nextDouble();

        time= sc.nextDouble();

        rate= sc.nextDouble();

        s= ob.simpleInterest(pr, time, rate); // Function call, with parameter(Actual parameter)

        System.out.println("Simple Interest=" + s);

    }

}

In the above program, we have calculated simple interest using the return method. All the values have taken from the main() method and send the values with the method as an actual parameter and that value have received by simpleInterest (double p1, double t1, double r1) method. After calculating the value, the method sends or returns it to the main() method and then prints it on the screen.

 

Question for ICSE exam

ABC bank declares a new tariff or rate for term deposit schemes for their general customers and senior citizens as given below:

Term

Rate of Interest for General

Rate of Interest for Senior Citizen

Up to 2 year

7.5%

8.0%

Up to 4 years

8.5%

9.0%

Up to 6 years

9.5%

10.0%

More than 6 years

10.0%

11.0%

The 'senior citizen's rates are applicable to the customers whose age is 65 years or more. Write a program to input the principal in term deposit scheme, age of the customer and the term or time. The program displays the information in the following format:

Principal Deposited

Term/Duration

Age

Interest earned

Amount Paid

xxxx

xxxx

xxxx

xxxx

xxxx

 

Solution

import java.util.*;

public class BankDepositSchemes

{

    // instance variables

    double term, principal, interest, amount, rate;

    int age;

    // input method for taking values

    void input()

    {

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter Principal :");

        principal=sc.nextDouble();

        System.out.println("Enter Term :");

        term=sc.nextDouble();

        System.out.println("Enter Age :");

        age=sc.nextInt();

    }

   // Calculate the si

    void calculate()

    {

        if(term<=2)

        {

            if(age>=65)

            {

                rate=8.0;

            }

            else

            {

                rate=7.5;

            }

        }

        else if(term>2 && term<=4)

        {

            if(age>=65)

            {

                rate=9.0;

            }

            else

            {

                rate=8.5;

            }

        }

        else if(term>4 && term<=6)

        {

            if(age>=65)

            {

                rate=10.0;

            }

            else

            {

                rate=9.5;

            }       

        }

        else

        {

            if(age>=65)

            {

                rate=11.0;

            }

            else

            {

                rate=10.0;

            }

        }

        interest=(principal*term*rate)/100.0;

        amount=principal + interest;

    }

    // print the values

    void display()

    {

        System.out.println("Amount Deposited\tTerm\tAge\tInterest Earned\t\tAmount Paid");

        System.out.println(principal + "\t\t\t" + term + "\t" + age + "\t" + interest + "\t" + amount);

    }

// main() method

    public static void main(String args[])

    {

        BankDepositSchemes ob=new BankDepositSchemes();

        ob.input();

        ob.calculate();

        ob.display();

    }           

}


More Java programs