Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Please need help with converting c++ code to python

Please need help with converting c++ code to python

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 676 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tenger
    wrote on last edited by VRonin
    #1

    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;
    }
    
    ODБOïO 1 Reply Last reply
    0
    • T Tenger

      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;
      }
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      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

      T 1 Reply Last reply
      6
      • ODБOïO 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

        T Offline
        T Offline
        Tenger
        wrote on last edited by
        #3

        Hi @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 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved