{"id":8599,"date":"2021-09-30T10:30:21","date_gmt":"2021-09-30T05:00:21","guid":{"rendered":"https:\/\/python-programs.com\/?p=8599"},"modified":"2021-11-22T18:39:24","modified_gmt":"2021-11-22T13:09:24","slug":"program-to-read-a-number-n-and-compute-n-nn-nnn-in-cpp-and-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/program-to-read-a-number-n-and-compute-n-nn-nnn-in-cpp-and-python\/","title":{"rendered":"Program to Read a Number n and Compute n+nn+nnn in C++ and Python"},"content":{"rendered":"
In the previous article, we have discussed about Program to Clear the Rightmost Set Bit of a Number in C++ and Python<\/a>. Let us learn Program to Read a Number n and Compute n+nn+nnn in C++ Program and Python.<\/p>\n Given a number n , the task is to calculate the value of n+ nn +nnn in C++ and Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Our website provided core java programs examples with output<\/a> aid beginners and expert coders to test their knowledge gap and learn accordingly.<\/p>\n There are several ways to calculate the value of n + nn + nnn in C++ and\u00a0 python some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\ngiven number = 8<\/pre>\n
The value of 8 + 88 + 888 = 984<\/pre>\n
given number = 4<\/pre>\n
Enter any random number = 4\r\nThe value of 4 + 44 + 444 = 492<\/pre>\n
given number = 9<\/pre>\n
The value of 9 + 99 + 999 = 1107<\/pre>\n
Program to Read a Number n and Compute n+nn+nnn in C++ and Python<\/h2>\n
\n
Method #1:Using String Concatenation (Static Input) in Python<\/h3>\n
\n
# given number numb\r\nnumb = 8\r\n# converting the given number to string\r\nstrnum = str(numb)\r\n# Add the string twice to concatenate it and store it in another variable.\r\nstrnum1 = strnum+strnum\r\n# Add the string thrice to concatenate it and store it in another variable.\r\nstrnum2 = strnum+strnum+strnum\r\n# converting the strnum1 and strnum2 from string to integer using int() function\r\nintnum1 = int(strnum1)\r\nintnum2 = int(strnum2)\r\n# Calculating the result value\r\nresultVal = numb+intnum1+intnum2\r\nprint(\"The value of\", strnum, \"+\", strnum1, \"+\", strnum2, \"=\", resultVal)\r\n<\/pre>\n
The value of 8 + 88 + 888 = 984<\/pre>\n
Method #2:Using String Concatenation (User Input) in Python<\/h3>\n
\n
# Scan the give number\r\nnumb = int(input(\"Enter any random number = \"))\r\n# converting the given number to string\r\nstrnum = str(numb)\r\n# Add the string twice to concatenate it and store it in another variable.\r\nstrnum1 = strnum+strnum\r\n# Add the string thrice to concatenate it and store it in another variable.\r\nstrnum2 = strnum+strnum+strnum\r\n# converting the strnum1 and strnum2 from string to integer using int() function\r\nintnum1 = int(strnum1)\r\nintnum2 = int(strnum2)\r\n# Calculating the result value\r\nresultVal = numb+intnum1+intnum2\r\nprint(\"The value of\", strnum, \"+\", strnum1, \"+\", strnum2, \"=\", resultVal)\r\n<\/pre>\n
Enter any random number = 4\r\nThe value of 4 + 44 + 444 = 492<\/pre>\n
Method #3:Using String Concatenation (Static Input) in C++<\/h3>\n
\n
#include <bits\/stdc++.h>\r\nusing namespace std;\r\nint main()\r\n{\r\n \/\/ given number numb\r\n int numb = 9;\r\n \/\/ converting the given number to string\r\n string strnum = to_string(numb);\r\n\r\n \/\/ Add the string twice to concatenate it and store it\r\n \/\/ in another variable.\r\n string strnum1 = strnum + strnum;\r\n \/\/ Add the string thrice to concatenate it and store it\r\n \/\/ in another variable.\r\n string strnum2 = strnum + strnum + strnum;\r\n \/\/ converting the strnum1 and strnum2 from string to\r\n \/\/ integer using stoi() function\r\n int intnum1 = stoi(strnum1);\r\n int intnum2 = stoi(strnum2);\r\n \/\/ Calculating the result value\r\n int resultVal = numb + intnum1 + intnum2;\r\n cout << \"The value of \" << strnum << \" + \" << strnum1\r\n << \" + \" << strnum2 << \" = \" << resultVal;\r\n return 0;\r\n}<\/pre>\n
The value of 9 + 99 + 999 = 1107<\/pre>\n
\n