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

cse115-lab-manual-12-function

Uploaded by

safiulbasar51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

cse115-lab-manual-12-function

Uploaded by

safiulbasar51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CSE 115 Lab on functions – Ara2

1. C program illustrating the difference between void and non-void function:


#include <stdio.h> //main function
// definition of a non-void function void main()
float computeSquare(float x) {
{ float m, n;
return x*x; printf("\nEnter a number: ");
} scanf("%f", &m);
//call computeSquare function on m
// definition of a void function n = computeSquare(m);
void printCube(float x) printf ("Square = %f", n);
{ //call printCube function on m
printf("Cube = %f", x*x*x); printCube(m);
} }

2. C program to determine if a given number is odd/even using function

#include <stdio.h> void main()


void oddEven(int x) {
{ int m;
if(x%2==0) printf("Even"); printf("\nEnter an integer: ");
else printf("Odd"); scanf("%d", &m);
} oddEven(m); //function call
}

Try yourself2: Write C program using a function to check if a given number is positive, negative, or zero.

Here’s a C program that uses a function to check if a given number is positive, negative, or zero:

#include <stdio.h>

// Function to check if a number is positive, negative, or zero


void checkNumber(int num) {
if (num > 0) {
printf("%d is a positive number.\n", num);
} else if (num < 0) {
printf("%d is a negative number.\n", num);
} else {
printf("%d is zero.\n", num);
}
}

int main() {
int num;

// Ask the user to enter a number


printf("Enter a number: ");
scanf("%d", &num);

// Call the function to check the number


checkNumber(num);

return 0;
}

Explanation:

1. The program defines a function checkNumber(int num) that takes an integer as an argument.
2. Inside the function, we check if the number is positive, negative, or zero using if-else statements.
3. In the main function, the user is prompted to input a number, which is passed to the checkNumber()
function to determine its type.

Sample Output:
Enter a number: 5
5 is a positive number.

Enter a number: -3
-3 is a negative number.

Enter a number: 0
0 is zero.

This program checks if the number is positive, negative, or zero using a function and prints the appropriate
message.

3. C program to determine if a given number is prime using function

#include <stdio.h> int main()


int isPrime(int x) {
{ int m;
int i; printf("\nEnter an integer: ");
for(i=2;i<=x/2;i++) scanf("%d", &m);
{ int n = isPrime(m);
if(x%i==0) if(n==0)
return 0; printf("Not prime")
} else
return 1; printf("Prime");
} }

Try yourself 3: Write C program using a function to check if a given number is a perfect number.

A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding the number
itself). For example, the number 6 is a perfect number because its divisors (excluding 6) are 1, 2, and 3, and
their sum is 6.

Here’s a C program using a function to check if a given number is a perfect number:


#include <stdio.h>

// Function to check if a number is a perfect number


int isPerfectNumber(int num) {
int sum = 0;

// Find divisors and sum them


for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i; // Add divisor to sum
}
}

// Check if sum of divisors equals the number


if (sum == num && num != 0) {
return 1; // The number is perfect
} else {
return 0; // The number is not perfect
}
}

int main() {
int num;

// Ask the user to enter a number


printf("Enter a number: ");
scanf("%d", &num);

// Call the function and check if the number is perfect


if (isPerfectNumber(num)) {
printf("%d is a perfect number.\n", num);
} else {
printf("%d is not a perfect number.\n", num);
}

return 0;
}

Explanation:

1. isPerfectNumber Function:
o The function isPerfectNumber(int num) calculates the sum of all divisors of the given
number num (excluding the number itself).
o It checks if the sum of the divisors is equal to the number. If it is, the number is a perfect
number, and the function returns 1. Otherwise, it returns 0.
2. Main Function:
o The main function asks the user to input a number.
o It then calls the isPerfectNumber() function to check if the number is perfect.
o Based on the result, it prints whether the number is perfect or not.

Sample Output:
Enter a number: 6
6 is a perfect number.

Enter a number: 28
28 is a perfect number.

Enter a number: 10
10 is not a perfect number.

This program checks if the given number is a perfect number using a function and prints the appropriate
message.

4. C program to compute sum of all natural numbers between m and n (using function)

#include <stdio.h> int main()


int sum(int m, int n) {
{ int n;
int i, sum=0; printf("\nEnter 2 integers: ");
for(i=m;i<=n;i++) scanf("%d%d", &m, &n);
{
sum+=i; int s = sum(m,n);
} printf("sum=%d",s)
return sum; }
}

5. C program to compute the integer resulting from rounding a number n (using function)

#include <stdio.h> int main()


int round1(float n) {
{ float n;
int i=n; //integer part of n printf("\nEnter a number: ");
if(n-i>=0.5) return i+1; scanf("%f", &n);
else return i;
} int s = round1(n);
printf("%d",s)
}

Exercise:
1. Write a C program using 3 functions to compute diameter, circumference and area of a circle whose
radius is given by the user as the input.
2. Find the sum of the following series using a function: 12 + 22 + 32 + … + N2

Assignment:

1. Find the sum of the following series using user-defined function: 1/1! + 2/2! + 3/3! + …… +1/N!
2. Write a C code using functions that takes two integers: a and b as inputs and returns the value of a b.
3. Compute the sum of the following geometric progression using a function with 2 parameters r and n:

1 + r + r2 + … + rn (read the values of r and n from user)

4. Write a C program that reads an integer and returns the reverse of that number using function.
5. Write a C program using function that reads a floating point number n and an integer d and then
prints the rounded value of n up to d decimal places. E.g. for n=5.678 and d = 2; it should print 5.68

Exercise Problems

1. Write a C program using 3 functions to compute diameter, circumference, and area of a circle whose
radius is given by the user as input.

c
Copy
#include <stdio.h>

#define PI 3.14159

// Function to compute diameter


float computeDiameter(float radius) {
return 2 * radius;
}

// Function to compute circumference


float computeCircumference(float radius) {
return 2 * PI * radius;
}

// Function to compute area


float computeArea(float radius) {
return PI * radius * radius;
}

int main() {
float radius;

// Ask the user for the radius


printf("Enter the radius of the circle: ");
scanf("%f", &radius);

// Call the functions and print the results


printf("Diameter: %.2f\n", computeDiameter(radius));
printf("Circumference: %.2f\n", computeCircumference(radius));
printf("Area: %.2f\n", computeArea(radius));

return 0;
}

Explanation:

 Three functions are used to calculate the diameter, circumference, and area of the circle.
 The main() function asks the user for the radius, then calls each function to compute and display the
values.

Sample Output:
yaml
Copy
Enter the radius of the circle: 5
Diameter: 10.00
Circumference: 31.42
Area: 78.54

2. Find the sum of the following series using a function: 1^2 + 2^2 + 3^2 + … + N^2.

c
Copy
#include <stdio.h>

// Function to calculate the sum of squares of the first N numbers


int sumOfSquares(int N) {
int sum = 0;
for (int i = 1; i <= N; i++) {
sum += i * i;
}
return sum;
}

int main() {
int N;

// Ask the user for the value of N


printf("Enter a number N: ");
scanf("%d", &N);

// Call the function and print the result


printf("Sum of squares from 1^2 to %d^2 is: %d\n", N, sumOfSquares(N));

return 0;
}

Explanation:

 The function sumOfSquares(int N) calculates the sum of squares from 1 to N.


 The main() function asks the user for the value of N, then calls the function and displays the result.

Sample Output:
vbnet
Copy
Enter a number N: 5
Sum of squares from 1^2 to 5^2 is: 55

Assignment Problems

1. Find the sum of the following series using a user-defined function: 1/1! + 2/2! + 3/3! + … + 1/N!

c
Copy
#include <stdio.h>

// Function to compute factorial


long long factorial(int n) {
long long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}

// Function to compute the series


double computeSeries(int N) {
double sum = 0.0;
for (int i = 1; i <= N; i++) {
sum += (double)i / factorial(i);
}
return sum;
}

int main() {
int N;

// Ask the user for the value of N


printf("Enter a number N: ");
scanf("%d", &N);

// Call the function and print the result


printf("Sum of the series is: %.4f\n", computeSeries(N));

return 0;
}

Explanation:

 The function factorial() calculates the factorial of a given number.


 The function computeSeries() calculates the sum of the series 1/1! + 2/2! + 3/3! + ... + N/N!.
 The main() function asks the user for N, then calls computeSeries() to calculate and display the sum.

Sample Output:
mathematica
Copy
Enter a number N: 5
Sum of the series is: 2.7167

2. Write a C code using functions that takes two integers: a and b as inputs and returns the value of a^b.

c
Copy
#include <stdio.h>

// Function to compute a^b


long long power(int a, int b) {
long long result = 1;
for (int i = 1; i <= b; i++) {
result *= a;
}
return result;
}

int main() {
int a, b;

// Ask the user for a and b


printf("Enter base (a) and exponent (b): ");
scanf("%d %d", &a, &b);

// Call the function and print the result


printf("%d raised to the power of %d is: %lld\n", a, b, power(a, b));

return 0;
}

Explanation:

 The function power() calculates the value of a^b by multiplying a with itself b times.
 The main() function asks the user for a and b, then calls power() to compute the result.

Sample Output:
css
Copy
Enter base (a) and exponent (b): 3 4
3 raised to the power of 4 is: 81

3. Compute the sum of the following geometric progression using a function with 2 parameters r and n:

1 + r + r^2 + … + r^n (read the values of r and n from the user).

c
Copy
#include <stdio.h>

// Function to compute the sum of the geometric progression


double geometricSum(double r, int n) {
double sum = 0.0;
for (int i = 0; i <= n; i++) {
sum += pow(r, i); // pow(r, i) computes r^i
}
return sum;
}

int main() {
double r;
int n;

// Ask the user for r and n


printf("Enter the common ratio (r) and number of terms (n): ");
scanf("%lf %d", &r, &n);

// Call the function and print the result


printf("Sum of the geometric progression is: %.2f\n", geometricSum(r, n));

return 0;
}

Explanation:

 The function geometricSum() calculates the sum of a geometric progression using the formula 1 + r +
r^2 + ... + r^n.
 The main() function asks the user for r (common ratio) and n (number of terms), then calls
geometricSum() to compute the sum.
Sample Output:
java
Copy
Enter the common ratio (r) and number of terms (n): 2 5
Sum of the geometric progression is: 63.00

4. Write a C program that reads an integer and returns the reverse of that number using a function.

c
Copy
#include <stdio.h>

// Function to reverse the number


int reverseNumber(int num) {
int reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return reversed;
}

int main() {
int num;

// Ask the user for the number


printf("Enter an integer: ");
scanf("%d", &num);

// Call the function and print the reversed number


printf("Reversed number is: %d\n", reverseNumber(num));

return 0;
}

Explanation:

 The function reverseNumber() reverses the digits of a given number.


 The main() function reads an integer and calls the function to reverse the number.

Sample Output:
vbnet
Copy
Enter an integer: 12345
Reversed number is: 54321

5. Write a C program using function that reads a floating point number n and an integer d and then
prints the rounded value of n up to d decimal places.

c
Copy
#include <stdio.h>
#include <math.h>
// Function to round the number to d decimal places
void roundNumber(float n, int d) {
printf("Rounded number: %.*f\n", d, n);
}

int main() {
float n;
int d;

// Ask the user for n and d


printf("Enter a floating point number: ");
scanf("%f", &n);
printf("Enter number of decimal places: ");
scanf("%d", &d);

// Call the function and print the result


roundNumber(n, d);

return 0;
}

Explanation:

 The function roundNumber() uses printf with the %.d format specifier to round the floating point
number to d decimal places.
 The main() function asks for the number and the number of decimal places, then calls the function to
print the rounded result.

Sample Output:
yaml
Copy
Enter a floating point number: 5.678
Enter number of decimal places: 2
Rounded number: 5.68

You might also like