Skip to content

Commit c9eff32

Browse files
Decision Making and loop questions uploaded
1 parent a5c3082 commit c9eff32

24 files changed

+407
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#Q1] Python Program to Check if a Number is Positive, Negative or 0
2+
3+
4+
5+
6+
7+
# from curses.ascii import isalpha
8+
9+
10+
def detec(num):
11+
if(num == 0):
12+
print(num)
13+
14+
elif(num > 0):
15+
print("{} is a Positive number".format(num))
16+
else:
17+
print("{} is a negative number".format(num))
18+
19+
20+
21+
num = input("Enter number : ")
22+
23+
num = int(num)
24+
detec(num)
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
# if num.isalpha() == True:
35+
# print("It's a word")
36+
# elif num.isnumeric() == True:
37+
# num = int(num)
38+
# detec(num)
39+
# elif num.isalnum() == True:
40+
# print("This string contains both letters and numbers")
41+
42+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Q10] Python Program to Check Armstrong Number
2+
3+
4+
def detec(num,res):
5+
6+
if (num == res):
7+
print("It is an Armstrong number")
8+
else:
9+
print("It is not an Armstrong number")
10+
11+
12+
def armstrong(num):
13+
num1 = num
14+
temp = 0
15+
rev = 0
16+
add = 0
17+
num_count = len(str(num1))
18+
while(num > 0):
19+
temp = num % 10
20+
# rev = rev*10 + temp
21+
add = add + pow(temp,num_count)
22+
num = num // 10
23+
return add
24+
25+
26+
27+
num = int(input("Enter number : "))
28+
29+
res = 0
30+
res = armstrong(num)
31+
detec(num,res)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
def armstrong(num):
4+
num1 = num
5+
temp = 0
6+
rev = 0
7+
add = 0
8+
num_count = len(str(num1))
9+
while(num > 0):
10+
temp = num % 10
11+
# rev = rev*10 + temp
12+
add = add + pow(temp,num_count)
13+
num = num // 10
14+
15+
if(num1 == add):
16+
return num1
17+
18+
19+
20+
21+
n1 = int(input("Enter n1 : "))
22+
n2 = int(input("Enter n2 : "))
23+
24+
for i in range(n1,n2):
25+
if(armstrong(i) == None):
26+
continue
27+
print(armstrong(i),end = ' ')
28+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#Q12] Python Program to Find the Sum of Natural Numbers
2+
3+
4+
5+
n1 = int(input("Enter range 1 : "))
6+
n2 = int(input("Enter range 2 : "))
7+
8+
if(n1 < 0 or n2 < 0):
9+
print("Cant accept negative values")
10+
exit()
11+
if(n1 > n2):
12+
temp = 0
13+
temp = n1
14+
n1 = n2
15+
n2 = temp
16+
add = 0
17+
for i in range(n1,n2+1):
18+
add +=i
19+
20+
print("Sum of the Natural numbers from {0} to {1} is {2}".format(n1,n2,add))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Example 1: Program to print half pyramid using *
2+
"""
3+
*
4+
* *
5+
* * *
6+
* * * *
7+
* * * * *
8+
* * * * * *
9+
* * * * * * *
10+
* * * * * * * *
11+
* * * * * * * * *
12+
n = 10
13+
"""
14+
15+
n = int(input("Enter rows : "))
16+
17+
for i in range(0,n):
18+
for j in range(0,i):
19+
print("*",end=' ')
20+
print()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Example 2: Program to print half pyramid a using numbers
2+
3+
"""
4+
1
5+
1 2
6+
1 2 3
7+
1 2 3 4
8+
1 2 3 4 5
9+
n = 6
10+
"""
11+
n = int(input("Enter number of rows : "))
12+
13+
for i in range(0,n):
14+
for j in range(0,i):
15+
print(j+1,end =' ')
16+
print()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Example 3: Program to print half pyramid using alphabets
2+
"""
3+
n = 5
4+
5+
A
6+
B B
7+
C C C
8+
D D D D
9+
"""
10+
n = int(input("Enter number of rows : "))
11+
12+
alpha = 64
13+
for i in range(0,n):
14+
for j in range(0,i):
15+
alph = chr(alpha)
16+
print(alph,end =' ')
17+
alpha +=1
18+
print()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Example 4: Inverted half pyramid using *
2+
3+
"""
4+
n = 5
5+
* * * * *
6+
* * * *
7+
* * *
8+
* *
9+
*
10+
"""
11+
n = int(input("Enter number of rows : "))
12+
13+
for i in range(0,n):
14+
for j in range(0,n-i):
15+
print("*",end=' ')
16+
print()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Example 5: Inverted half pyramid using numbers
2+
"""
3+
n = 5
4+
1 2 3 4 5
5+
1 2 3 4
6+
1 2 3
7+
1 2
8+
1
9+
"""
10+
n = int(input("Enter number of rows : "))
11+
12+
for i in range(0,n):
13+
for j in range(0,n-i):
14+
print(j+1,end=' ')
15+
print()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Example 6: Program to print full pyramid using *
2+
"""
3+
n = 5
4+
*
5+
* *
6+
* * *
7+
* * * *
8+
"""
9+
n = int(input("Enter number of rows : "))
10+
11+
for i in range(0,n):
12+
for j in range(i,n):
13+
print(" ",end ='')
14+
for k in range(0,i):
15+
print("* ",end='')
16+
print()
17+

0 commit comments

Comments
 (0)