Home | | Computer Science 12th Std | Python program Executing C++ Program using control statement

Chapter: 12th Computer Science : Chapter 14 : Integrating Python with MySql and C++ : Importing C++ Programs In Python

Python program Executing C++ Program using control statement

Now let us write a Python program to read a C++ coding and execute its result.

Python program Executing C++ Program using control statement

Now let us write a Python program to read a C++ coding and execute its result. The steps for executing the C++ program to check a given number is palindrome or not is given below




Example:- 14.7.1 - Write a C++ program to enter any number and check whether the number is palindrome or not using while loop.

/*. To check whether the number is palindrome or not using while loop.*/

//Now select File->New in Notepad and type the C++ program

#include <iostream>

using namespace std;

int main()

{

int n, num, digit, rev = 0;

cout<< "Enter a positive number: ";

cin>>num;

n = num;

while(num)

{

digit = num % 10;

rev = (rev * 10) + digit;

num = num / 10;   }

cout<< " The reverse of the number is: " << rev <<endl;

if (n == rev)

cout<< " The number is a palindrome";

else

cout<< " The number is not a palindrome";

return 0;

}

// Save this file as pali_cpp.cpp

 

#Now select File→New in Notepad and type the Python program

#Save the File as pali.py . Program that compiles and executes a .cpp file

#Python c:\pyprg\pali.py -i c:\pyprg\pali_cpp

import sys, os, getopt

def main(argv):

cpp_file = ''

exe_file = ''

opts, args = getopt.getopt(argv, "i:",['ifile='])

for o, a in opts:

if o in ("-i", "--ifile"):

cpp_file = a + '.cpp'

exe_file = a + '.exe'

run(cpp_file, exe_file)

def run(cpp_file, exe_file):

print("Compiling " + cpp_file)

os.system('g++ ' + cpp_file + ' -o ' + exe_file)

print("Running " + exe_file)

print("-----------------------")

print

os.system(exe_file)

print

if __name__ =='__main__':

main(sys.argv[1:])

 

Output of the above program

C:\Program Files\OpenOffice 4\program>Python c:\pyprg\pali.py -i c:\pyprg\pali_cpp

Compiling c:\pyprg\pali_cpp.cpp

Running c:\pyprg\pali_cpp.exe

--------------------------------------

Enter a positive number: 56765

The reverse of the number is: 56765

The number is a palindrome

-----------------------------------------------

C:\Program Files\OpenOffice 4\program>Python c:\pyprg\pali.py -i c:\pyprg\pali_cpp

Compiling c:\pyprg\pali_cpp.cpp

Running c:\pyprg\pali_cpp.exe

--------------------------------------

Enter a positive number: 56756

The reverse of the number is: 65765

The number is not a palindrome

-----------------------------------------------


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 14 : Integrating Python with MySql and C++ : Importing C++ Programs In Python : Python program Executing C++ Program using control statement |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.