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

Computer Programming - Task 2 Functions

Uploaded by

amiirrrrayman0
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Computer Programming - Task 2 Functions

Uploaded by

amiirrrrayman0
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Programming Task2_Functions

10/10/2024
1. Write a C++ program to first allow a user to input two floating numbers x and y from K.B. Then, call a
function to add the two numbers, call a second function to sub the two numbers, and call a third
function to multiply the two numbers. Finally, print the results on the screen.

#include <iostream>

using namespace std;

int ADD(int x, int y) {

return x + y; }

int SUB (int x, int y){

return x - y;}

int MUL (int x, int y){

return x * y;}

int main() {

int x, y;

cout << "Enter two integers: ";

cin >> x >> y;

cout << "Sum: " << ADD(x,y) << endl;

cout << "Difference: " << SUB(x,y) << endl;

cout << "Product: " << MUL(x,y) << endl;

return 0;

}
2. Write a C++ program to first allow a user to input two integer numbers x and y from KB. Then, call one
function called calculate to find and return the sum, the difference, and the product of the two numbers.
Finally, from the main function print the results on the screen.

Hint: To use one function to compute the sum, difference and product , then print them from the main
function, it must be like this:
void calculate(int x, int y, int& sum, int& difference, int& product)
{ sum = x + y; difference = x - y; product = x * y;
}
Answer
#include <iostream>
using namespace std;

void calculate(int x, int y, int& sum, int& difference, int& product) {


sum = x + y;
difference = x - y;
product = x * y;
}

int main() {
int x, y, sum, diff, pro;
cout << "Enter two integers: ";
cin >> x >> y;

calculate(x, y, sum, diff, pro);

cout << "Sum: " << sum << endl;


cout << "Difference: " << diff << endl;
cout << "Product: " << pro << endl;

return 0;
}

3. Write a C++ program to first allow a user to input an integer number n from KB. Then, call a function to
find and return the factorial of n. Finally, print the result on the screen.

Answer
#include <iostream>
using namespace std;
int fun (int n){
if (n= =1)
return 1;
else
return n*fun(n-1);
}
int main()
{
int n;
cin>>n;
cout<<fun(n);
return 0;
}

4. Write a C++ program to first allow a user to input an integer number n from K.B. Then, call a function to
find and return the sum of the values from 1 to n. Finally print the sum and the average on the screen.

#include <iostream>
using namespace std;
int sum (int n){
int sumn=0;
for(int i=1;i<=n;i++){
sumn+=i;
}
cout<<sumn;
return 0;
}
int main() {
int n;
cout<<"enter the nember: ";
cin>>n;
cout <<endl;
sum(n);
return 0;
}

5. Write a C++ program to first allow a user to input two integer numbers (A and B) from KB. Then, call a
function to find and return the sum of the values from A to B. Finally print the sum and the average on
the screen.

#include <iostream>
using namespace std;
int sum (int A,int B){
int sumn=0;
for(int i=A;i<=B;i++){
sumn+=i;
}
cout<<"the sum is: "<<sumn;
return 0;
}
int main() {
int A,B;
cout<<"enter the start: ";
cin>>A;
cout<<"enter the end number: ";
cin>>B;
sum(A,B);
return 0;
}

6. Write a C++ program to first allow a user to input two integer numbers (S and E) from K.B. Then, call a
function called sumEven() to find and return the sum of the even values from S to E. Finally print the
sum and the average on the screen.

#include <iostream>
using namespace std;
int sumEven (int A,int B){
int sumn=0;
for(int i=A;i<=B;i++){
if (i%2= =0){
sumn+=i;
}
}

cout<<"the sum is: "<<sumn;


return 0;
}
int main() {
int A,B;
cout<<"enter the start: ";
cin>>A;
cout<<"enter the end number: ";
cin>>B;
sum(A,B);
return 0;
}

7. Write a C++ program to first allow a user to input an integer number n from KB. Then, call a function
called sumOdd() to find and return the sum of the odd values from 1 to n. Finally print the sum and the
average on the screen.
#include <iostream>
using namespace std;
int sumOdd (int A){
int sumn=0;
for(int i=A;i<=B;i++){

sumn+=I;
}

cout<<"the sum is: "<<sumn;


return 0;
}
int main() {
int A;

cout<<"enter the end number: ";


cin>>A;
sum(A);
return 0;
}

8. Write a C++ program to first allow a user to input three different integer numbers from KB. Then, call a
function called Min() to find and return the min value and another function called Max()to find and
return the max value. Finally, print the min and the max values on the screen.
#include <iostream>
using namespace std;

int min(int x,int y, int z){


return min(x,min(y,z));
}
int max(int x,int y, int z){
return max(x,min(y,z));
}
int main() {
int x,y,z;
cout<<"enter the numbers"<<endl;
cin>>x>>y>>z;
cout<<"the minimum number is : "<<min(x,y,z)<<endl;
cout<<"the maximum number is : "<<max(x,y,z);
return 0;
}

9. Implement a function to generate the Fibonacci sequence up to a specified term. The Fibonacci
sequence is a series of numbers that starts with 0 and 1. Each subsequent number is the sum of the
previous two.
The sequence looks like this:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

#include <iostream>
Using namespace std;

Void fibonacci(int n) {
Int a = 0, b = 1;
Cout << a << “ “ << b << “ “;
For (int I = 2; I < n; ++i) {
Int next = a + b;
Cout << next << “ “;
A = b;
B = next;
}
Cout << endl;
}

Int main() {
Int n;
Cout << “Enter the number of terms for Fibonacci sequence: “;
Cin >> n;

Fibonacci(n);

Return 0;
}
10. Write a C++ program to first allow a user to input two integer numbers (A and B) from KB. Then, call a
function to swap them. You should print the two numbers before and after swapping.

11. Write a C++ program to first allow a user to input an integer number n from KB. Then, by using
recursion, call a function to find and return the factorial of n. Finally, print the result on the screen.

You might also like