Please need help with converting c++ code to python
-
wrote on 14 Oct 2019, 09:09 last edited by VRonin
Hello, i have one work, that i must to complete in my colledge. We are working only on c++, but without any experience we must rewrite this code to pyton and i don't know how that's possible. Who can help me with this problem. I will be very happy and grateful if someone can convert this code:
#include <iostream> using namespace std; int main() { cout << "Noteikt skaitlja m pirmo n ciparu summu\n"; int n, m, sum=0; cout << "Ievadiet m un n veselos skaitlus\n"; cin >> m >> n; int mKopija = m, x=1; while ((mKopija/=10) > 0) x++; int c = x - n; if (c < 0) cout << "Nepietiek ciparu skaitlii" << endl; else{ for(int i=1; i<=c; i++) { m /= 10; } while (m > 0) { sum += m%10; m /= 10; } cout << "Summa:" << sum <<endl; sum=0; } return 0; }
-
Hello, i have one work, that i must to complete in my colledge. We are working only on c++, but without any experience we must rewrite this code to pyton and i don't know how that's possible. Who can help me with this problem. I will be very happy and grateful if someone can convert this code:
#include <iostream> using namespace std; int main() { cout << "Noteikt skaitlja m pirmo n ciparu summu\n"; int n, m, sum=0; cout << "Ievadiet m un n veselos skaitlus\n"; cin >> m >> n; int mKopija = m, x=1; while ((mKopija/=10) > 0) x++; int c = x - n; if (c < 0) cout << "Nepietiek ciparu skaitlii" << endl; else{ for(int i=1; i<=c; i++) { m /= 10; } while (m > 0) { sum += m%10; m /= 10; } cout << "Summa:" << sum <<endl; sum=0; } return 0; }
wrote on 14 Oct 2019, 11:01 last edited by ODБOïhi @Tenger
this is first time i write python code, i struggled a little bit because of the specific syntax, but the rest is very similar to c++disclaimer : i have 15 minute of Pyhton background, there may be an error in the code
print("Determine the sum of the first n digits of m\n") n = 0 m = 0 sum=0 print("Enter m and n integers\n") m = int(input("Type the Number (m) : ")) n = int(input("Type digit count (n) : ")) copy = m x = 1 while( copy > 0): x += 1 copy = copy / 10 c = x - n if c < 0 : print("Not enough digits\n") else : for j in range(c) : m /= 10 while(m>0) : sum += int(m%10) m/=10 print("Sum is %s" %int(sum))
note: your question is not really qt related
https://forum.qt.io/topic/107733/c-c-programming-homework-help
-
hi @Tenger
this is first time i write python code, i struggled a little bit because of the specific syntax, but the rest is very similar to c++disclaimer : i have 15 minute of Pyhton background, there may be an error in the code
print("Determine the sum of the first n digits of m\n") n = 0 m = 0 sum=0 print("Enter m and n integers\n") m = int(input("Type the Number (m) : ")) n = int(input("Type digit count (n) : ")) copy = m x = 1 while( copy > 0): x += 1 copy = copy / 10 c = x - n if c < 0 : print("Not enough digits\n") else : for j in range(c) : m /= 10 while(m>0) : sum += int(m%10) m/=10 print("Sum is %s" %int(sum))
note: your question is not really qt related
https://forum.qt.io/topic/107733/c-c-programming-homework-help
wrote on 14 Oct 2019, 15:06 last edited byHi @LeLev
It's so close working :D But anyway thank you, i'll try to rebuild something. And yes, sorry, i'm first time writting there and don't know where and what i must to write. Thank you and sorry! :D
1/3