Python n nn nnn nnnn – Program to Read a Number n and Compute n+nn+nnn in C++ and Python

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

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:

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

Related Programs: