CPR Write-Ups (10,11 and 12)
CPR Write-Ups (10,11 and 12)
Algorithm:
1. Start
2. Define a function power(a, b) that takes two parameters: base a and exponent b
3. Initialize a variable result to 1
4. For i = 1 to b:
○ Multiply result by a
5. Return result
6. In the main function:
○ Declare variables for base, exponent, and result
○ Input values for base and exponent
○ Call the power function and store the result
○ Display the result
7. End
Code:
#include <stdio.h>
return result;
}
int main() {
int base, exponent, result;
return 0;
}
#include <stdio.h>
int check(int ch);
void main() {
int i = 20, c;
c = check(i);
printf("\n%d", c);
}
int check(int ch) {
if (ch >= 50)
return (100);
else
return (50);
}
Explanation:
Errors:
1. Missing return type for the function (should be int, float, double, etc.)
2. Missing data type for parameter a
Corrected function:
int sqr(int a) {
return (a*a);
}
#include <stdio.h>
return 0;
}
(2) Program to find area and perimeter of a circle using user-defined function:
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area, perimeter;
// Input radius
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Display results
printf("Radius of the circle: %.2f units\n", radius);
printf("Area of the circle: %.2f square units\n", area);
printf("Perimeter of the circle: %.2f units\n", perimeter);
return 0;
}
(3) Program to perform arithmetic operations using switch case and user-defined
functions:
#include <stdio.h>
int main() {
float num1, num2, result;
char operation;
case '-':
result = subtract(num1, num2);
printf("%.2f - %.2f = %.2f\n", num1, num2, result);
break;
case '*':
result = multiply(num1, num2);
printf("%.2f * %.2f = %.2f\n", num1, num2, result);
break;
case '/':
result = divide(num1, num2);
if (num2 != 0) {
printf("%.2f / %.2f = %.2f\n", num1, num2, result);
}
break;
default:
printf("Error: Invalid operation selected.\n");
}
return 0;
}
#include <stdio.h>
int main() {
char character, result;
// Input a character
printf("Enter an alphabet: ");
scanf("%c", &character);
return 0;
}
Experiment 11
i) Call by Value
Algorithm:
1. Start
2. Define a function swap(int a, int b) that takes two integers as parameters
3. Inside the function, swap the values using a temporary variable
4. Print the values before and after the swap function call in the main function
5. End
Code:
#include <stdio.h>
int main() {
int a = 10, b = 20;
return 0;
}
Algorithm:
1. Start
2. Define a function swap(int *a, int *b) that takes two integer pointers as parameters
3. Inside the function, swap the values using a temporary variable and pointers
4. Print the values before and after the swap function call in the main function
5. End
Code:
#include <stdio.h>
int main() {
int a = 10, b = 20;
return 0;
}
#include <stdio.h>
void fun(int *p, int *q)
{
*p = 20;
*q = *p * 2;
}
void main()
{
int i = 2, j = 10;
fun(&i, &j);
printf("%d %d", i, j);
}
Output: 20 40
Explanation:
#include <stdio.h>
void modify(int x)
{
x = x * 10;
}
void main()
{
int x = 10;
modify(x);
printf("%d", x);
}
Answer:
#include <stdio.h>
int fun(int *p, int q)
{
int temp;
temp = *p;
*p = q;
return temp;
}
void main()
{
int i = 10, j = 20;
j = fun(&i, j);
printf("%d %d", i, j);
}
Answer:
Explanation:
#include <stdio.h>
int main() {
int num1, num2, num3, sum;
return 0;
}
Question (2): Program to find area and perimeter of a rectangle using call by reference
#include <stdio.h>
int main() {
float length, width, area, perimeter;
return 0;
}
#include <stdio.h>
int main() {
int num;
unsigned long result;
// Input number
printf("Enter a non-negative integer: ");
scanf("%d", &num);
// Input validation
if (num < 0) {
printf("Error: Factorial is not defined for negative numbers.\n");
return 1;
}
// Calculate factorial
result = factorial(num);
return 0;
}
Experiment 12
Algorithm:
1. Start
2. Include necessary header files (stdio.h, math.h)
3. Define functions for each mathematical operation
4. In the main function, demonstrate the use of each function with examples
5. Print the results
6. End
Code:
#include <stdio.h>
#include <math.h>
int main() {
// Demonstration of mod (absolute value) operation
double a = -15.5;
double mod_result = (a < 0) ? -a : a;
printf("1. Mod operation: |%.1f| = %.1f\n", a, mod_result);
1. Code Reusability: Functions allow us to write code once and use it multiple times,
reducing duplication.
2. Modularity: Functions help break down complex programs into smaller, manageable
modules.
3. Abstraction: Functions hide implementation details, allowing users to focus on what the
function does rather than how it does it.
4. Testing and Debugging: Functions make it easier to test and debug code as they can be
tested in isolation.
5. Readability: Functions with meaningful names make code more readable and
self-documenting.
6. Maintainability: Functions make code easier to maintain as changes need to be made in
only one place.
7. Encapsulation: Functions can encapsulate data processing logic, protecting data integrity.
8. Parameter Passing: Functions allow for flexible parameter passing using call by value or
call by reference.
1. ceil(): Returns the smallest integer greater than or equal to a given number.
○ Syntax: double ceil(double x)
○ Example: ceil(3.2) returns 4.0
2. floor(): Returns the largest integer less than or equal to a given number.
○ Syntax: double floor(double x)
○ Example: floor(3.8) returns 3.0
3. fabs(): Returns the absolute value of a floating-point number.
○ Syntax: double fabs(double x)
○ Example: fabs(-5.6) returns 5.6
4. sin(): Returns the sine of an angle (in radians).
○ Syntax: double sin(double x)
○ Example: sin(0) returns 0.0
#include <stdio.h>
#include <math.h>
void main()
{
int i = 10;
printf("%d", log10(i));
}
Explanation: The log10() function returns the base-10 logarithm of a number. The base-10
logarithm of 10 is 1, as 10^1 = 10. However, log10() returns a double, but the printf statement
uses %d format specifier, which is for integers. This will truncate the decimal part, but in this
case, log10(10) = 1.0, so the output is 1.
#include <stdio.h>
#include <math.h>
return 0;
}
This distance calculator uses the Euclidean distance formula: len = √[(x₂ - x₁)² + (y₂ - y₁)²]
The program demonstrates the use of mathematical functions like sqrt() and pow() from
the math.h library to implement the distance formula correctly.