Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
// Example program
#include <iostream>
#include <string>
using namespace std;

  void findMultiples( int i ,int n){
      int count=0;
  for( i ; i <= n; i++)
    if(i % 3 == 0 ){
    count++;
      cout << i<<"  ";
      if(count==5)
      cout<<endl;
 }
}

int="" main()="" {
="" int="" x;
="" y;
="" cin="">>x>>y;
  findMultiples(x,y);

  return 0;
}

<pre>// Example program
#include <iostream>
#include <string>
using namespace std;

  void findMultiples( int i ,int n){
      int count=0;
  for( i ; i <= n; i++)
    if(i % 3 == 0 ){
    count++;
      cout << i<<"  ";
      if(count==5)
      cout<<endl;
    }
}

int main() {
    int x;
    int y;
    cin>>x>>y;
  findMultiples(x,y);

  return 0;
}


What I have tried:

C++
// Example program
#include <iostream>
#include <string>
using namespace std;

  void findMultiples( int i ,int n){
      int count=0;
  for( i ; i <= n; i++)
    if(i % 3 == 0 ){
    count++;
      cout << i<<"  ";
      if(count==5)
      cout<<endl;
    }
}

int main() {
    int x;
    int y;
    cin>>x>>y;
  findMultiples(x,y);

  return 0;
}
Posted
Updated 31-Jan-23 21:34pm
v2
Comments
CHill60 11-Mar-21 12:32pm    
Both of those code samples are C++. What is the Python code that you have produced and what is the problem with it
Richard Deeming 11-Mar-21 12:37pm    
This is not a code conversion service.

There is nothing complicated in that C code, so it should not take long to convert. See The Python Tutorial — Python 3.7.10 documentation[^] for help.
 
Share this answer
 
Comments
Maciej Los 12-Mar-21 1:41am    
5ed!
CPallini 12-Mar-21 3:17am    
5.
Well...

I have no idea why do you want to convert C code to Python...

The main rule of CP QA forum is to use Google before you ask a question. So, use it!

I'd suggest to read this:
ctopy(1): quick/dirty C to Python translator) - Linux man page[^]
Calling C Functions from Python - JournalDev[^]

As Richard MacCutchan mentioned, if you understand what above code does, you can write in Python without any converter.
 
Share this answer
 
Comments
CPallini 12-Mar-21 3:17am    
5.
Maciej Los 12-Mar-21 3:32am    
Thank you, Carlo.
Member 2158094 11-Dec-21 4:06am    
Re "I have no idea why do you want to convert C code to Python":
Here's an idea: One has a code in C and he wants to convert it to Python! :))
Sometimes, it is better to clear understand the requirements and the original source code logic, in order to gain insight.

Sometimes, it is better to understand the requirements and the original source code logic, to perform a good conversion into the target language.

Sometimes, it is better to understand the requirements and the original source code logic, in order to throw that junk away and restart from scratch your program, using the target language.

Python
def find_multiples_of_3_in_range( first, last ):
  if last < first:
    return 0
  frem = first % 3
  lrem = last % 3

  if frem != 0:
   first += (3 - frem)

  last -= lrem

  return (last -first)//3 + 1



def main():
  first = int(input("insert the starting number of the range "))
  last = int(input("insert the last number of the range "))
  multiples = find_multiples_of_3_in_range(first, last)
  print("thre are {} multiples of 3 in the given range".format(multiples))

main()
 
Share this answer
 
Comments
Maciej Los 12-Mar-21 3:32am    
5ed!
CPallini 12-Mar-21 13:28pm    
Thank you!
Richard MacCutchan 12-Mar-21 6:00am    
+5. More brilliant code.
CPallini 12-Mar-21 13:29pm    
You are way to good, man. :-)
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900