Professional Documents
Culture Documents
ASSIGNMENT
The University Of
Lahore
1
BSEET01191005
Q No 1
Write a program using function which accept two integers as an argument and
return its sum. Call this function from main () and print the results in main ().
#include<iostream>
using namespace std;
int sum(int, int);
int main()
{
int x,y,s;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
s=sum(x,y);
cout<<"The sum is : "<<s;
return 0;
}
int sum(int a, int b)
{
int total;
total=a+b;
return total;
}
Q No 2
2
BSEET01191005
int main()
{
int x,f;
cout<<"Enter number : ";
cin>>x;
f=factorial(x);
cout<<"The factorial is :"<<f;
return 0;
}
int factorial(int a)
{
int fact=1;
while(a>=1)
{
fact=fact*a;
a--;
}
return fact;
}
Q No 3
Write a function that receives two numbers as an argument and display all
prime numbers between these two numbers. Call this function from main ().
#include<iostream>
using namespace std;
void showprime(int, int);
int main()
{
int x,y;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
showprime(x,y);
3
BSEET01191005
return 0;
}
void showprime(int a, int b)
{
bool flag;
for(int i=a+1;i<=b;i++)
{
flag=false;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
flag=true;
break;
}
}
if(flag==false && i>1)
cout<<i<<endl;
}
}