o Get two numbers and a choice from user o Call the respective functions with

O get two numbers and a choice from user o call the

This preview shows page 65 - 72 out of 118 pages.

o Get two numbers and a choice from user o Call the respective functions with user given number as parameter using switch statement o Print the result from addition(), subtraction, division(), multiplication(). • In user defined functions: o Plus and Minus function get two interger values and return interger. o Multiply and Divide functions get two interge values and return float. SOLUTION: #include <iostream> using namespace std; int add(int a,int b); int sub(int a,int b); float div(int a,int b); float mult(int a,int b); int main() { int a,b,choice,c,d; float e,f; cout<<"Enter 2 Integer Values="; cin>>a>>b; cout<<"Press 1 For Addition Of Numbers="<<endl; cout<<"Press 2 For Subtraction Of Numbers="<<endl; cout<<"Press 3 For Multiplication Of Numbers="<<endl; cout<<"Press 4 For Division Of Numbers="<<endl; cin>>choice; cout<<endl; switch(choice) { case 1: c=add(a,b); cout<<"Addition:"<<c; break; case 2:
Image of page 65
d=sub(a,b); cout<<"Subtraction:"<<d; break; case 3: e=mult(a,b); cout<<"Multiplication:"<<e; break; case 4: f=div(a,b); cout<<"Division:"<<f; break; default: cout<<"Invalid Choice"; } } int add(int a,int b ) { a+b; return a+b; } int sub(int a,int b ) { a-b; return a-b; } float mult(int a,int b ) { a*b; return a*b; } float div(int a,int b ) { a/b; return a/b; }
Image of page 66
OUTPUT:
Image of page 67
Task # 4 Write a C++ Program that calculates the power of a base by a user defined function as follows: • Take the power and the base from the user in the main function. Calcuate the power of the base in a user defined function “MY_POWER” by passing power and base from the main ( ) to the MY_POWER function. • Calculated value must return from the function to the main and display there. Print the result. SOLUTION: #include <iostream> #include<math.h> using namespace std; int MY_POWER(int a,int b); int main() { int b,p,y; cout<<"Enter Any Base="; cin>>b; cout<<"Enter Power="; cin>>p; y=MY_POWER(b,p); cout<<"Result For Power "<<p<<" Of Base "<<b<<"="<<y; } int MY_POWER(int a,int b) { int p; p=pow(a,b); return p; } OUTPUT:
Image of page 68
CSL 113 –Computer Programming Lab BSCS 1-B - SPRING 2018 Lab # 08 Function-II STUDENT NAME: Hamza Ansari STUDENT ENROLLMENT: 02-134181-081 COURSE INSTRUCTOR: Amna Iftikhar LAB ENGINEER: Tarwan Kumar DEPARTMENT OF COMPUTER SCIENCE BAHRIA UNIVERSITY, KARACHI CAMPUS.
Image of page 69
Task # 1 Write a C++ Program that calculates the power of a base by a user defined function as follows: • Take the power and the base from the user in the main function. • Calcuate the power of the base in a user defined function “MY_POWER” by passing power and base from the main ( ) to the MY_POWER function. • Calculated value must return from the function to the main and display there. Print the result SOLUTION: #include<iostream> #include<cmath> using namespace std; int MY_POWER(int &a,int &b) { int temp=1; for(int i=0;i<b;i++) { temp=temp*a; } return temp; } int main() { int b,p,; cout<<"Enter Base: "; cin>>b; cout<<"Enter Power: "; cin>>p; cout<<"Result Is: "<<MY_POWER(b,p); } OUTPUT:
Image of page 70
Task # 2 Write a C++ Program that perform following task. • Generate three random numbers ranged from 1 to 100.
Image of page 71
Image of page 72

You've reached the end of your free preview.

Want to read all 118 pages?

  • Fall '19

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture