## Pregunta de entrevista

Entrevista de Quality Assurance Engineer

-

# Write a function in any language that will print "AN" if a number is divisible by 8, "ANIM" if divisible by 16 and "ANIMAL" if divisible by 32.

Respuesta

## Respuestas de entrevistas

11 respuestas

16

Good work. I checked your code and it runs. This is a variation on the classic FizzBuzz coding question. One suggestion: You can simplify your code by printing the user prompt in the input function. # i = input("Enter a number or press Q to quit:") Another suggestion: Make the letter "q" case insensitive by passing the ".lower()" method after your input string # i = input("Enter a number or press Q to quit:").lower() One thing: You don't need the following if clause which I've commented out. This condition is already checked and handled outside of the try block. """ try: n = int(i) # if i == "q": # print("Goodbye!") """

Interview Dude en

0

Use string builder to improve the run time.

Anónimo en

0

public static String printAnimal(int num) { if (num >= 8 && num = 16 && num = 16 && num < 32) { if (num % 32 == 0) { return "ANIMAL"; } } return "Number Not Divisible by 8"; }

NVR en

0

I could add the exceptional handling also to this code, incase input is not an integer, I did not add the code though, but that one simple too to add. given below is the solution with user Input data user_data = int(input("Enter your Number :")) def number_divisible(user_data): try: num = int(user_data) print("Proceed") if num %32 ==0: print("ANIMAL") elif num %16 ==0: print("ANIM") elif num % 8 ==0: print("AN") else: print("This number is not divisible by 8 or 16 or 32") except: print("Please Enter valid Number") number_divisible(user_data)

Anónimo en

0

printStringBasedOnDivisibleNumber("AN", 8, 8); printStringBasedOnDivisibleNumber("ANIM", 16, 8); printStringBasedOnDivisibleNumber("ANIMAL", 32, 8); private static String printStringBasedOnDivisibleNumber(String inputString, int inputNumber, int inputDivisibleNumber) { String outputString = ""; if (inputNumber % inputDivisibleNumber == 0) { outputString = inputString; } else { outputString = "Input number - " + inputNumber + " is not divisble by " + inputDivisibleNumber; } return outputString; }

KD en

0

function main(num) { (num%32 == 0) ? console.log('ANIMAL') : (num%16 == 0) ? console.log('ANIM') : (num%8 == 0) ? console.log('AN') : console.log ('Number not divisible by 8, 16, 32'); }

One Line Javascript solution en

0

import java.util.Scanner; public class PrintAnimal { // Write a function in any language that will print "AN" if a number is // divisible by 8, "ANIM" if divisible by 16 and "ANIMAL" if divisible by 32. public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Please enter the number:"); int num = s.nextInt(); if (num != 0) { if (num % 8 == 0) { if (num % 16 == 0) { if (num % 32 == 0) { System.out.println("ANIMAL"); } else System.out.println("ANIM"); } else System.out.println("AN"); } } else { } System.out.println("Entered num not divisle by 8,16,32"); s.close(); } }

Hope this will help someone, Please find the answer using Java en

3

I was not strong enough in Python to solve this problem on the spot. But after some study I believe the following would have been a suitable answer: def numberchecker(): print("Enter Number or Press q to quit:") i = input() if i == "q": print("Good bye!") else: try: n = int(i) if i == "q": print("Good bye!") elif n % 32 == 0: print("ANIMAL") numberchecker() elif n % 16 == 0: print("ANIM") numberchecker() elif n % 8 == 0: print("AN") numberchecker() else: print("Try a diffrent number next time.") numberchecker() except ValueError: print("Entry is unacceptable.") numberchecker() numberchecker()

Anónimo en

3

import java.util.*; import java.util.Scanner; public class Main { public static void main (String[]args) { int num ; Scanner a = new Scanner(System.in); num = a.nextInt(); System.out.println("enter num " +num); if(num%8==0) { System.out.print("AN"); } if(num%16==0) { System.out.print("IM"); } if(num%32==0) { System.out.print("AL"); } } }

Anónimo en

0

for i in range (1,100): #print(i) if i%8==0 and i%16!=0 and i%32!=0: print("AN") print(i) elif i%32==0: print('ANIMAL') print(i) elif i%16==0 and i%32 !=0: print('ANIM') print(i)

Aravind en

1

Simple one to solve in Python Coding def number_divisible(num): if num %32 ==0: print("ANIMAL") elif num %16 ==0: print("ANIM") elif num % 8 ==0: print("AN") given_num = 8 number_divisible(given_num)

Anónimo en