prac

mail@pastecode.io avatar
unknown
python
a year ago
3.8 kB
3
Indexable
3. Write a function to find sum of digits of a number
4. Write a function to print n terms of Fibonacci series
5. Write a program to print the following pattern:
*
* *
* * *
* * * * (Example: if number of lines = 4)


def sum_of_digits(n):
    # Initialize the sum to 0
    total = 0
    
    # Iterate through each digit of the number
    while n > 0:
        digit = n % 10
        total += digit
        n //= 10
    
    return total

# Test the function
number = int(input("Enter a number: "))
result = sum_of_digits(number)
print("Sum of digits:", result)



def fibonacci(n):
    # Initialize the first two terms
    a, b = 0, 1
    
    # Check if n is less than or equal to 0
    if n <= 0:
        return "Invalid input"
    
    # Print the first n terms of the Fibonacci series
    for _ in range(n):
        print(a, end=" ")
        a, b = b, a + b

# Test the function
terms = int(input("Enter the number of terms: "))
fibonacci(terms)


def print_pattern(n):
    for i in range(1, n + 1):
        for j in range(i):
            print("*", end=" ")
        print()

# Test the function
lines = int(input("Enter the number of lines: "))
print_pattern(lines)



# Assignment 1
#! Practice Questions
#program to check if n if postive or negative

# def checkN(n):
#     if(n > 0):
#         print(n,"is greater Postive Number")
#     elif (n < 0):
#         print(n, "is Negative Number")
#     else:
#         print(n,"is zero")

# checkN(5)
# checkN(-1)
# checkN(0)

# #check if n is / by 3

# def check_for_three(n):
#     return n % 3 == 0

# print(check_for_three(3))
# print(check_for_three(27))
# print(check_for_three(1))

# def main():
#     a = int(input("Enter value of a"))
#     b = int(input("Enter value of b"))
#     c = int(input("Enter value of c"))

#     check_middle(a,b,c)


# def check_middle(a,b,c):
#     if a > b & a < c:
#         print("A is between B and C")
#     else:
#         print("A is not between B and C") 

# main()


#Loops
# for i in range(1, 10):
#     if(i % 2 == 0):
#         print(i)

# def getNo():
#     n = int(input("Enter the number of elements"))
#     for i in range(0, n):
#         j = int(input("Enter the element"))
#         check_even_odd(j)

# def check_even_odd(n):
#     if(n % 2 == 0):
#         print(n, "is Even")
#     elif (n % 2 != 0):
#         print(n, "is odd")
#     else:
#         print("Neither Even nor odd")   

# getNo()

# inefficent method 
# def check_for_prime():
#     n = int(input("enter the number to check"))
#     is_prime(n)

# def is_prime(n):
#     if(n > 1):
#         for i in range(2, n):
#             if(n % i == 0):
#                 print(n,"Is not Prime")
#                 break;
#         else:
#             print(n,"Is Prime")
#     else:
#         print(n,"is not Prime")

# check_for_prime()

#display all numbers between 1 to 100 / 5
#Using for loop
# for i in range(1, 101):
#     if(i % 5 == 0):
#         print(i)

#using while loop
# i = 1
# while(i <= 100):
#     if(i % 5 == 0):
#         print(i)
#     i+=1

#using while loop (another method)
# num = 5

# while num <= 100:
#     print(num)
#     num += 5

# take 2 numbers as parameters and return which is max
# def find_max():
#     a = int(input("Enter a"))
#     b = int(input("Enter b"))
#     print("Max of a and b are ", max(a,b))

# def which_max(a,b):
#     max = 0
#     if( a > b):
#         max = a
#     else:
#         max = b
#     return max

# find_max()

#Assignment 2
def welcome(*args):
    print("Welcome " + args[2])
    print(args)

welcome("Abc", "Xyz", "Pqr", "Mno")