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

Lab06 (Functions1)

The document discusses using functions in C++ programs. It provides examples of functions to find the cube of a number, find maximum and minimum of three numbers, check if a number is even or odd, check if a number is prime, and find all prime numbers within a range.

Uploaded by

Samer Sweileh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lab06 (Functions1)

The document discusses using functions in C++ programs. It provides examples of functions to find the cube of a number, find maximum and minimum of three numbers, check if a number is even or odd, check if a number is prime, and find all prime numbers within a range.

Uploaded by

Samer Sweileh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Functions Exercises

(Lab 06)
1. Write a C++ program to find cube of a number using function.
#include <iostream>
using namespace std;

int cubeNum(int);

int main(){
int n, cube;
cout << "Enter a number: ";
cin >> n;

cube = cubeNum(n);

cout << "Cube of " << n << " is: " << cube << endl;
return 0;
}
int cubeNum(int x){
return (x * x * x);
}
2. Write a C++ program to find maximum and minimum between three
numbers using function.
#include <iostream>
using namespace std;

float large(float a, float b, float c);


float small(float a, float b, float c);

int main()
{
float num1, num2, num3, largest, smallest;
cout<<"please Enter Three umbers:"<<endl;
cin>>num1>>num2>>num3;

largest = large(num1, num2, num3);


smallest = small(num1, num2, num3);
cout<<"The Maximum nmber is: "<<largest<<endl;
cout<<"The Minimum nmber is: "<<smallest<<endl;

system ("pause");

return 0;
}
float large(float a, float b, float c)
{
if(a>=b && a>=c) return a;
else if(b>=a && b>=c) return b;
else return c;
}

float small(float a, float b, float c)


{
if(a<=b && a<=c) return a;
else if(b<=a && b<=c) return b;
else return c;
}

3. Write a C++ program to check whether a number is even or odd using


functions.
#include <iostream>
using namespace std;

void find_Oddeven(int);

int main()
{
int num;
cout << "Enter a number to ceck odd or even" << endl;
cin>>num;
find_Oddeven(num);

return 0;
}
void find_Oddeven(int num){
if(num%2==0)
cout<<num<<" is an even";
else
cout<<num<<" is an odd";
}
4. Write a C++ program to check whether a number is prime or not using
functions.
#include <iostream>
using namespace std;

void check_prime(int);

int main()
{
int number;
cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
cin>>number;
check_prime(number);
return 0;
}

void check_prime(int n)
{
int count=0;
for(int i=2;i<n;i++)
{
if(n%i==0) count++;
}
if(count>0) cout<<"NOT A PRIME NUMBER \n";
else cout<<"PRIME NUMBER \n";
}

OR

#include <iostream>
using namespace std;

void check_prime(int);

int main()
{
int number;
cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
cin>>number;
check_prime(number);
return 0;
}

void check_prime(int n)
{
for (int i=2 ; i<x1;i++)
{
if (x1%i == 0) {cout<<"This is Not prime number\n"; break;}
else {cout<<"This is prime number\n"; break;}
}
}
5. Write a C++ program to find all prime numbers between given interval using
functions.

#include <iostream>
using namespace std;

void find_prime(int,int);
int check_prime(int);

void main()
{
int n1,n2;
cout<<"Input Lower Limit ";
cin>>n1;
cout<<"Input Upper Limit ";
cin>>n2;
cout<<"The Prime Number between "<<n1<<" - "<<n2<<" are: ";
find_prime(n1,n2);
system ("pause");
}

void find_prime(int n1,int n2)


{
int flag;
for (int i=n1; i<=n2;i++)
{
flag = check_prime(i);
if (flag) cout<<i<<" ";
}
cout<<endl;
}

int check_prime(int n)
{
int count=0;
for(int i=2;i<n;i++)
{
if(n%i==0) count++;
}
if(count > 0) return 0;
else return n;
}

You might also like