0% found this document useful (0 votes)
39 views

Functions: Write A C++ Program To Print Out The Following Using Functions and Compare It Without Functions

The document discusses C++ programs that use functions to perform various mathematical operations and comparisons in a more organized way compared to writing the same code without functions. It provides examples of programs that use functions to print patterns, find the larger of two numbers, calculate sum/difference/average of three values, and compute area and volume of a sphere. The last program shows using built-in mathematical functions like absolute value, power, and square root. Functions allow breaking down programs into smaller, reusable parts to make the code better structured and more readable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Functions: Write A C++ Program To Print Out The Following Using Functions and Compare It Without Functions

The document discusses C++ programs that use functions to perform various mathematical operations and comparisons in a more organized way compared to writing the same code without functions. It provides examples of programs that use functions to print patterns, find the larger of two numbers, calculate sum/difference/average of three values, and compute area and volume of a sphere. The last program shows using built-in mathematical functions like absolute value, power, and square root. Functions allow breaking down programs into smaller, reusable parts to make the code better structured and more readable.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Functions

Write a C++ program to print out the following using functions and
compare it without functions.

With functions:
# include <iostream>
using namespace std;
void stars ()
{
int i, j;
for (i=0; i<3; i++)
{
for (j=0; j<6; j++)
{
cout << "*" << "\t";
}
cout << "\n";
}
}

main ()
{
stars ();
cout << "Welcome" << "\n";
stars ();
cout << "Good Bye" << "\n";
stars ();
}

Without functions:
#include<iostream>
using namespace std;
main()
{
int i, j;
for (i=0; i<3; i++)
{
for (j=0; j<6; j++)
{
cout << "*" << "\t";
}
cout << "\n";
}
cout << "Welcome" << "\n";
for (i=0; i<3; i++)
{
for (j=0; j<6; j++)
{
cout << "*" << "\t";
}
cout << "\n";
}
cout << "Good Bye" << "\n";
for (i=0; i<3; i++)
{
for (j=0; j<6; j++)
{
cout << "*" << "\t";
}
cout << "\n";
}
}
Write a C++ program to find the larger value of two integer numbers.
#include<iostream>
using namespace std;
int larger(int num1, int num2)
{
int max;
if (num1 >= num2)
max = num1;
else
max = num2;
return max;
}

main()
{
int n1, n2;
cout <<"Please enter 2 integer numbers: ";
cin >> n1 >> n2;
cout << "\n" <<"The larger number is:" << larger (n1,n2);
}
Write a C++ program to sum and subtract three integer value.
Summation:
#include<iostream>
using namespace std;
int sum(int n1, int n2, int n3)
{
int c;
c = n1 + n2 + n3;
return c;
}
main ()
{
int x, y, z;
cout <<"Please enter 3 integer numbers: ";
cin >> x >> y >> z;
cout << "\n" <<"The sum is: " << sum (x,y,z);
}

Subtraction:
#include<iostream>
using namespace std;
int sub(int n1, int n2, int n3)
{
int c;
c = n1 - n2 - n3;
return c;
}
main ()
{
int x, y, z;
cout <<"Please enter 3 integer numbers: ";
cin >> x >> y >> z;
cout << "\n" <<"The substraction is: " << sub (x,y,z);
}

Write a C++ program to find the average value of three values.


#include<iostream>
using namespace std;
float average(int a, int b, int c)
{
int y;
y = (a+b+c)/3;
return y;
}
main()
{
int x,y,z;
x = 5;
y = 6;
z = 7;
cout << average(x,y,z);
}

Write a C++ program to find the area and volume of sphere.


#include<iostream>
using namespace std;
float area(float x)
{
float A;
A = 4 * 3.14 * x * x;
return A;
}

float volume(float x)
{
float A;
A = (3/4) * 3.14 * x * x * x;
return A;
}

main()
{
float x;
cout << "Please enter the radius of the sphere: ";
cin >> x;
cout << "\n" << "The area of sphere is: " << area(x);
cout << "\n" << "The volume of sphere is: " << volume(x);
}

Write a C++ program using the built in functions of mathematical


equations to find the absolute value, the power of number and the
square root.
// Ready made functions:
// ---------------------
#include <iostream>
#include<cmath>
using namespace std;
main()
{
int i = -5;
double x = 5, y = 6, z = 7;
cout << "The absolute value of i is: " << abs(i) <<"\n";
cout << "The power of x to the power of y is: " << pow(x,y) << "\n";
cout << "The square root of z is: " << sqrt (z);
}

You might also like