Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.

Similar presentations


Presentation on theme: "Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout."— Presentation transcript:

1 Lecture 4 Function example

2 Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout << max (x, y); }

3 Example 2 Write C++ program to calculate the squared value of a number passed from main function. Use this function in a program to calculate the squares of numbers from 1 to 10: #include int square ( int y ) { int z; z = y * y; return ( z ); } void main( ) { ………………………………….. }

4 Example 3 Write C++ program using function to calculate the average of two numbers entered by the user in the main program: #include …………… averge (………….…….) { …………………… } void main( ) { float x; // x is the varible for average int num1,num2; cout << "Enter 2 positive number \n"; cin >> num1 >> num2; x = ……………………………….. cout << x; }

5 Example 4 Write C++ program, using function, to find the summation of the following series: #include int summation ( int x) { } void main ( ) { int n,s; cout << "enter positive number"; cin >> n; s = summation ( n ); cout << "sum is: " << s << endl; }

6 Example 5 Write a C++ program, using function, to convert a given temperature radian in a degrees Fahrenheit (F) to degrees Celsius (C) using the formula: C = 5 / 9 ( F - 32 )


Download ppt "Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout."

Similar presentations


Ads by Google