Python Basic: Exercise-10 with Solution
Write a Python program that accepts an integer [n] and computes the value of n+nn+nnn.
Sample value of n is 5
Python int[x, base=10]:
The function returns an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, return x.__int__[]. For floating point numbers, this truncates towards zero.
- If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base
- The literal can be preceded by + or - [with no space in between] and surrounded by whitespace
- A base-n literal consists of the digits 0 to n-1, with a to z [or A to Z] having values 10 to 35. The default base is 10.
- The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code.
Pictorial Presentation:
Sample Solution :-
Python Code:
a = int[input["Input an integer : "]]
n1 = int[ "%s" % a ]
n2 = int[ "%s%s" % [a,a] ]
n3 = int[ "%s%s%s" % [a,a,a] ]
print [n1+n2+n3]
Sample Output:
615
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python program to display the examination schedule. [extract the date from exam_st_date].
Next: Write a Python program to print the documents [syntax, description etc.] of Python built-in function[s].
Python: Tips of the Day
Function With Multiple Outputs:
If a function is required to return multiple values then:
[Variable] AggregateFunction[[Value] for [item] in [collection]]
This is a Python Program to read a number n and compute n+nn+nnn. Problem Description
The program takes a number n and computes n+nn+nnn.
Problem Solution
1. Take the value of a element and store in a variable n.
2. Convert the integer into string and store it in another variable.
3. Add the string twice so the string gets
concatenated and store it in another variable.
4. Then add the string thrice and assign the value to the third variable.
5. Convert the strings in the second and third variables into integers.
6. Add the values in all the integers.
7. Print the total value of the expression.
8. Exit.
Program/Source Code
Here is the source code of the Python Program to read a number n and compute n+nn+nnn. The program output is also shown below.
n=int[input["Enter a number n: "]] temp=str[n] t1=temp+temp t2=temp+temp+temp comp=n+int[t1]+int[t2] print["The value is:",comp]
Program Explanation
1. User must first enter the value and store it in a variable n.
2. The integer is converted to string for concatenation of the value of n.
3. The string is then concatenated once and twice and stored in separate variables.
4. Later to find the total sum, the string is converted back to integer.
5. The total value of the expression is then printed.
Runtime Test Cases
Case 1: Enter a number n: 5 The value is: 615 Case 2: Enter a number n: 20 The value is: 204060
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Next Steps:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.