Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Python: triangle of stars partten proramming.

 Question:

write a program in python to print the triangle of stars.

Answer:

=====Code  =========

num=int(input("Enter the number ::"))
i=1
for i in range(num):
    for j in range(i+1):
        print("* " , end = ' ')
    print(" ")

 

====== output is ======

HP-Notebook:~$ python3 pyramid.py

Enter the number :: 5
*   
*  *   
*  *  *   
*  *  *  *   
*  *  *  *  *   

Description:

The num is a variable which stores the value input if user.  

num=int(input("Enter the number ::"))

we have applied two loops. 

- First loop: Normal loop statement

- Second Loop: Nested Loop statement(loop inside the loop).

First loop:

First Loop will tell that how many rows will be there in the triangle.  the iteration of this loop will change the line for printing the stars  as shown in the output.

Second Loop:

Second loop will tell that how many starts will be print in single line ( one iteration of first loop ).

At the end normal print function used for change the line. it is under the first loop.

print(" ")

 

Python: Write a program to find the reverse of given number.

 

Question: 
Write a program in python to find the reverse of given number.
 
Answer:
 
num=int(input("Enter the number:"))     # At this point we have taken an integer value from console.
 
 rev=(rev*10+num%10)         
# At this point we are finding the last number of integer stored in the "num" and that last number is added in previous 10th place of  "rev"(rev*10).
 
num=num//10      
# At this point last digit will be removed from number. "//" removed remainder from the number .
                               
code
---------------------------------------------------
num=int(input("Enter the number:"))
rev=0
while(num>0):
    rev=(rev*10+num%10)
    num=num//10
print("reverse =",rev)
 
---------------------------------------------------  

Output is:
 
Enter the number:98765123
reverse= 32156789

********************************************
Above code can also be write as below in simplest way:

---------------------------------------------------------
num=int(input("Enter the number:"))
reverse = 0
remainder = 0
while(num>0):
    remainder = num%10
    reverse = reverse*10 + remainder
    num = num//10

print("reverse=",reverse)
 
---------------------------------------------------------

Note:

you could run the above commands on online compiler. go to the below link and copy pest the code there and play with code to understand.

Link: https://www.programiz.com/python-programming/online-compiler/

Python: Python-Programming Exercise

 

Python: Python-Programming Exercise

In this section, we have placed the list of Python programs for doing some basic practices for beginners and experienced. solutions are also provided. please click the link shared below under each questions.

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

1-  Write a program to find the Fibonacci series with certain number of count.

Answer: please click the below link.

Result:

Enter the value of 'n': 10
0

1

1

2

3

5

8

13

21

34

Link: https://writeprogramin.blogspot.com/2021/04/python-fibonacci-sequence.html


2- Write a program to find the reverse of given number.

Answer: please click the below link.

Link: https://writeprogramin.blogspot.com/2021/04/python-write-program-to-find-reverse-of.html


3- write a program in python to print the triangle of stars.

Answer: please click the below link.

Enter the number :: 5
*   
*  *   
*  *  *   
*  *  *  *   
*  *  *  *  *  

Link:https://writeprogramin.blogspot.com/2021/04/python-triangle-of-stars-partten.html


Answer: please click the below link.

Link: 


5- 

Answer: please click the below link.

Link: 




Python: Fibonacci sequence

 Fibonacci sequence:

Here we will look, that how to find the Fibonacci series/ Fibonacci sequence is  for certain given number.

1- Below line will take input from console. For type conversion we have placed "int" at starting point. so that it will store an integer number in "getNumFromConsole" variable.

getNumFromConsole = int(input("Enter the value of 'n': "))
 
If "int" is removed from above line then it will take string only.

After that we have taken some variables and assign some initial values. these initial values will be use in use when first iteration of loop will be executed. 
 

in below code under loop, we have just swapped the values of variable a and b. when first iteration of loop will execute, then all initial value will be in use and calculate the sum.

when second iteration of loop will execute, then sum will print and last stored value of b moved to variable a and value of sum will move to variable b, after that sum will be calculated as per the assign value.

 while(count < getNumFromConsole):
    print(sum)
    count = count + 1                    #increment loop count
    a = b
    b = sum
    sum = a + b 

 

Below table shows loop iteration how swapping of value is getting placed at every loop iteration.




Print SumVariable a Variable bVariable sum = a+bLoop_Iteration
0 0 1 1 Loop_1st Iteration All initial value is taken
1 0 1 1 Loop_2nd Iteration
1 1 1 2 Loop_3th  Iteration
2 1 2 3 Loop_4th  Iteration
3 2 3 5 Loop_5th  Iteration
5 3 5 8 Loop_6th  Iteration
8 5 8 13 Loop_7th  Iteration
13 8 13 21 Loop_8th  Iteration
21 13 21 34 Loop_9th  Iteration
34 21 34 55 Loop_10th  Iteration

 
 
 
Code:
============================


# python/bin/ 

getNumFromConsole = int(input("Enter the value of 'n': "))

a = 0                 #First variable 

b = 1                 #Second variable 

sum = 0 

count = 0                     #For the use of loop initial point. 

print("Fibonacci Series: ") 

while(count < getNumFromConsole):
    print(sum)
    count = count + 1                    #increment loop count
    a = b
    b = sum
    sum = a + b

==========================

Result:

Enter the value of 'n': 10


0

1

1

2

3

5

8

13

21

34