Sunday, 24 December 2017

Question :
A normal number is defined to be one that has no odd factors, except for 1 and possibly itself. Write a method named isNormal that returns 1 if its integer argument is normal, otherwise it returns 0. The function signature is
int isNormal(int n)

Examples: 1, 2, 3, 4, 5, 7, 8 are normal numbers. 6 and 9 are not normal numbers since 3 is an odd factor. 10 is not a normal number since 5 is an odd factor. 

Solution :
public static int isNormal(int n)
       {
           int result = 1;
           for(int i=2;i<n;i++)
           {
               if(n%i==0)
               {
                   if(i%2!=0)
                   {
                       result = 0;
                       break;
                   }
               }
           }
           return result;
       }

1 comment:

  1. //This is my brief code
    static int isNormal(int n) {
    for (int i = 2; i < n; i++) {
    if (n % i == 0 && i % 2 != 0) {
    return 0;
    }
    }
    return 1;
    }
    }

    ReplyDelete

Powered by Blogger.

Followers

Translate

Currency Converter

Exchange Rate

Featured post

Interpolation in angular 5

When we want to communicate from component class to template ,we can use interpolation which use the { { expression }} to render the bound...

Popular Posts

My Facebook Page