Programming in C 312303 Practical No 22: Write C program to demonstrate User defined Functions

Programming in C 312303 Practical No 22: Write a C program to demonstrate User-defined Functions

Hello, students in this post we solve programming in C 312303 manual practical no.22 in which we answer all practical related questions based on the C program to demonstrate User-defined Functions.

Programming in C 312303 Practical No 22

Students will be able to define and declare user-defined functions and can understand the scope of variables. Students will use parameters using call by value and call by reference in the C program.

Exercise

1. Find the factorial of a given number using the function

Answer:
#include <stdio.h>
unsigned long long factorial(int n) {
  // Base case: Factorial of 0 is 1
  if (n == 0) {
    return 1;
  }
  // Recursive case: Factorial of n is n * factorial(n-1)
  unsigned long long result = factorial(n – 1) * n;
  // Check for overflow (result exceeding the maximum value of unsigned long long)
  if (result < 0) {
    printf(“Error: Factorial overflow for number %dn”, n);
    return 0; // Indicate error
  }
  return result;
}
int main() {
  int num;
  printf(“Enter a non-negative integer: “);
  scanf(“%d”, &num);
  if (num < 0) {
    printf(“Error: Factorial is not defined for negative numbers.n”);
    return 1; // Indicate error
  }
  unsigned long long fact = factorial(num);
  if (fact == 0) {
    // Error handling from the factorial function
  } else {
    printf(“The factorial of %d is %llun”, num, fact);
  }
  return 0;
}
Output;
Enter a non-negative integer: 5
The factorial of 5 is 120
=== Code Execution Successful ===
2. Create a function to find GCD of given number. Call this function in a program.

Answer:
#include <stdio.h>
int gcd(int a, int b) {
  // Base case: If b is 0, then GCD is a
  if (b == 0) {
    return a;
  }
  // Recursive case: GCD(a, b) = GCD(b, a % b)
  // We use the modulo operator (%) to find the remainder when a is divided by b.
  return gcd(b, a % b);
}
int main() {
  int num1, num2;
  printf(“Enter two integers: “);
  scanf(“%d %d”, &num1, &num2);
  int greatest_common_divisor = gcd(num1, num2);
  printf(“The GCD of %d and %d is %dn”, num1, num2, greatest_common_divisor);
  return 0;
}
Output:
Enter two integers: 8 5
The GCD of 8 and 5 is 1
=== Code Execution Successful ===

Algorithm

1. Define a function gcd(a, b) that takes two integers a and b as input.
2. Base Case:
If b == 0: This means a is perfectly divisible by b, so itself is the GCD. Return a.
3. Recursive Case:
Use the Euclidean algorithm:
Calculate the remainder (r) when a is divided by b using the modulo operator (%).
Recursively call gcd(b, r). This effectively calculates the GCD of the smaller number (b) and the remainder from the previous division (r).
4. In the main function:
Prompt the user to enter two integers.
Call the gcd function with the entered numbers to get the result.
Print the calculated GCD.

Flowchart

         +———+
          | Start   |
          +———+
                 |
                 v
          +———+
          | Input a |
          +———+
                 |
                 v
          +———+
          | Input b |
          +———+
                 |
                 v
          +———+         +———+
          | b == 0? |         | Yes     |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Yes     |         | Return a|
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | No      |         | r = a % b |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | gcd(b, r)|———>| GCD     |
          +———+         +———+
                 |                 |
                 v                 v
          +———+         +———+
          | Output  |         | (result) |
          +———+         +———+
                 |                 |
                 v                 v
          +———+
          | End     |
          +———+

Practical Related Questions

1. Write a program to reverse the number 1234 using a function.
Answer:
#include <stdio.h>
int reverse_number(int num) {
  int reversed = 0;
  while (num != 0) {
    int last_digit = num % 10;
    reversed = reversed * 10 + last_digit;
    num /= 10;
  }
  return reversed;
}
int main() {
  int original_number = 1234;
  int reversed_number = reverse_number(original_number);
  printf(“The original number is %dn”, original_number);
  printf(“The reversed number is %dn”, reversed_number);
  return 0;
}
Output:
The original number is 1234
The reversed number is 4321
=== Code Execution Successful ===
2. What would be the output of the following programs:

b. 
#include <stdio.h>
main() {
  int i = 45, c;
  c = check(i);
  printf(“n%dn”, c);
}
int check(int ch) {
  if (ch >= 45) {
    return 100;
  } else {
    return 10 * 10;
  }
}
Output:
/tmp/aQ4wqt5ugk.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
    3 | main() {
      | ^~~~
/tmp/aQ4wqt5ugk.c: In function ‘main’:
/tmp/aQ4wqt5ugk.c:7:7: warning: implicit declaration of function ‘check’ [-Wimplicit-function-declaration]
    7 |   c = check(i);
      |       ^~~~~
/tmp/aQ4wqt5ugk.o
100
=== Code Execution Successful ===

Conclusion

We Successfully completed practical no.22 of Programming in C 312303 manual in which we answered all practical related questions.

Suraj Diware
Suraj Diware

Hi everyone, I'm Suraj Diware, and I'm passionate about helping students succeed in MSBTE programs. This blog is dedicated to providing clear and concise explanations of MSBTE curriculum topics, along with practical tips and resources to support your learning journey.

Articles: 703

Leave a Reply

Your email address will not be published. Required fields are marked *