Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
1.6k views
in Importing C++ Programs in Python by (49.1k points)
closed by

Write a C++ program to find cube of a number. Write a python program to execute it?

1 Answer

+1 vote
by (54.8k points)
selected by
 
Best answer

/* Write a C++program using a user definedfunction to function cube of a number. */

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

#include<iostream>

using namespace std; 

//Function declaration

int cube(int num); int main( )

{

int num; int c;

cout<<“Enter any number: “<<end1;

cin>>num;

c = cube(num);

cout<<“Cube of’ <<num<<” is “<<c;

return 0;

}

//Function to find cube of any number

int cube(int num)

{

return (num * num * num);

}

//Save this file as cube_file.cpp

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

#Save the File as fun.py

#Program that compiles and executes a .cpp file 

#Python fun.py -i c:\pyprg\cuheJile.cpp

import sys, os, getopt

def main(argv):

cpp_Jile = ”

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” + cppfile)

os.system(‘g++’+ cpp file + ‘-o ‘+ exe file) 

print(“Running” + exe_file)

print(“.........“)

print

os. sy stem(exe_file)

print

if_name_== ’_main_’:

main(sys.argv[1:])

Output of the above program

Compiling c:\pyprg\cube_file.cpp

Running c:\pyprg\cube_file.exe

..............................

Enter any number:

5

Cube of 5 is 125

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

...