cse115-lab-manual-12-function
cse115-lab-manual-12-function
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>
int main() {
int 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.
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.
int main() {
int 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)
5. C program to compute the integer resulting from rounding a number n (using function)
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:
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
int main() {
float 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>
int main() {
int N;
return 0;
}
Explanation:
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>
int main() {
int N;
return 0;
}
Explanation:
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>
int main() {
int 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:
c
Copy
#include <stdio.h>
int main() {
double r;
int 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>
int main() {
int num;
return 0;
}
Explanation:
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;
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