Lab#5
Lab#5
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>
int main() {
info("Zainab Saleem", 19, 'I', "BCS");
return 0;
}
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 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
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;
}
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>
int main() {
int n;
tableOf(n);
return 0;
}
void tableOf(int x) {
int i;
TASK # 06
Compile all sample programs
Sample Program 1:
#include <stdio.h>
int main() {
int z;
z = addition(5, 3);
printf("The result is %d", z);
return 0;
}
Sample Program 2:
#include <stdio.h>
int main() {
int x = 5, y = 3, z;
z = subtraction(7, 2);
printf("The first result is %d\n", z);
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:
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:
#include <stdio.h>
int main() {
int x = 5, y = 2;
float n = 5.0, m = 2.0;
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>
int main() {
int a, b;
printf("Enter a number: ");
scanf("%d", &a);
printf("Enter a number: ");
scanf("%d", &b);
square(&a, &b);
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 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;
}
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 main() {
int base, powerRaised, result;
printf("Enter base number: ");
scanf("%d", &base);
return 0;
}
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;
}