Posts

Factorial | Fibonacci | Star printing | Count vowel and consonants

Factorial num=int(input("enter number")); fact=1 if num<0:     print ("invalid input") elif num==0:     print("factorial is:",1) else:     for i in range(num,0,-1):         fact=fact*i     print("factorial is",fact) Explanation      This Python code calculates the factorial of a user-inputted number. It initializes a variable fact to 1 and checks for valid input. If the input is negative, it prints an error message. If the input is zero, it prints that the factorial is 1. For positive inputs, it uses a for loop to multiply the current value of fact by numbers from the input down to 1. Finally, it displays the calculated factorial. Fibonacci #fibonacci sireis of a number n=int(input("enter number")); x=0 y=1 z=0 while z<=n:     print(z)     x=y     y=z     z=x+y Explanation This Python code generates a Fibonacci sequence up to a user-inputted number n using a while loo...