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 loop. It initializes three variables, x, y, and z, to 0, 1, and 0 respectively. The loop continues until z exceeds or is equal to n. In each iteration, it prints the current value of z, updates x to the value of y, y to the value of z, and calculates the new value of z as the sum of x and y. This sequence printing continues until reaching or surpassing the user-inputted number, producing a Fibonacci sequence output.


star (pyramid) printing.

n = int(input("enter number"))

i = 1

while i <= n:

    s = 1

    # Print spaces

    while s <= (n - i):

        print(" ", end="")

        s = s + 1

    j = 1

    # Print stars

    while j <= (2 * i - 1):

        print("*", end="")

        j = j + 1

    print()

    i = i + 1

Explanation

This Python code takes a user-inputted number n and prints a pyramid pattern of stars. It uses nested while loops to control the printing of spaces and stars for each row of the pyramid. The outer loop (while i <= n) iterates through each row, the middle loop (while s <= (n - i)) prints leading spaces, and the inner loop (while j <= (2 * i - 1)) prints the stars in a triangular pattern. The result is a visually appealing pyramid of stars whose height is determined by the user's input.


vowel and consonant

str = input("enter a string");
'''vowel = ("a","e","i","o","u");
VOWEL = ("A","E","I","O","U");'''
vowel = ("a,e,i,o,u");
VOWEL = ("A,E,I,O,U");
countv = 0;
countc = 0;
for i in range(0,len(str)):
    if str[i] in vowel or str[i] in VOWEL :
        countv+=1
    else:
        countc+=1
print("no. of vowels=",countv)
print("no. of consonents=",countc)

Explanation

This Python code takes a string input from the user and counts the number of vowels and consonants in the given string. It defines two sets, vowel and VOWEL, which respectively store lowercase and uppercase vowels. It initializes counters for vowels (countv) and consonants (countc). Then, it iterates through each character in the input string, checking if the character is present in either the lowercase or uppercase vowel sets. Depending on the result, it increments the corresponding counter. Finally, it prints the counts of vowels and consonants in the input string. Note that the commented section appears to contain an alternative way of defining vowels, but it's not used in the code execution.

      



Comments