Home > Software design >  how to solve program that sum of multiple number by 3 or 5 but only print number which is lower than
how to solve program that sum of multiple number by 3 or 5 but only print number which is lower than

Time:01-22

I wrote code like this using Python:

a = int(input("number = "))

def solution(a) :
    
    b = 1
    c = 0
    for b in range (1,a) :
        
        if b%3==0 or b%5==0 :
            c  = b
            print(" ",b,end=" ")
            
        if b>=a-1 :
            print("=",c)
            
solution(a)

But when I run this code it show's this:

number = 10
  3   5   6   9 = 23

How to delete before number 3?

thank you

CodePudding user response:

Try:

a = int(input("number = "))

def solution(a) :
    
    b = 1
    c = 0
    for b in range (1,a) :
        #For first iteration
        if c == 0:
          if b%3==0 or b%5==0 :
            c  = b
            print(b,end=" ")

        #For remaining numbers
        if c>b and b%3==0 or b%5==0:
            c  = b
            print(" ",b,end=" ")
            
        if b>=a-1 :
            print("=",c)
            
solution(a)
  •  Tags:  
  • Related