0% found this document useful (0 votes)
2 views9 pages

For Loop Coding Practice 9b.python

The document contains various coding exercises focused on loops and basic programming concepts. It includes tasks such as printing numbers divisible by a given number, counting vowels in a string, generating multiplication tables, and determining perfect numbers. Each exercise is accompanied by example inputs and outputs for clarity.

Uploaded by

kvenu8637
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

For Loop Coding Practice 9b.python

The document contains various coding exercises focused on loops and basic programming concepts. It includes tasks such as printing numbers divisible by a given number, counting vowels in a string, generating multiplication tables, and determining perfect numbers. Each exercise is accompanied by example inputs and outputs for clarity.

Uploaded by

kvenu8637
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

For Loop Coding Practice 9B:

Print numbers divisible by M:

n=int(input())
m=int(input())
for i in range(1, n+1):
if i%m==0:
print(i)

Input:10, 3
Output:
3
6
9

Count of Numbers divisible by 2---------------------------------

n=int(input())
t=int(input())
count=0
for i in range(1, n+1):
if i%t==0:
count=count+1
print(count)

Input:12, 13
Output:4

Count of Vowels:------------------------------------------------

s=input()
count=0
for i in s:
if (i=="a") or (i=="e") or (i=="i") or (i=="o") or (i=="u") :
count=count+1
print(count)

Print numbers divisible by 2 and 3------------------------------

n=int(input())
for i in range(1, n+1):
if i%2==0 and i%3==0:
print(i)

Input:15
Output:6, 12

Print required characters:--------------------------------------

s=input()
for i in s:
if i=="a" or i =="z":
print(i)

Input :zigzag
Output :z, z, a

Print from M to N in reverse------------------------------------


m=int(input())
n=int(input())
total_numbers=n-m
for i in range(total_numbers+1):
print(n-i)
Input:2, 5
Output:5, 4, 3, 2

Print characters in Reverse:------------------------------------

s=input()
len_s=len(s)
for i in range(1, len_s+1):
n=len_s-i
print(s[n])

Input: scale
Output: e, l, a, c, s

Print Odd Numbers from N to M:----------------------------------

m=int(input())
n=int(input())
total_numbers=n-m
emp=""
for i in range(total_numbers+1):
num=n-i
if num%2==1:
emp=emp+str(num)+" "
print(emp)

Input:1, 10
Output:9 7 5 3 1

Inverted Right Angled Triangle:---------------------------------

n=int(input())
for i in range(n):
m=n-i
m=m*"* "
print(m)

n = int(input())

for i in range(1, n + 1):


if i == 1:
row = ("* ") * n
elif i == n:
row = "* "
else:
middle_spaces = " " * (n - i - 1)
row = ("* ") + middle_spaces + ("* ")

print(row)
Input:4
Output:
* * * *
* * *
* *
*

Two Right Angled Triangle:--------------------------------------

n=int(input())
for i in range(1, n+1):
print(i*"* ")
for i in range(n):
print((n-i)*"* ")

Input:3
Output:

*
* *
* * *
* * *
* *
*

Inverted Right Angled Triangle:---------------------------------

n=int(input())
print(n*"* ")
for i in range(1, n):
print((n-i)*"+ ")

Input:4
Output:
* * * *
+ + +
+ +
+

Product of Numbers divisible by 3 from M to N:------------------

m=int(input())
n=int(input())
product=1
for i in range(m, n+1):
if i%3==0:
product=product*i
if True:
print(product)
else:
print(1)

( or )
m=int(input())
n=int(input())
product=1
for i in range(m, n+1):
if i%3==0:
product*=i
print(product)

Input:2, 7
Output:18

Sum of K powers:------------------------------------------------

n=int(input())
k=int(input())
sum=0
for i in range(1, n+1):
m=i**k
sum=sum+m
print(sum)

Input:5, 3
Output:225

Count of Numbers divisible by 6 and 8:--------------------------

n=int(input())
count=0
for i in range(1, n+1):
if i%6==0 and i%8==0:
count+=1
print(count)

Input:50
Output:2

Find Power of Number:-------------------------------------------

n=int(input())
m=int(input())
print(pow(n, m))

( or )

n=int(input())
m=int(input())
p=1
for _ in range(m):
p*=n
print(p)

Input:2, 3
Output:8

Product of Numbers from M to N:---------------------------------

m=int(input())
n=int(input())
product=1
for i in range(m, n+1):
product*=i
print(product)

Input:2, 5
Output:120

-----------------------------------------------------------------------------------
---------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------

Multiplication Table:

n=int(input())
for i in range(1, 11):
print(str(n) + " x "+str(i)+ " = "+str(i*n))

Input:3
Output:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

Vowels in a String:---------------------------------------------

s=input()
emp=""
for i in s:
if i=="a" or i=="e" or i=="i" or i=="o" or i=="u":
emp+=i
print(emp)

Input: container
Output: oaie

Reverse the String:---------------------------------------------

s=input()
len_s=len(s)
emp=""
for i in range(1, len_s+1):
n=len_s-i
emp+=s[n]
print(emp)

Input: Hurray! We have won the match.


Output: .hctam eht now evah eW !yarruH

Sum Of K Powers-2:----------------------------------------------

n=input()
len_n=len(n)
sum=0
for i in range(len_n):
sum+=int(n[i])**len_n
print(sum)

Input:24753
Output:21231

Armstrong Number:-----------------------------------------------

n=int(input())
len_n=len(str(n))
sum=0
for i in str(n):
sum+=int(i)**len_n
if sum==n:
print("Armstrong Number")
else:
print("Not an Armstrong Number")

Input:54748
Output: Armstrong Number

Factors of a Number:--------------------------------------------

n=int(input())
for i in range(1, n+1):
if n%i==0:
print(i)

Input:6
Output:1, 2, 3, 6

Factors of a Number-2:------------------------------------------

n=int(input())
result=""
for i in range(1, n+1):
factors=n%i==0
if factors:
result+=str(i)+" "
print(result)

Input:15
Output:1 3 5 15

Sum of all Factors:---------------------------------------------

n=int(input())
sum=0
for i in range(1, n+1):
factors=n%i==0
if factors:
sum+=i
print(sum)

Input:12
Output:28
Greatest Among N Numbers:---------------------------------------

n=int(input())
first_input=int(input())
greatest_number=first_input
for i in range(n-1):
number=int(input())
if number>greatest_number:
greatest_number=number
print(greatest_number)

Input:5, 8, 9, 10, 44, 54


Output:54

Perfect Number:-------------------------------------------------

n=int(input())
sum=0
for i in range(1, n):
if n%i==0:
sum+=i
if sum==n:
print("Perfect Number")
else:
print("Not a Perfect Number")

Input:6
Output: Perfect Number

Number of digits from M to N------------------------------------

m = int(input())
n = int(input())
total_digits_count = 0
for number in range(m, n + 1):
digits_count = len(str(number))
total_digits_count = total_digits_count + digits_count
print(total_digits_count)

Input:4, 13
Output:14

Divisible by 6--------------------------------------------------

m = int(input())
n = int(input())
count = 0
numbers_divisible_by_6 = ""
for number in range(m, n + 1):
if number % 6 == 0:
count = count + 1
numbers_divisible_by_6 = numbers_divisible_by_6 + str(number) + (" ")
if count == 0:
print("No Numbers Found")
else:
print(numbers_divisible_by_6)

Input:6, 23
Output:6, 12, 18

Vowels of a String:---------------------------------------------

string = input()
vowels_count = 0
for character in string:
is_vowel = (character == "a") or (character == "e") or (character == "i") or
(character == "o") or (character == "u")
if is_vowel:
vowels_count = vowels_count + 1
if vowels_count > 2:
print("String has more than two vowels")
else:
print("String doesn't have more than two vowels")

Indivisible Number:---------------------------------------------

n=int(input())
is_divisible=False
for i in range(2, 10):
if n%i==0:
is_divisible=True
if is_divisible:
print("Divisible Number")
else:
print("Indivisible Number")

Smallest among N Numbers:---------------------------------------

n = int(input())
first_input = int(input())
smallest_number = first_input
for i in range(n - 1):
number = int(input())
if number < smallest_number:
smallest_number = number
print(smallest_number)

Divisible by 9 in a range:--------------------------------------

m = int(input())
n = int(input())
count = 0
numbers_divisible_by_9 = ""
for number in range(m, n + 1):
if number % 9 == 0:
count = count + 1
numbers_divisible_by_9 = numbers_divisible_by_9 + str(number) + (" ")
if count == 0:
print("No Numbers found")
else:
print(numbers_divisible_by_9)

----------------------------------------------------------------

n = int(input())
for i in range(1, n + 1):
term = ("2") * i
term = int(term)
print(term)

Sum of N terms in 2 series:-------------------------------------

n = int(input())
term_number = "2"
total = 0
for i in range(1, n+1):
term = term_number * i
total = total + int(term)
print(total)

Input:3
Output:246

Sum of N Terms 1 Series:----------------------------------------

n = int(input())
term_number = "1"
sum_of_terms = 0
for i in range(1, n + 1):
term = term_number * i
sum_of_terms = sum_of_terms + int(term)
print(sum_of_terms)

Input:4
Output:1234

Sum of N Terms in X Series:-------------------------------------

x = int(input())
n = int(input())
sum_of_terms = 0
for i in range(1, n + 1):
term = str(x) * i
sum_of_terms = sum_of_terms + int(term)
print(sum_of_terms)

Input:7, 4
Output:8638

Sum of N Terms in X Square Series:------------------------------

x = int(input())
n = int(input())
sum_of_terms = 0
for i in range(1, n + 1):
number = str(x) * i
term = int(number) ** 2
sum_of_terms = sum_of_terms + term
print(sum_of_terms)

Input:4, 3
Output:199088

You might also like