asked 97.4k views
5 votes
Give an example of an operationin a programming languagethat is implemented.

i) Directly

ii) As a subprogram

iii) As an inline code

1 Answer

2 votes

answer:

(i) #include <iostream>

using namespace std;

int main() {

int n1,n2,max;

cin>>n1>>n2;

if(n1>n2)//finding maximum between n1 and n2 in the program directly.

max=n1;

else

max=n2;

cout<<max;

return 0;

}

(ii)

#include <iostream>

using namespace std;

int maximum(int n1 ,int n2)

{

if(n1>n2)//finding maximum between n1 and n2 using function.

return n1;

else

return n2;

}

int main() {

int n1,n2,max;

cin>>n1>>n2;

max=maximum(n1,n2);

cout<<max;

return 0;

}

(iii)

#include <iostream>

using namespace std;

inline int maximum(int n1 ,int n2)

{

return (n1>n2)? n1:n2;

}

int main() {

int n1,n2,max;

cin>>n1>>n2;

max=maximum(n1,n2);//Finding maximum using inline function.

cout<<max;

return 0;

}

Output:-

54 78

78

all three codes give the same output.

Explanation:

In part (i) I have done the operation in the main function directly.

In part(ii) I have used function to calculate the maximum.

In part(iii) I have used inline function to calculate maximum.

answered
User Hasparus
by
7.9k points