Docsity

C++ Programming – Functions (6 Solved Programs), Exercises of Computer science

Toggle sidebar
bg1
1.Write a C++ program with a function that takes 2 integer parameters, adds them
together and then returns the sum.
#include <iostream>
using namespace std;
int addNumbers(int a, int b) {
return a + b;
}
int main() {
int num1 = 5;
int num2 = 7;
int result = addNumbers(num1, num2);
cout << "The sum is: " << result << endl;
return 0;
}
2.Write a program with a function name "mean" to read in three integers from the
keyboard to find the arithmetic mean..
#include <iostream>
using namespace std;
double mean(int a, int b, int c) {
return (a + b + c) / 3.0;
}
int main() {
int num1, num2, num3;
cout << "Enter three integers: ";
pf3
pf4
pf5

Partial preview of the text

Download C++ Programming – Functions (6 Solved Programs) and more Exercises Computer science in PDF only on Docsity!

1.Write a C++ program with a function that takes 2 integer parameters, adds them together and then returns the sum. #include using namespace std; int addNumbers(int a, int b) { return a + b; } int main() { int num1 = 5; int num2 = 7; int result = addNumbers(num1, num2); cout << "The sum is: " << result << endl; return 0; } 2.Write a program with a function name "mean" to read in three integers from the keyboard to find the arithmetic mean.. #include using namespace std; double mean(int a, int b, int c) { return (a + b + c) / 3.0; } int main() { int num1, num2, num3; cout << "Enter three integers: ";

cin >> num1 >> num2 >> num3; double result = mean(num1, num2, num3); cout << "The arithmetic mean is: " << result << endl; return 0; } 3.Write a C++ program having a function named ‘rectangle’ to read the length and width of the rectangle from the keyboard and find the area of the rectangle. The result should be returned to the main program for displaying on the screen. #include using namespace std; int rectangle(int length, int width) { return length * width; } int main() { int length, width; cout << "Enter the length of the rectangle: "; cin >> length; cout << "Enter the width of the rectangle: "; cin >> width; int area = rectangle(length, width);

cout << "The area of the square is: " << squareArea << endl; cout << "The perimeter of the square is: " << squarePerimeter << endl; return 0; } 5 .Write a C++ program to read a number from the keyboard and then pass it to a function to determine whether it is prime or composite. #include using namespace std; bool isPrime(int number) { if (number <= 1) { return false; } for (int i = 2; i * i <= number; i++) { if (number % 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 a composite number." << endl; } return 0; } 6 .Write a C++ program to get an integer number from the keyboard in the main program and pass it as an argument to a function where it calculates and displays the table. #include using namespace std; void displayTable(int number) { cout << "Multiplication Table of " << number << ":" << endl; for (int i = 1; i <= 10; i++) { cout << number << " x " << i << " = " << number * i << endl; } } int main() {