Sample programs: Functions basics

Simple programs - solution for practice:

 Copy and try on your own.
1) First 10 prime numbers program in python
    code:
    n = 10
    count=0
    answer=[]
    print("First ten prime no program in Python")
    for i in range(1,n+1):
        for j in range(2,n+1):
            if i % j == 0:
                count+=1
        if count == 1:
            answer.append(i)
        count=0
    print(answer)



First 10 prime number program in Python
Output:
Output of prime number program
2) Write a program to set any two numbers to calculate the sum, difference, product, and division. Print the result.

code:
Num1=int(input("Enter 1st number:"))
Num2=int(input("Enter 2nd number:"))

#Addition logic
Num3 = Num1+Num2
print("Sum value:",Num3)

#Subtraction logic
Num4 = Num1 - Num2
print("Difference value:",Num4)

#Product/multiplication logic
Num5 = Num1 * Num2
print("Product value:",Num5)

#Single division logic
Num6 = Num1 / Num2
print("Single divison value:",Num6)

#Double division logic
Num7 = Num1 // Num2
print("Double division value:",Num7)

#Modulus logic
Num8 = Num1 % Num2

print("Modulus division value:",Num8)


Arithmetic operations
Output:
Output of Arithmetic operations

3) Write a Python program to read a list of integers and print only the multiples of 8. 



code:

list_of_integers=[1,2,5,8,10,16,32,36,40,54,72,75,80]
#Logic to read a list of integers and print only the multiples of 8. 
print("Multiples of 8 are:")
for i in list_of_integers:
    if (i % 8) == 0:
        print(i, end=',')
        
Multiples of 8
Output:
Output - Multiples of 8
4) Write a function in python to find the greater of 2 numbers.

code:
    def greater(a,b):
        if a > b:
            print(a,"is greater than ",b)
        else:
            print(b,"is greater than ",a)
    print("To find greater of 2 number")    
    num1=int(input("Enter 1st number:"))
    num2=int(input("Enter 2nd number:"))

    greater(num1,num2)


Greater of 2 numbers
Output:
Output - Greater of  2 numbers

Simple programs to practice:


1) Write a Python function Factorial(n) which returns the factorial of the given number(n).


2) Write a Python function, Square(num) which returns the square of the given number(num).

3) Write a Python function, Sum(n) which returns the sum of first n numbers.

4) Write a Python function, Palindrome(num) that accepts an integer num as input and returns true/false based on condition.

    Ex: num = 12321 ---> output = 12321 ----> then given number is palindrome.

5) Write a Python function, check_strong_number(num) that accepts a positive as input and return true/false based on condition.

    A number is said to be Strong number if the sum of the factorial of each digit is equal to the given number.

   Ex: 145 is strong number as (1! =1) + (4! = 24) + (5! = 120) = 145.


Previous post:
  • Collections
          Click here -----> Lesson 4: Collections
  • Functions
          Click here -----> Lesson 3: Functions in python
  • Control structures
          Click here -----> Lesson 2: Control structures
  • Basics of python
          Click here -----> Lesson 1: Basics of python
  • About: Introduction to python course
          Click here -----> About: Introduction to python course

No comments:

Post a comment