Question 4

(i)   Write a function to print the Floyds triangle with N rows

              Example: If N = 5, Output:

              1

              2 3

              4 5 6

              7 8 9 10

              11 12 13 14 15

       (ii)   Write a funcion that takes in a number as parameter and check and print whether it is a Pronic number or not. (Pronic number is the number which is the product of two consecutive integers)

              Examples: 12 = 3 × 4

              20 = 4 × 5

              42 = 6 × 7

       (iii)  Write a a function takes a number as parameter and check and display whether it is a Niven number or not. (Niven number is that number which is divisible by its sum of digits).

              Example :

              Consider the number 126.

              Sum of its digits is 1+2+6 = 9 and 126 is divisible by 9.

a)
      public void floyids(int n)
      {
       int i,j,k = 1;
       for(i = 1; i <= n; i++) {
         for(j=1;j <= i; j++){
            System.out.print(" "+k++);
         }
         System.out.println();
         } 
     }
b)
public void pronic (int num)
        {
        int flag = 0;
        for(int i=0; i<num; i++)
        {
            if(i*(i+1) == num)
            {
                flag = 1;
                break;
            }
        }         
        if(flag == 1)
            System.out.println("Pronic Number.");
        else
            System.out.println("Not a Pronic Number.");      
    }

c)
       public void harshadMumber(int n)
        {
        int c=n, d, sum = 0;         
        //finding sum of digits
        while(c>0)
        {
            d = c%10;
            sum = sum + d;
            c = c/10;
        }         
        if(n%sum == 0)
            System.out.println(n+" is a Niven Harshad Number.");
        else
            System.out.println(n+" is not a Niven Harshad Number.");      
       }
This entry was posted in Term 1. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *