Friday, February 1, 2019

Find Year

  • Problem Description

    Most years have 365 days. However, the time required for the Earth to orbit the Sun is actually slightly more than that. As a result, an extra day, February 29, is included in some years to correct for this difference. Such years are referred to as leap years.

    The rules for determining whether or not a year is a leap year follow:

    Any year that is divisible by 400 is a leap year.
    Of the remaining years, any year that is divisible by 100 is not a leap year.
    Of the remaining years, any year that is divisible by 4 is a leap year.
    All other years are not leap years.

    Write a program that reads a year from the user and displays a message indicating
    whether or not it is a leap year.
  • CODING ARENA
  • m=int(input())
    if(m%4)==0:
        if(m%100)==0:
            if(m%400)==0:
                print(m,"is a leap year")
            else:
                print(m,"is not a leap year")
        else:
            print(m,"is a leap year")
    else:
        print(m,"is not a leap year")
        

  • Test Case 1

    Input (stdin)
    2016
    
    
    Expected Output
    2016 is a leap year
  • Test Case 2

    Input (stdin)
    2001
    
    
    Expected Output
    2001 is not a leap year

Sieve of Eratosthenes

  • Problem Description

    Python Program to Read & Print Prime Numbers in a Range using Sieve of Eratosthenes.

    To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:

    Create a list of consecutive integers from 2 through n: (2, 3, 4, ..., n).

    Initially, let p equal 2, the smallest prime number.

    Enumerate the multiples of p by counting to n from 2p in increments of p, and mark them in the list (these will be 2p, 3p, 4p, ...; the p itself should not be marked).

    Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this new number (which is the next prime), and repeat from step 3.

    When the algorithm terminates, the numbers remaining not marked in the list are all the primes below n.
  • CODING ARENA
  • n=int(input())
    sieve=set(range(2,n+1))
    while sieve:
        prime=min(sieve)
        print(prime,end="\n")
        sieve-=set(range(prime,n+1,prime))
     
    print()
  • Test Case 1

    Input (stdin)
    10
    
    
    Expected Output
    2
    
    3
    
    5
    
    7
  • Test Case 2

    Input (stdin)
    20
    
    
    Expected Output
    2
    
    3
    
    5
    
    7
    
    11
    
    13
    
    17
    
    19

May to August

  • Problem Description

    The length of a month varies from 30 and 31 days. 

    In this exercise you will create a program that reads the name of a month from the user as a string. 

    The program should get input from May to August.

    If the input is from may to other months then display error messages as "Invalid"

    Note:
    Use only if and elif
  • CODING ARENA
  • month=input()
    if(month=="Jun"):
        print("30")
    elif (month=="May","July","Aug"):
        print("31")
    else:
        ("Invalid")

  • Test Case 1

    Input (stdin)
    May
    
    
    Expected Output
    31
  • Test Case 2

    Input (stdin)
    Jun
    
    
    Expected Output
    30

Bigger Even

  • Problem Description

    Write a program to find the biggest even number from the given input

    Input:Positive numbers

    Output:Display the largest even number.

    Refer sample input and output for formatting specification.
  • CODING ARENA
  • arr=[]
    a=int(input())
    b=(input().split())
    for i in range(a):
        if(int(b[i])%2==0):
            arr.append(b[i])
    print("Largest even number:",arr[-1])
  • Test Case 1

    Input (stdin)
    5
    
    199 191 991 2 551
    
    
    Expected Output
    Largest even number: 2
  • Test Case 2

    Input (stdin)
    7
    
    199 191 991 2 551 226 554
    
    
    Expected Output
    Largest even number: 554

Range Armstrong

  • Problem Description

    Write a program to find the armstrong numbers between the given range
    Input 1:Lower Range

    Input 2: Upper range

    Output:
    List of Armstrong numbers

    Refer sample input and output for formatting specification.
  • CODING ARENA
  • lower=int(input())
    upper=int(input())
    for num in range(lower, upper + 1):
       order = len(str(num))
        

       sum = 0

       temp = num
       while temp > 0:
           digit = temp % 10
           sum += digit ** order
           temp //= 10

       if num == sum:
           print(num)
  • Test Case 1

    Input (stdin)
    100
    
    2000
    
    
    Expected Output
    153
    
    370
    
    371
    
    407
    
    1634
  • Test Case 2

    Input (stdin)
    100
    
    1000
    
    
    Expected Output
    153
    
    370
    
    371
    
    407

Sum of Squares of Each Digit

  • Problem Description

    Write a program to read a positive integer and find the sum of squares of individual digits.
  • CODING ARENA
  • n=int(input())
    tot=0
    while(n>0):
        dig=n%10
        tot=tot+dig*dig
        n=n//10
    print(tot)
  • Test Case 1

    Input (stdin)
    8974
    
    
    Expected Output
    210
  • Test Case 2

    Input (stdin)
    5398
    
    
    Expected Output
    179

FACTORS

  • Problem Description

    The program finds the factors of a number, which means the other numbers which can divide the given number. 
    Eg: 5 is divisible by 5 and 1
  • CODING ARENA
  • a=int(input())
    for i in range(1,a+1):
        if(a%i==0):
            print(i)
  • Test Case 1

    Input (stdin)
    6
    
    
    Expected Output
    1
    
    2
    
    3
    
    6
  • Test Case 2

    Input (stdin)
    5
    
    
    Expected Output
    1
    
    5