Python n nn nnn nnnn: In the previous article, we have discussed about Program to Clear the Rightmost Set Bit of a Number in C++ and Python. Let us learn Program to Read a Number n and Compute n+nn+nnn in C++ Program and Python.
Given a number n , the task is to calculate the value of n+ nn +nnn in C++ and Python.
Examples:
Example1:
Input:
given number = 8
Output:
The value of 8 + 88 + 888 = 984
- Python Program to Compute the Value of Euler’s Number ,Using the Formula: e = 1 + 1/1! + 1/2! + …… 1/n!
- Python Program to Find Sum of Series 1^1/1+2^2/2+3^3/3…+n^n/n
- Python Program to Accept a Sentence and Print only the First Letter of Each Word in Capital Letters Separated by a Full Stop
Example2:
Input:
given number = 4
Output:
Enter any random number = 4 The value of 4 + 44 + 444 = 492
Example3:
Input:
given number = 9
Output:
The value of 9 + 99 + 999 = 1107
Program to Read a Number n and Compute n+nn+nnn in C++ and Python
b nn nnn: Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
There are several ways to calculate the value of n + nn + nnn in C++ and python some of them are:
- Using String Concatenation (Static Input) in Python
- Using String Concatenation (User Input) in Python
- Using String Concatenation (Static Input) in C++
Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.
Method #1:Using String Concatenation (Static Input) in Python
Approach:
- Give the input number as static.
- Convert the number to a string and save it in a different variable.
- Add the string twice to concatenate it and store it in another variable.
- Then multiply the string by three and assign the result to the third variable.
- Convert the second and third variables’ strings to integers.
- Add the values from all the integers together.
- Print the expression’s total value.
- Exit of program
Below is the implementation:
# given number numb numb = 8 # converting the given number to string strnum = str(numb) # Add the string twice to concatenate it and store it in another variable. strnum1 = strnum+strnum # Add the string thrice to concatenate it and store it in another variable. strnum2 = strnum+strnum+strnum # converting the strnum1 and strnum2 from string to integer using int() function intnum1 = int(strnum1) intnum2 = int(strnum2) # Calculating the result value resultVal = numb+intnum1+intnum2 print("The value of", strnum, "+", strnum1, "+", strnum2, "=", resultVal)
Output:
The value of 8 + 88 + 888 = 984
Method #2:Using String Concatenation (User Input) in Python
Approach:
- Scan the given number and store it in the numb variable.
- Convert the number to a string and save it in a different variable.
- Add the string twice to concatenate it and store it in another variable.
- Add the string thrice to concatenate it and store it in another variable.
- Convert the second and third variables strings to integers.
- Add the values from all the integers together.
- Print the expression’s total value.
- Exit of program
Below is the implementation:
# Scan the give number numb = int(input("Enter any random number = ")) # converting the given number to string strnum = str(numb) # Add the string twice to concatenate it and store it in another variable. strnum1 = strnum+strnum # Add the string thrice to concatenate it and store it in another variable. strnum2 = strnum+strnum+strnum # converting the strnum1 and strnum2 from string to integer using int() function intnum1 = int(strnum1) intnum2 = int(strnum2) # Calculating the result value resultVal = numb+intnum1+intnum2 print("The value of", strnum, "+", strnum1, "+", strnum2, "=", resultVal)
Output:
Enter any random number = 4 The value of 4 + 44 + 444 = 492
Method #3:Using String Concatenation (Static Input) in C++
Approach:
- Give the input number as static.
- Convert the number to a string using to_string() function in C++ and save it in a different variable.
- Add the string twice to concatenate it and store it in another variable.
- Add the string thrice to concatenate it and store it in another variable.
- Convert the second and third variables strings to integers using stoi() function.
- Add the values from all the integers together.
- Print the expression’s total value.
- Exit of program
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // given number numb int numb = 9; // converting the given number to string string strnum = to_string(numb); // Add the string twice to concatenate it and store it // in another variable. string strnum1 = strnum + strnum; // Add the string thrice to concatenate it and store it // in another variable. string strnum2 = strnum + strnum + strnum; // converting the strnum1 and strnum2 from string to // integer using stoi() function int intnum1 = stoi(strnum1); int intnum2 = stoi(strnum2); // Calculating the result value int resultVal = numb + intnum1 + intnum2; cout << "The value of " << strnum << " + " << strnum1 << " + " << strnum2 << " = " << resultVal; return 0; }
Output:
The value of 9 + 99 + 999 = 1107
Answer yourself:
- Read a number n and compute nnnnnn in python?
- Input an integer n and computes the value of nnnnnn?
- nnnnnnnn1000 n?
- How to print nnnnnn in python?
- Input an integer (n and computes the value of n+nn+nnn)?
- nnn+nn+n+n+n=1000 n=?
- How to print n+nn+nnn in python?
- Read a number n and compute n+nn+nnn in python?
- Calculate n + nn + nnn + … + n(n times) in c++?
Related Programs:
- Python Program to Read Two Numbers and Print Their Quotient and Remainder
- Python Program to Read a File and Capitalize the First Letter of Every Word in the File
- Python Program to Read a Text File and Print all the Numbers Present in the Text File
- Python Program to Read Print Prime Numbers in a Range using Sieve of Eratosthenes