# Logic Building Programs In Python With Solutions

Want to improve your programming logic, then you are at the right place, Today I will share with you logic building programs in python with solutions.

Logic building is correct reasoning in which you need to do logical thinking and apply principles in a disciplined manner to achieve an acceptable result.

You can solve this programs on this **online python compiler** if you don’t have python installed.

### 1. Guessing Number Program

You need to write a program that will generate a random number between 1 to 10 this number will have to be guessed by the user and if they guess the number then print a message saying you guessed the number also print the number of tries it took for the user to guess the number.

Also give a limit of 5 guesses to the user if they don’t guess in 5 tries then print a message saying they didn’t guess the number and print the actual number.

**Example Output**

```
Guess a number between 1 and 10: 1
Your guess is too low you have 4 left
Guess a number between 1 and 10: 3
Your guess is too low you have 3 left
Guess a number between 1 and 10: 9
Your guess is too high you have 2 left
Guess a number between 1 and 10: 9
Your guess is too high you have 1 left
Guess a number between 1 and 10: 9
Your guess is too high you have 0 left
You did not guess the number, The number was 5
```

### 2. Check Even Odd Number Program

You need to write a python program to check whether the entered number is even or odd. A clue for you even numbers when divided by 2 the remainder is 0 use % operator to find remainder.

**Example Output**

```
Enter a number to check whether it is odd or even: 2
The number is even
```

### 3. Leap Year Program

A leap year has 366 days which is one more day extra then normal year so you need to write a program that will ask the user to enter a year and you have to check and print whether it is a leap or not leap year.

Note that leap year is after every 4 years leap years example 2000, 2004, 2008.

**Example Output**

```
enter a year to check: 2003
not a leap year
```

### 4. Check If Number Is Positive Or Negative Program

Write a program in python that will check and print if the given number is positive (example: 1,2,3…) or negative (example -1,-2,-3…) it is a simple program so no clue.

**Example Output**

```
Enter the number: -1
The number provided is negative
```

### 5. Pyramid Pattern Program

Write a program in python that will print the below start pyramid pattern. Clue you need to use nested for loop.

**Example Output**

```
*
* *
* * *
* * * *
* * * * *
```

### 6. Print Table For Any Given Number Program

Write a program in python that will print table of any given number till 10 times. Clue run a for loop for 10 times.

**Example Output**

```
Enter a number to print the tables for: 10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
```

### 7. Print Square Of Any Given Number Program

Write a program in python that will print the square of any given number. Example squre of 2 is 4.

**Example Output**

```
Enter a number: 3
square = 9
```

### 8. Remove Spaces From String Without Using Inbuilt Function Program

Write a program in python that will remove the spaces from an string without using inbuilt function in python clue: Iterate the string and check for what you are trying to remove.

**Example Output**

```
Enter a String : Hellow World
String after removing spaces : HelloWorld
```

### 9. Find The Largest Among 3 Numbers Program

Write a program in python that will find the largest among 3 numbers. Clue: Compare them with each other.

**Example Output**

```
Enter A value :90
Enter B value :32
Enter C value :12
a is largest
```

### 10. Reverse A Number Using While Loop Program

Write a program in python that will reverse a number using while loop for example 1234 will become 4321.

**Example Output**

```
Enter a number: 3434
Reversed Number: 4343
```

### 11. Remove Punctuations From A String Program

Write a program in python that will remove punctuations from a string. To do this program first define punctuations then check.

**Example Output**

```
Enter a string with punctuations: Hello how are you?
Hello how are you
```

### 12. Count The Number Of Digits in A Number Program

Write a program in python that will count the number of digits in a number use ‘//’.

**Example Output**

```
Enter Number: 8080484
Number of digits: 7
```

## Logic Building Program Solutions In Python

Below are the solutions to the above logic building programs in python, Note that your code can be different only thing you need to get is the result properly.

### 1. Guessing Number Program Solution

```
import random
number = random.randint(1, 10)
number_of_guesses = 0
while number_of_guesses < 5:
guess = int(input('Guess a number between 1 and 10: '))
number_of_guesses += 1
if guess < number:
print(f"Your guess is too low you have {5 - number_of_guesses} tries left")
if guess > number:
print(f"Your guess is too high you have {5 - number_of_guesses} tries left")
if guess == number:
break
if guess == number:
print('You guessed the number in ' + str(number_of_guesses) + ' tries')
else:
print('You did not guess the number, The number was ' + str(number))
```

### 2. Check Even Odd Number Program Solution

```
num = int(input("Enter a number to check whether it is odd or even: "))
if num % 2 == 0:
print ("The number is even")
else:
print ("The number is odd")
```

### 3. Leap Year Program Solution

```
year = int(input('enter a year to check: '))
if year % 400 == 0:
print('it is a leap year')
elif year % 4 == 0:
print('it is a leap year')
elif year % 100 == 0:
print('not a leap year')
else:
print('not a leap year')
```

### 4. Check If Number Is Positive Or Negative Program Solution

```
number = float(input("Enter the number: "))
if number >= 0:
print ("The number is positive")
else:
print ("The number is negative")
```

### 5. Pyramid Pattern Program Solution

```
for i in range(0, 5):
for j in range(0, i+1):
print("* ",end="")
print("\r")
```

### 6. Print Table For Any Given Number Program Solution

```
number = int(input("Enter a number to print the tables for: "))
for i in range(1,11):
print(number,"x",i,"=", number * i)
```

### 7. Print Square Of Any Given Number Program Solution

```
num = int(input("Enter a number: "))
print("square =",num*num)
```

### 8. Remove Spaces From String Without Using Inbuilt Function Program Solution

```
string = input("Enter a String : ")
result=""
for i in string:
if i!=" ":
result += i
print("String after removing spaces :",result)
```

### 9. Find The Largest Among 3 Numbers Program Solution

```
a = int(input("Enter A value :"))
b = int(input("Enter B value :"))
c = int(input("Enter C value :"))
if a >= b and a >= c:
print("a is largest")
elif b>=a and b>=c:
print("b is largest")
elif c>=a and c>=b:
print("c is largest")
```

### 10. Reverse A Number Using While Loop Program Solution

```
num = int(input("Enter a number: "))
reversed_num = 0
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("Reversed Number: " + str(reversed_num))
```

### 11. Remove Punctuations From A String Program

```
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
punct_str = input("Enter a string with punctuations: ")
no_punct_str = ""
for char in punct_str:
if char not in punctuations:
no_punct_str = no_punct_str + char
print(no_punct_str)
```

### 12. Count The Number Of Digits in A Number Program

```
num = int(input("Enter Number: "))
count = 0
while num != 0:
num //= 10
count += 1
print("Number of digits: " + str(count))
```

## Summary

This were all the python programs for logic building I hope you it will help you to improve your logic building skill, do share this tutorial with your friends who might benefit from this article.

**Here are some more python programs tutorials for you:**

**Python Programs For Class 12 Practicals.****Python Program For Employee Salary.****Python Program For Email Header Analyzer.**

I hope you found what you were looking for from this tutorial, and if you want more python programs and tutorials like this, do join our **Telegram channel** for future updates.

Thanks for reading, have a nice day 🙂