0

Can someone help me convert below c++ to codes into C++ function???

#include <iostream> using namespace std; int main() { int num1, num2; cout<<"enter two integers"<<endl; cin>> num1 >> num2; int sum; sum = num1 + num2; cout<< num1 <<" + "<< num2 <<" = "<<sum<<endl; int sub; sub = num1 - num2; cout<< num1 <<" - "<< num2 <<" = "<<sub<<endl; int mult; mult = num1 * num2; cout<< num1 <<" * "<< num2 <<" = "<< mult<<endl; int div; div = num1 / num2; cout<< num1 <<" / "<< num2 <<" = "<<div<<endl; int mod; mod = num1 % num2; cout<< num1 <<" % "<< num2 <<" = "<< mod<<endl; return 0; }

16th Jan 2023, 4:49 PM
Al-hassan Rabi'u
Al-hassan Rabi'u - avatar
2 Answers
+ 1
#include <iostream> using namespace std; void sum(int num1, int num2) { int sum; sum = num1 + num2; cout << num1 << " + " << num2 << " = " << sum << endl; } void sub(int num1, int num2) { int sub; sub = num1 - num2; cout<< num1 <<" - "<< num2 <<" = "<<sub<<endl; } void mult(int num1, int num2) { int mult; mult = num1 * num2; cout<< num1 <<" * "<< num2 <<" = "<< mult<<endl; } void dive(int num1, int num2) { int div; div = num1 / num2; cout<< num1 <<" / "<< num2 <<" = "<<div<<endl; } void mod(int num1, int num2) { int mod; mod = num1 % num2; cout<< num1 <<" % "<< num2 <<" = "<< mod<<endl; } int main() { int num1, num2; cout<<"enter two integers"<<endl; cin>> num1 >> num2; sum(num1,num2); sub(num1,num2); mult(num1,num2); div(num1,num2); mod(num1,num2); return 0; } if you need them to return a value, then you need: int sum (int a, int b) { return a + b; }
16th Jan 2023, 5:22 PM
Kirill Ovchinnikov
Kirill Ovchinnikov - avatar
+ 1
Yes I would like them to return value
16th Jan 2023, 5:35 PM
Al-hassan Rabi'u
Al-hassan Rabi'u - avatar