mercredi 7 août 2019

how can i reduce the count of "if statement" to 3?

Write a program that receives a number on the input. - If the number is a multiple of 3, it prints "Jugs". - If the number is a multiple of 5, it prints "Mugs". - If the number is a multiple of 7, it prints "Pugs".

  • If the number is a multiple of both 3 and 5, it prints "JugsMugs".
  • If the number is a multiple of both 3 and 7, it prints "JugsPugs".
  • If the number is a multiple of both 5 and 7, it prints "MugsPugs".
  • If the number is a multiple of both 3, 5 and 7, it prints "JugsMugsPugs".

Otherwise, it prints the number.

i got the output using 4 if statements but how can it be done using just 3 if statements?

a=int(input())
if(a%3==0):
  print ("Jugs",end='')
if(a%5==0):
  print("Mugs",end='')
if(a%7==0):
  print("Pugs",end='')
if(a%3!=0 and a%5!=0 and a%7!=0):
  print(a)

Aucun commentaire:

Enregistrer un commentaire