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

Lab 6 Functions

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

Lab 6 Functions

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

Department of Computing

CS110: Fundamentals of Computer Programming


BSDS: 1A

Lab 6: functions

Name : Muhammad Abubakar rashid


reg no : 459565

Lab Tasks
1. Write a function that displays at the left margin of the screen a solid square of asterisks whose
side is specified in integer parameter side. For example, if side is 4, the function displays:

CS110: Fundamentals of Computer Programming Page 1


Code:
//muhammad abubakar rashid
//459565
#include<iostream>
using namespace std;
void s_square(int i, int j){
for (int a = 1; a <= i+1; a++){
for (int b = 1; b < j+1;b++){
cout << "*";
}
cout << endl;
}

}
int main(){
int x, y;
cout << "enter the number of sides of starik" << endl;
cin >> x;
y = x;
s_square(x, y);
return 0;
}

output:

2. Modify the function created in task 1 to form the square out of whatever character is contained
in character parameter fillCharacter. Thus if side is 5 and fillCharacter is “#” then
this function should print:

CS110: Fundamentals of Computer Programming Page 2


Code:
//muhammad abubakar rashid
//459565
#include<iostream>

using namespace std;


void s_square(int i,char o){
for (int a = 1; a <= i; a++){
for (int b = 1; b <=i;b++){
cout <<o;
}
cout << endl;
}

}
int main(){
int x, y;
char l;
cout << "enter the number of sides of starik" << endl;
cin >> x;
cout << "enter the special character you want to display" << endl;
cin >> l;
cout << endl;
s_square(x,l);
return 0;
}
output:

CS110: Fundamentals of Computer Programming Page 3


3. Define a function called hypotenuse that calculates the length of the hypotenuse of a right
triangle when the other two sides are given. Use this function in a program to determine the
length of the hypotenuse for each of the following triangles. The function should take two
arguments of type double and return the hypotenuse as a double. The sample output is as
following.

Code:
//muhammad abubakar rashid
//459565
#include<iostream>
#include<cmath>

using namespace std;


double s_square(float i, float j){
double hypotenuse;
hypotenuse = sqrt(i*i + j*j);
return hypotenuse;
}

int main(){
float x, y;
double m;

cout << "enter perpendicular" << endl;


cin >> x;
cout << "enter base" << endl;
cin >> y;
cout << endl;
m=s_square(x,y);
cout << "the hypotenuse is :" << m << endl;
return 0;

CS110: Fundamentals of Computer Programming Page 4


output:

4. Write a function reversed that takes an integer value(between 1-9999) and


returns the number with its digits reversed. For example, given the number
7631, the function should return 1367. Demonstrate the use of this function
in your program with the following sample output.

Enter a number between 1 and 9999: 7631


The number with its digits reversed is: 1367

Code:
//muhammad abubakar rashid
//459565
#include<iostream>
#include<cmath>

using namespace std;


void s_square(int i){
int j, k, l, h;
j = i % 10;
k = (i / 10) % 10;
l = ((i / 10) / 10) % 10;
h = i / 1000;
cout << j << k << l << h;
}

int main(){
int x, y, z;

cout << "enter a four digit number" << endl;


cin >> x;

cout << endl;

CS110: Fundamentals of Computer Programming Page 5


s_square(x);
cout << endl;
return 0;
}
Output:

5. Write a function is_bouncy that accepts an integer as argument and returns


1 if the number is bouncy and 0 otherwise. Use this function in your program
to print on screen all the 4 digit bouncy numbers along with the total count
of the 4-digit bouncy numbers.

6. Write a function is_prime that accepts an integer as argument and returns


1 if the number is prime and 0 otherwise. Use this function in your program
to print on screen all the 4 digit prime numbers.

Deliverable:

Please submit a word document with codes of all tasks and output on LMS
before the deadline.

CS110: Fundamentals of Computer Programming Page 6

You might also like