Assignment # 4 Q
Assignment # 4 Q
Assignment # 4 Q
Assignment # 04
NAME HAMAYUN SAEED
ROLL NO 19014156-019
Questions
1. Write a program that takes a number from user and pass it to a function. That
function should return the square of the number.
#include<iostream>
using namespace std;
int square(int i);
int main(){
int j;
cout<<"entre a number=";
cin>>j;
square (j);
return 0;
}
int square(int i){
int a;
a=i*i;
cout<<"square of the number is="<<a;
return a;
}
2. Write a program that takes a number from user and pass it to a function. That
function should return the cube of the number.
#include<iostream>
using namespace std;
int cube(int b);
int main(){
int a;
cout<<"entre a number=";
cin>>a;
cube(a);
return 0;
}
int cube(int b){
int m;
m=b*b*b;
cout<<"cube of the number is="<<m;
return m;
3.Write a program that inputs two number from user and pass it to a function. That
function should return the power of number as first number raised to power second
number
#include<iostream>
#include<math.h>
using namespace std;
int po(int r,int t);
int main(){
int a,b;
cout<<"entrefirst number=";
cin>>a;
cout<<"entre second number=";
cin>>b;
pow(a,b);
return 0;
}
int po(int r,int t){
int power=pow (r,t);
cout<<r<<"power is="<<power;
return 0;
}
.
1. Write a program that implements the functionality of Question 1, 2 and 3 in a
single program. The program should take a choice from user for a particular
operation (e.g. square, cube or power). Then call respective function to get the
result.
#include<iostream>
#include<math.h>
using namespace std;
intpo();
intsq();
int cu();
int main()
{
int a;
cout<<"press 1 for square:\npress 2 for cube:\npress3 for power:\n";
cin>>a;
if(a==1){
cout<<"square is"<<sq();
}
else if(a==2){
cout<<"cube is"<<cu();
}
else if(a==3){
cout<<"power is"<<po();
}
return 0;
}
intpo(){
inta,b,power;
cout<<"enter 1st number:";
cin>>a;
cout<<"enter 2nd number:";
cin>>b;
power=pow(a,b);
return power;
}
intsq(){
inte,square;
cout<<"enter 1st number:";
cin>>e;
square=e*e;
return square;
}
int cu(){
intr,cube;
cout<<"enter 1st number:";
cin>>r;
cube=r*r*r;
return cube;
}
9.Write a program that takes two numbers from user and pass them to a function.
That function should swap their values. If I print those numbers in main function
after calling the SWAP function, their swapped values should be displayed.
#include<iostream>
using namespace std;
void swap(intx,int y);
int main()
{
inta,b;
cout<<"enter two numbers:";
cin>>a>>b;
cout<<"before swapping"<<endl;
cout<<"a ="<<a<<endl;
cout<<"b ="<<b<<endl;
swap(a,b);
return 0;
}
void swap(intx,int y){
int z;
z=x;
x=y;
y=z;
cout<<"after swapping"<<endl;
cout<<"a ="<<x<<endl;
cout<<"b ="<<y<<endl;
}
return 0;
}
}
cout<<"max:"<<max<<endl;
return max;
}
intpr(intnum){
for(i = 2; i<= n / 2; ++i)
{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout<< "This is a prime number";
else
cout<< "This is not a prime number";
return 0;
}
13.Write a program that inputs a matrix of 4*2. Pass it to a function that should
return the sum of all elements of that matrix.
#include<iostream>
using namespace std;
int sum(intarr[4][2]);
int main()
{
int arr1[4][2];
for(inti=0;i<4;i++){
for(int j=0;j<2;j++){
cout<<"enter values of array:["<<i<<"]["<<j<<"]";
cin>>arr1[i][j];
}
}
cout<<"sum is:"<<sum(arr1);
return 0;
}
int sum(intarr[4][2]){
inti,j,summ=arr[0][0];
for(i=0;i<4;i++){
for(j=0;j<2;j++){
}
summ=arr[i][j];
returnsumm;
}..