Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab#5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Lab # 5

Functions

TASK # 01

Create a function which display your Name , Reg no, Class, Section.
Display all the things within the body of function, call the function in main.

Source code:
#include <stdio.h>

void info(char n[], int reg, char sem, char dept[]);

int main() {
info("Zainab Saleem", 19, 'I', "BCS");
return 0;
}

void info(char n[], int reg, char sem, char dept[]) {


printf("%s\n%d\n%c\n%s", n, reg, sem, dept);
}

Output:

TASK # 02
Create a function SUM in C++ which calculates the sum of 5 numbers entered by user.

#include <stdio.h>

void SUM();

int main() {
SUM();
return 0;
}

void SUM() {
int a, b, c, d, e, sum;
printf("Enter 5 numbers:\n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
sum = a + b + c + d + e;
printf("Sum = %d", sum);
}

TASK # 03

Create a function IS_Prime which take a number as argument, return TRUE if number is
prime.
Take number from user at run time.

#include <stdio.h>

int IS_PRIME(int x);

int main() {
int n;
int r;
printf("Enter a number: ");
scanf("%d", &n);
r = IS_PRIME(n);
if (r == 1)
printf("TRUE");

return 0;
}

int IS_PRIME(int x) {
int flag = 1;
int i;

if (x == 0 || x == 1) {
flag = 0;
} else {
for (i = 2; i < x; i++)
if (x % i == 0) {
flag = 0;
break;
}
}

return flag;
}

TASK # 04

Create a calculator using functions


make separate functions for each of the following operators + , - , * , / , %
Give user choices for operator, call the required function accordingly
#include <stdio.h>

int add(int x, int y);


int subtract(int x, int y);
int prod(int x, int y);
float division(float x, float y);
int mod(int x, int y);

int main() {
int a, b;
char ch;
printf("Enter 2 numbers:\n");
scanf("%d %d", &a, &b);
printf("Enter the operation you want to perform: ");
scanf(" %c", &ch); // Note the space before %c to consume any newline characters

switch(ch) {
case '+':
printf("%d", add(a, b));
break;
case '-':
printf("%d", subtract(a, b));
break;
case '*':
printf("%d", prod(a, b));
break;
case '/':
printf("%f", division(a, b));
break;
case '%':
printf("%d", mod(a, b));
break;
}

return 0;
}

int add(int x, int y) {


return (x + y);
}

int subtract(int x, int y) {


return (x - y);
}

int prod(int x, int y) {


return (x * y);
}

float division(float x, float y) {


return (x / y);
}

int mod(int x, int y) {


return (x % y);
}

TASK # 05

Create a function tableof which take a number as argument and print the table of that
number into reverse order from 12 to 1.

#include <stdio.h>

void tableOf(int x);

int main() {
int n;

printf("Enter a number: ");


scanf("%d", &n);

tableOf(n);
return 0;
}

void tableOf(int x) {
int i;

printf("Multiplication table of %d in reverse order:\n", x);

for (i = 12; i >= 1; i--) {


printf("%d * %d = %d\n", x, i, x * i);
}
}

TASK # 06
Compile all sample programs
Sample Program 1:
#include <stdio.h>

int addition(int a, int b) {


int r;
r = a + b;
return r;
}

int main() {
int z;
z = addition(5, 3);
printf("The result is %d", z);
return 0;
}
Sample Program 2:

#include <stdio.h>

int subtraction(int a, int b) {


int r;
r = a - b;
return r;
}

int main() {
int x = 5, y = 3, z;

z = subtraction(7, 2);
printf("The first result is %d\n", z);

printf("The second result is %d\n", subtraction(7, 2));

printf("The third result is %d\n", subtraction(x, y));

z = 4 + subtraction(x, y);
printf("The fourth result is %d\n", z);

return 0;
}
Sample Program 3:

#include <stdio.h>

void printmessage() {
printf("I'm a function!");
}

int main() {
printmessage();
return 0;
}

Sample Program 4:

// declaring functions prototypes


#include <stdio.h>

void odd(int x);


void even(int x);

int main() {
int i;
do {
printf("Please, enter a number (0 to exit): ");
scanf("%d", &i);
odd(i);
} while (i != 0);
return 0;
}

void odd(int x) {
if ((x % 2) != 0)
printf("It is odd.\n");
else
even(x);
}

void even(int x) {
if ((x % 2) == 0)
printf("It is even.\n");
else
odd(x);
}

Sample Program 5:

// passing parameters by reference

#include <stdio.h>

int operateInt(int a, int b) {


return a * b;
}

float operateFloat(float a, float b) {


return a / b;
}

int main() {
int x = 5, y = 2;
float n = 5.0, m = 2.0;

printf("%d\n", operateInt(x, y));


printf("%f\n", operateFloat(n, m));

return 0;
}

Sample Program 6:

// factorial calculator
#include <stdio.h>

long factorial(long a) {
if (a > 1)
return (a * factorial(a - 1));
else
return 1;
}

int main() {
long number = 9;
printf("%ld! = %ld", number, factorial(number));
return 0;
}

TASK # 07
Create a program with a function which calculate the square of both the values entered by user.
(Using call be reference)

#include <stdio.h>

void square(int *x, int *y);

int main() {
int a, b;
printf("Enter a number: ");
scanf("%d", &a);
printf("Enter a number: ");
scanf("%d", &b);

square(&a, &b);

printf("Square of these 2 numbers are\n%d\n%d", a, b);


return 0;
}

void square(int *x, int *y) {


*x = (*x) * (*x);
*y = (*y) * (*y);
}

TASK # 08
Write a program with a function volume( ) which accepts three sides of a cube and returns its
volume. Provide a default value of 1 for all the three sides of a cube. Call this function with zero,
one, two and three arguments and display the volume returned in the main().
Use Function Overloading concept for this task
v=s^3 ,s = length of side

#include <stdio.h>
#include <math.h>

int volume() {
int a = 1;
int v;
v = pow(a, 3);
return v;
}

int volume1(int x) {
int v;
v = pow(x, 3);
return v;
}

int volume2(int x, int y) {


int v;
v = x * x * y;
return v;
}

int volume3(int x, int y, int z) {


int v;
v = x * y * z;
return v;
}

int main() {
printf("Function with 0 arguments: %d\n", volume());
printf("Function with 1 arguments: %d\n", volume1(1));
printf("Function with 2 arguments: %d\n", volume2(1, 1));
printf("Function with 3 arguments: %d\n", volume3(1, 1, 1));

return 0;
}
TASK # 09
Perform Task # 03 by using Default value concept call the function with 1, 2 and 3 Arguments
#include <stdio.h>

bool IS_Prime(int x) {
bool flag = true;
for (int i = 2; i < x; i++) {
if (x % i == 0) {
flag = false;
break;
}
}
return flag;
}

bool IS_Prime2(int x, int y) {


bool flag_x = true;
bool flag_y = true;
for (int i = 2; i < x; i++) {
if (x % i == 0) {
flag_x = false;
break;
}
}
for (int i = 2; i < y; i++) {
if (y % i == 0) {
flag_y = false;
break;
}
}
return flag_x && flag_y;
}

bool IS_Prime3(int x, int y, int z) {


bool flag_x = true;
bool flag_y = true;
bool flag_z = true;
for (int i = 2; i < x; i++) {
if (x % i == 0) {
flag_x = false;
break;
}
}
for (int i = 2; i < y; i++) {
if (y % i == 0) {
flag_y = false;
break;
}
}
for (int i = 2; i < z; i++) {
if (z % i == 0) {
flag_z = false;
break;
}
}
return flag_x && flag_y && flag_z;
}

int main() {
int r, s, t;

r = IS_Prime(2);
printf("Function with 1 argument(2):\n");
if (r == true)
printf("TRUE. 2 is a prime number.\n");
else
printf("FALSE. 2 is not a prime number.\n");

s = IS_Prime2(3, 4);
printf("\nFunction with 2 arguments(3, 4):\n");
if (s == true)
printf("TRUE. All are prime numbers.\n");
else
printf("FALSE. 4 is not a prime number.\n");

t = IS_Prime3(5, 9, 11);
printf("\nFunction with 3 arguments(5, 9, 11):\n");
if (t == true)
printf("TRUE. All are prime numbers.\n");
else
printf("FALSE. 9 is not a prime number.\n");
return 0;
}

TASK # 10
Create a program with a function which calculate the power of a number, both number and
power should be entered by user at run time. (Note : Use Recursion for this program)
#include <stdio.h>

int calculatePower(int base, int powerRaised);

int main() {
int base, powerRaised, result;
printf("Enter base number: ");
scanf("%d", &base);

printf("Enter power number(positive integer): ");


scanf("%d", &powerRaised);

result = calculatePower(base, powerRaised);


printf("%d^%d = %d", base, powerRaised, result);

return 0;
}

int calculatePower(int base, int powerRaised) {


if (powerRaised != 0)
return (base * calculatePower(base, powerRaised - 1));
else
return 1;
}

TASK # 11
Create a C++ program with a recursive function to calculate the sum of first 7 natural numbers

#include <stdio.h>

int sum(int n) {
if (n == 0)
return n;
else
return n + sum(n - 1);
}

int main() {
int n = 7;
printf("Sum of first %d natural numbers is: %d", n, sum(n));
return 0;
}

You might also like