Skip to content

Commit 4ecd2fa

Browse files
committed
added more problem - Prime numbers related
1 parent c9dbe85 commit 4ecd2fa

14 files changed

+631
-11
lines changed

Check-Prime/Prime-Numbers.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## 7.1 Check Prime:
2+
3+
### S-1: The problem
4+
For a given number, check whether the number is a prime number or not.
5+
6+
### S-2: Hint
7+
A number is a prime number. If that number is only divisible by 1 and the number itself.
8+
9+
This means a prime number is not divisible by any numbers between 1 and the number itself.
10+
11+
So, to check prime, you can start dividing the number from 2. And then increase it by 1. If the number gets divided, then it’s not a prime number.
12+
13+
### S-3: The solution
14+
15+
```python
16+
def is_prime(num):
17+
for i in range(2,num):
18+
if (num % i) == 0:
19+
return False
20+
return True
21+
22+
23+
num = int(input("Enter a number: "))
24+
25+
check_prime = is_prime(num)
26+
27+
if check_prime:
28+
print('Your number is a Prime')
29+
else:
30+
print('Your number is not a Prime')
31+
```
32+
33+
[try it: button]
34+
35+
### S-4: Explanation
36+
The core part of the algorithm is the is_prime function.
37+
38+
There we start a for loop the range(2, num). Because we want to know that the number is not divisible by any number except 1 or the number itself.
39+
40+
For example, 29. It’s only divisible by 1 or 29.
41+
42+
So, to test whether the number is divisible by any number greater than 1. We started the for loop from 2. And then going to before the num variable.
43+
44+
To check the number divisible, we check the remainder to be equal to 0. If the number is gets divided, the remainder will be 0.
45+
46+
So, if the number gets divided by, it’s not a prime number. That’s why we will return False.
47+
48+
If the number doesn’t get divided by any numbers smaller than the number, it won’t go inside the if block. It will finish all the numbers up to the num.
49+
50+
Then we will return True. Because it didn’t get divided, by any numbers. Hence, it will be a prime number.
51+
52+
### S-5: Many Solution
53+
There are other solutions to this problem as well. We encourage you to google, “check prime number python”. In that way, you will learn a lot.
54+
55+
### S-6: Take Away
56+
A prime number is only divisible by 1 and the number itself.
57+
58+
 
59+
[![Next Page](assets/next-button.png)](..README.md)
60+
 
61+
62+
###### tags: `programmig-hero` `python` `float` `int` `math`

Computations/Calculate-Grades.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## 6.3 Calculate grades
2+
3+
### S-1: The problem
4+
Calculate grade of five subjects.
5+
6+
### S-2: Hint
7+
So, you have to take five inputs. These will be the marks of five subjects. Then, create the average.
8+
9+
Once you have the average. It just running an if-else. And decide the grade.
10+
11+
### S-3: The Solution
12+
```python
13+
print('Enter your marks:')
14+
sub1=int(input("First subject: "))
15+
sub2=int(input("Second subject: "))
16+
sub3=int(input("Third subject: "))
17+
sub4=int(input("Fourth subject: "))
18+
sub5=int(input("Fifth subject: "))
19+
20+
avg=(sub1+sub2+sub3+sub4+sub4)/5
21+
22+
if avg >= 90:
23+
print("Grade: A")
24+
elif avg >= 80:
25+
print("Grade: B")
26+
elif avg >= 70:
27+
print("Grade: C")
28+
elif avg >=60:
29+
print("Grade: D")
30+
else:
31+
print("Grade: F")
32+
```
33+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
34+
35+
### S-4: Explanation
36+
Calculation of average is easy. Add them all and then divide by the count. As we are taking numbers for 5 subjects we are dividing the total by 5.
37+
38+
After that, we are running a simple if-else to determine the grade.
39+
40+
 
41+
[![Next Page](assets/next-button.png)](Gravitational-Force.md)
42+
 
43+
44+
###### tags: `programmig-hero` `python` `float` `int` `math`

Computations/Complex-Interest.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## 6.2: Compound Interest
2+
3+
### S-1: The Problem
4+
Take money borrowed, interest and duration as input. Then, compute the compound interest rate.
5+
6+
### S-2: Hint
7+
Compound interest formula is:
8+
9+
A = P(1+r/100)t
10+
11+
Here, P is the principal amount; it is the amount that you borrowed. r is the interest rate in percentage and t is the time.
12+
13+
### S-3: Solution
14+
15+
```python
16+
def compound_interest(principle, rate, time):
17+
interest = principle * ((1 + rate / 100) ** time)
18+
return interest
19+
20+
principle = int(input("Money you borrowed: "))
21+
interest_rate = float(input("Interest Rate: "))
22+
time = float(input("Overall Duration: "))
23+
24+
total_due = compound_interest(principle, interest_rate, time)
25+
26+
print("Interest Amount is:", total_due)
27+
```
28+
29+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
30+
31+
### S-4: Explanation
32+
Inside the function, just look into the power calculation.
33+
34+
**(1 + rate / 100) time**
35+
36+
Rest of the part should be easy for you.
37+
38+
### S-5: Take Away
39+
To apply the same power on multiple things, put them inside parentheses and then apply the power.
40+
41+
42+
 
43+
[![Next Page](assets/next-button.png)](Calculate-Grades.md)
44+
 
45+
46+
###### tags: `programmig-hero` `python` `float` `int` `math`

Computations/Gravitational-Force.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## 6.4 Gravitational Force
2+
3+
### S-1: The Problem
4+
Compute gravitational force between two objects.
5+
6+
### S-2: Hint
7+
The formula for gravitational force is
8+
9+
**F = G m1m2/r2**
10+
11+
Here G is the gravitational constant. Its value is 6.673*10-11
12+
13+
So, take three input from the users. The mass of the first object, the mass of the second object and the distance between them.
14+
15+
### S-3: Solution
16+
17+
```python
18+
mass1 = float(input("First mass: "))
19+
mass2 = float(input("Second mass: "))
20+
21+
r = float(input("Distance between the objects: "))
22+
23+
G = 6.673*(10**-11)
24+
force =(G*mass1*mass2)/(r**2)
25+
26+
print("The gravitational force is:", round(force, 5),"N")
27+
```
28+
29+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
30+
31+
### S-4: Explanation
32+
The calculation is simple. Only two things need to be learned from this.
33+
34+
**Have a look, how we wrote 6.673*10-11**
35+
36+
> Also, note that while displaying the force, we used a round function. In the round function, we passed the force, the variable we want to display. Then we passed another parameter to tell, how many decimal points we want to display.
37+
38+
39+
 
40+
[![Next Page](assets/next-button.png)](Triangle-Area.md)
41+
 
42+
43+
###### tags: `programmig-hero` `python` `float` `int` `math`

Computations/Simple-Interest.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## 6.1: Simple Interest
2+
3+
---
4+
5+
### S-1: The Problem
6+
You borrowed $5000 for 2 years with 2% interest per year.
7+
Calculate the simple interest to know how much you have to pay?
8+
9+
### S-2: Hint
10+
Just take amount, duration and interest rate.
11+
12+
You have to multiply these three. And, don’t forget: you have to convert percent to a fraction by dividing it by 100.
13+
14+
### S-3: The Solution
15+
16+
```python
17+
principle = int(input("Money you borrowed: "))
18+
interest_rate = float(input("Interest Rate: "))
19+
time = float(input("Overall Duration: "))
20+
21+
# Calculates simple interest
22+
simple_interest = principle * (interest_rate/100) * time
23+
24+
print("Simple interest is:", simple_interest)
25+
```
26+
27+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
28+
29+
### S-4: Explanation
30+
Read the code. I think you don’t need any extra explanation here.
31+
32+
 
33+
[![Next Page](assets/next-button.png)](Math-Power.md)
34+
 
35+
36+
###### tags: `programmig-hero` `python` `float` `int` `math`
37+
38+
 
39+
[![Next Page](assets/next-button.png)](Complex-Interest.md)
40+
 
41+
42+
###### tags: `programmig-hero` `python` `float` `int` `math`

Computations/Triangle-Area.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## 6.5 Triangle Area
2+
---
3+
4+
### S-1: The Problem
5+
Take three sides of a triangle. And then calculate the area of the triangle.
6+
7+
### S-2: How it works
8+
To calculate the area of the triangle. First, calculate the half of the perimeter. Here perimeter is the sum of each side of the triangle.
9+
10+
Let’s call it s.
11+
12+
Then you have to perform square root of the formula like below-
13+
14+
```python
15+
s = (a+b+c)/2
16+
area = √(s(s-a)*(s-b)*(s-c))
17+
```
18+
19+
### S-3: the code
20+
```python
21+
import math
22+
23+
a = float(input('Enter first side: '))
24+
b = float(input('Enter second side: '))
25+
c = float(input('Enter third side: '))
26+
27+
# calculate the semi-perimeter
28+
s = (a + b + c) / 2
29+
30+
# calculate the area
31+
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
32+
print('Area of your triangle is ', area)
33+
```
34+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
35+
36+
### S-4: Explanation
37+
To calculate the square root. We used the math module. And call math.sqrt.
38+
39+
```python
40+
import math
41+
print ( math.sqrt(4) )
42+
```
43+
44+
This will give you 2 as output.
45+
46+
Similarly, math.sqrt(25) will give 5 as output.
47+
48+
This is something new you have learned this time.
49+
50+
### S-5: Quiz
51+
52+
1. How would you calculate the square root of a number.
53+
2. Use math.square.root
54+
3. Use math.sqroot
55+
4. Use math.sqrt
56+
57+
**The answer is: 3**
58+
59+
### S-6: Take Away
60+
The math module has a lot of math-related functionalities.
61+
62+
63+
 
64+
[![Next Page](assets/next-button.png)](..README.md)
65+
 
66+
67+
###### tags: `programmig-hero` `python` `float` `int` `math`

Conversions/Celsius-to-Fahrenheit.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## 4.2 Celsius to Fahrenheit
2+
3+
### S-1: The problem
4+
Take the temperature in degrees Celsius and convert it to Fahrenheit.
5+
6+
<details>
7+
<summary><b>S-2: Click Here For Show Hints</b></summary>
8+
<p>To convert degrees Celsius temperature to Fahrenheit, you have to multiply by 9 and divide by 5.
9+
10+
And then, add 32.</p>
11+
</details>
12+
13+
Think for a second...How will you multiply a variable by 9 and then divide by 5? and then add 32. Can you do it without looking at the solution?
14+
15+
### S-3: The solution
16+
```python
17+
celsius = float(input("Enter temperature in degrees Celsius: "))
18+
19+
fahrenheit = celsius*9/5+32
20+
21+
print("Temperature in Fahrenheit:", fahrenheit)
22+
```
23+
24+
**[Try It:](https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
25+
26+
&nbsp;
27+
[![Next Page](../assets/next-button.png)](../README.md)
28+
&nbsp;
29+
30+
###### tags: `programmig-hero` `python` `float` `int` `math`
31+
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## 4.4: Decimal to Binary (recursive)
2+
3+
### S-1: The Problem
4+
Convert a decimal number to binary number using a recursive function.
5+
<details>
6+
<summary><b>S-3: Click Here For Show Hints</b></summary>
7+
<p>After coding for a while, recursive will become fun. Until then, recursive functions might feel like confusing magic.
8+
9+
So, don’t worry if you felt confused. You are not alone. I am in the same condition as well.</p>
10+
</details>
11+
12+
### S-3: Recursive
13+
14+
```python=
15+
def dec_to_binary(n):
16+
if n > 1:
17+
dec_to_binary(n//2)
18+
print(n % 2,end = '')
19+
```
20+
21+
### decimal number
22+
```python=
23+
num = int(input("Your decimal number: "))
24+
dec_to_binary(num)
25+
print(" ")
26+
```
27+
28+
**[Try It:](/https://play.google.com/store/apps/details?id=com.learnprogramming.codecamp)**
29+
30+
### S-4: Explanation
31+
The core part of the logic is simple. If the number is greater than 1, call the dec_to_binary function again. And, while calling, send the result of dividing operation as the input number.
32+
33+
If you remember, the while loop in the previous code problem is similar. In the while loop, we were going back to the next iteration with n = n//2
34+
35+
Here we are calling the same function with the n//2
36+
37+
In the previous code problem (iterative), we are putting the remainder in a list. Here, we are printing it right away.
38+
39+
While printing, we have one extra thing called end=''.
40+
The purpose of end='' is to print the output in the same line. If you don’t add end='', every print output will be displayed in a new line.
41+
42+
43+
### S-6: Take Away
44+
There are multiple ways to format the print string. Google it, when needed.
45+
46+
47+
&nbsp;
48+
[![Next Page](../assets/next-button.png)](../README.md)
49+
&nbsp;
50+
51+
###### tags: `programmig-hero` `python` `float` `int` `math`

0 commit comments

Comments
 (0)