|
| 1 | +#Program 1: Factorial |
| 2 | + |
| 3 | +Find factorial of any number given |
| 4 | +Factorial of number is calculated by multiplying n*(n-1) |
| 5 | +Sample Input:4 |
| 6 | +Factorial: 4*3*2*1 |
| 7 | +Output: 24 |
| 8 | +Sample input:6 |
| 9 | +Factorial: 6*5*4*3*2*1 |
| 10 | +Output:720 |
| 11 | + |
| 12 | +Method 1 using For loop |
| 13 | + |
| 14 | +f=1 |
| 15 | +n = int(input('Sample input:')) |
| 16 | +for i in range(1,n+1): |
| 17 | + f=f*i |
| 18 | +print(f) |
| 19 | + |
| 20 | +Method 2 using while loop |
| 21 | + |
| 22 | +f=1 |
| 23 | +n = int(input(‘Sample input:’)) |
| 24 | +while(n>0): |
| 25 | + f=f*n |
| 26 | + n=n-1 |
| 27 | +print(f) |
| 28 | + |
| 29 | +#Program 2: Guess a Number |
| 30 | + |
| 31 | +Ask a user to guess a number between 1 to 50. He gets only 5 chance to guess. Give user a feedback if his guess is high or low. |
| 32 | + |
| 33 | +import random |
| 34 | + |
| 35 | +num=random.randint(1,50) |
| 36 | +print("Guess a Number from 1-50 \n You have only 5 chance\n") |
| 37 | +chances=0 |
| 38 | +while chances<5: #you only get 5 chances to guess |
| 39 | + guess=int(input('Take a guess:')) |
| 40 | + if guess<num: |
| 41 | + print('Your guess is too low') |
| 42 | + chances+=1 |
| 43 | + if guess>num: |
| 44 | + print('Your guess is too high') |
| 45 | + chances+=1 |
| 46 | + if guess==num: |
| 47 | + chances+=1 |
| 48 | + break |
| 49 | +else: |
| 50 | + print("You ran out of chances") |
| 51 | +if guess==num: |
| 52 | + print('Nicely Done! You guessed the number in ',chances,' guesses') |
| 53 | + |
| 54 | +#Program 3: FizzBuzz |
| 55 | + |
| 56 | +Consider ‘n’ numbers, print all the numbers from 1 to n and replace some number according to this criteria: |
| 57 | + • If the number is divisible by 3, write Fizz instead of the number |
| 58 | + • If the number is divisible by 5, write Buzz instead of the number |
| 59 | + • If the number is divisible by 3 and 5 both, write FizzBuzz instead of the number |
| 60 | +[Constraints n should be more than 1 and less than 100; i.e., 1<=n<100] |
| 61 | + |
| 62 | +while True: |
| 63 | + n = int(input("Enter a number(1-100):")) |
| 64 | + if n>=1 and n<=100: |
| 65 | + for i in range(1,n+1): |
| 66 | + if i%3 == 0 and i%5==0: |
| 67 | + print("FizzBuzz") |
| 68 | + elif i%3==0: |
| 69 | + print("Fizz") |
| 70 | + elif i%5==0: |
| 71 | + print("Buzz") |
| 72 | + else: |
| 73 | + print(i) |
| 74 | + break |
| 75 | + else: |
| 76 | + continue |
| 77 | + |
| 78 | +#Program 4: Neon Number |
| 79 | + |
| 80 | +Enter a number and check whether the number is neon number or not |
| 81 | +Sample input: 9 |
| 82 | +Sample output: Yes it is a neon number |
| 83 | +Sample input: 6 |
| 84 | +Sample output: No it is not a neon number |
| 85 | + |
| 86 | +***[Neon number explanation: input=9 |
| 87 | +Step one: 9*9 =81 |
| 88 | +Step two: 81 = 8+1 = 9 |
| 89 | +Step three: 9 == sample input |
| 90 | +Yes it is a neon number]*** |
| 91 | + |
| 92 | +n = int(input("Sample input:")) |
| 93 | +first_step=n*n |
| 94 | +sum=0 |
| 95 | +while(first_step>0): |
| 96 | + second_step=first_step%10 |
| 97 | + sum+=second_step |
| 98 | + first_step=first_step//10 |
| 99 | +if(sum==n): |
| 100 | + print('The given number',n,'is neon number') |
| 101 | +else: |
| 102 | + print('The given number',n,'is not neon number') |
0 commit comments