What's new

Help Pa convert po ng C++ to Python

Status
Not open for further replies.

Jameskrt

Forum Veteran
Elite
Joined
Aug 29, 2017
Posts
1,853
Solutions
3
Reaction
509
Points
630
#include <iostream>
using namespace std;

int main()
{
long int n, i, t = 9;
int sum = 0;
cout << "\n\n Display the sum of the series [ 9 + 99 + 999 + 9999 ...]\n";
cout << "-------------------------------------------------------------\n";
cout << " Input number of terms: ";
cin >> n;

for (i = 1; i <= n; i++)
{
sum += t;
cout << t << " ";
t = t * 10 + 9;
}
cout << "\n The sum of the sarise = " << sum << endl;
}
 
tama kaya to? hahah testing di ko masyado alam python

print('\n\n Display the sum of the series [ 9 + 99 + 999 + 9999 ...]')
print(' -------------------------------------------------------------\n')
num = int(input("Input number of terms: "))
sum = 0
k = 9
for i in range(0,num,1):
sum=sum*10+9

print(sum,end=" + ")
print()
 
Python:
# SOLUTION 1: Print the series with space separator like in your example.
def solution1():
    print('Display the sum of the series [ 9 + 99 + 999 + 9999 ...]\n')
    print('---------------------------------------------------------\n')
    t = 9
    s = 0
    n = int(input('Input number of terms: '))
    for _ in range(n):
        s += t
        print(t, end=' ')
        t = t * 10 + 9
    print(f'\nThe sum of the series = {s}')

OUTPUT:
Code:
Input number of terms: 5
9 99 999 9999 99999
The sum of the sarise = 111105

Python:
# SOLUTION 2: Print the series inside the loop with + separator
def solution2():
    print('Display the sum of the series [ 9 + 99 + 999 + 9999 ...]\n')
    print('---------------------------------------------------------\n')

    t = 9
    s = 0
    n = int(input('Input number of terms: '))

    for _ in range(n):
        s += t
        print(t, end=' + ')
        t = t * 10 + 9

    print('\b\b ')  # remove the last (+) separator
    print(f'\nThe sum of the series = {s}')

Python:
# SOLUTION 3: Add the series to list then print outside the loop with + separator (mas Pythonic way than the solution 2)
def solution3():
    print('Display the sum of the series [ 9 + 99 + 999 + 9999 ...]\n')
    print('---------------------------------------------------------\n')
    t = 9
    s = 0
    n = int(input('Input number of terms: '))
    series = []
    for _ in range(n):
        s += t
        series.append(t)
        t = t * 10 + 9
    print(*series, sep=' + ')
    print(f'\nThe sum of the series = {s}')

SOLUTION 2 AND 3 OUTPUT:
Code:
Input number of terms: 5
9 + 99 + 999 + 9999 + 99999

The sum of the series = 111105
 
Last edited:
Python:
# SOLUTION 1: Print the series with space separator like in your example.
def solution1():
    print('Display the sum of the series [ 9 + 99 + 999 + 9999 ...]\n')
    print('---------------------------------------------------------\n')
    t = 9
    s = 0
    n = int(input('Input number of terms: '))
    for _ in range(n):
        s += t
        print(t, end=' ')
        t = t * 10 + 9
    print(f'\nThe sum of the series = {s}')

OUTPUT:
Code:
Input number of terms: 5
9 99 999 9999 99999
The sum of the sarise = 111105

Python:
# SOLUTION 2: Print the series inside the loop with + separator
def solution2():
    print('Display the sum of the series [ 9 + 99 + 999 + 9999 ...]\n')
    print('---------------------------------------------------------\n')

    t = 9
    s = 0
    n = int(input('Input number of terms: '))

    for _ in range(n):
        s += t
        print(t, end=' + ')
        t = t * 10 + 9

    print('\b\b ')  # remove the last (+) separator
    print(f'\nThe sum of the series = {s}')

Python:
# SOLUTION 3: Add the series to list then print outside the loop with + separator (mas Pythonic way than the solution 2)
def solution3():
    print('Display the sum of the series [ 9 + 99 + 999 + 9999 ...]\n')
    print('---------------------------------------------------------\n')
    t = 9
    s = 0
    n = int(input('Input number of terms: '))
    series = []
    for _ in range(n):
        s += t
        series.append(t)
        t = t * 10 + 9
    print(*series, sep=' + ')
    print(f'\nThe sum of the series = {s}')

SOLUTION 2 AND 3 OUTPUT:
Code:
Input number of terms: 5
9 + 99 + 999 + 9999 + 99999

The sum of the series = 111105
Salamat po!
 
Status
Not open for further replies.

Similar threads

Back
Top