0% found this document useful (0 votes)
9 views4 pages

C++ Function Example Programs

The document presents five C++ programs that illustrate various function concepts, including parameters, return types, loops, and conditionals. Each program demonstrates a specific functionality, such as finding the maximum of two numbers, checking for prime numbers, calculating factorials, converting Celsius to Fahrenheit, and displaying a menu. These examples serve as practical applications of functions in C++ programming.

Uploaded by

preciousmpota2
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)
9 views4 pages

C++ Function Example Programs

The document presents five C++ programs that illustrate various function concepts, including parameters, return types, loops, and conditionals. Each program demonstrates a specific functionality, such as finding the maximum of two numbers, checking for prime numbers, calculating factorials, converting Celsius to Fahrenheit, and displaying a menu. These examples serve as practical applications of functions in C++ programming.

Uploaded by

preciousmpota2
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

C++ Function Programs

Each program demonstrates different function concepts like function calls, return types, parameters,
conditionals, and loops.

Program 1: Find the Maximum of Two Numbers

Concepts: Function with parameters and return value.

#include <iostream>

using namespace std;

int findMax(int a, int b) {

return (a > b) ? a : b;

int main() {

int num1, num2;

cout << "Enter two numbers: ";

cin >> num1 >> num2;

int max = findMax(num1, num2);

cout << "The maximum is: " << max << endl;

return 0;

Program 2: Check if a Number is Prime

Concepts: Boolean return, loop inside function.

#include <iostream>

using namespace std;

bool isPrime(int n) {

if (n <= 1) return false;


for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) return false;

return true;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

if (isPrime(number))

cout << number << " is a prime number." << endl;

else

cout << number << " is not a prime number." << endl;

return 0;

Program 3: Calculate the Factorial of a Number

Concepts: Function with loop, returning an integer.

#include <iostream>

using namespace std;

int factorial(int n) {

int result = 1;

for (int i = 1; i <= n; i++) {

result *= i;

return result;

}
int main() {

int num;

cout << "Enter a positive number: ";

cin >> num;

cout << "Factorial of " << num << " is: " << factorial(num) << endl;

return 0;

Program 4: Convert Celsius to Fahrenheit

Concepts: Function with float return type.

#include <iostream>

using namespace std;

float convertToFahrenheit(float celsius) {

return (celsius * 9 / 5) + 32;

int main() {

float tempC;

cout << "Enter temperature in Celsius: ";

cin >> tempC;

float tempF = convertToFahrenheit(tempC);

cout << "Temperature in Fahrenheit: " << tempF << endl;

return 0;

}
Program 5: Display a Menu using a Function

Concepts: Void functions, modularity.

#include <iostream>

using namespace std;

void showMenu() {

cout << "------ MENU ------" << endl;

cout << "1. Start Game" << endl;

cout << "2. Load Game" << endl;

cout << "3. Exit" << endl;

int main() {

showMenu();

int choice;

cout << "Enter your choice: ";

cin >> choice;

cout << "You selected option " << choice << endl;

return 0;

You might also like