You are on page 1of 4

BSEET01191005

ASSIGNMENT

Submitted To: Muhammad Amir Sb


Submitted By: Muhammad Afzal
Reg No: BSEET01191005
Section: EE-3P
Department: Electrical Engineering
Technology

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

Write a function to calculate the factorial value of any integer as an argument.


Call this function from main () and print the results in main ().
#include<iostream>
using namespace std;
int factorial(int);

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;
}
}

You might also like