0% found this document useful (0 votes)
19 views8 pages

Unit 4 C

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)
19 views8 pages

Unit 4 C

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/ 8

1) Develop a C program to find the factorial of a given number using recursion.

#include <stdio.h>
// Recursive function to find the factorial of a number
int factorial(int n) {
if (n <= 1) { // Base case
return 1;
} else { // Recursive case
return n * factorial(n - 1);
}
}
int main() {
int number;
// Input the number from the user
printf("Enter a number to find its factorial: ");
scanf("%d", &number);

// Call the recursive function and print the result


printf("Factorial of %d is: %d\n", number, factorial(number));
return 0;
}

2) Elaborate recursive function mechanism with a suitable example.

Recursion is a process where a function calls itself directly or indirectly. It is a powerful tool
in programming to solve problems that can be broken down into smaller, repetitive sub-
problems.
Example :
#include <stdio.h>
// Recursive function to find the factorial of a number
int factorial(int n) {
if (n <= 1) { // Base case
return 1;
} else { // Recursive case
return n * factorial(n - 1);
}
}
int main() {
int number;
// Input the number from the user
printf("Enter a number to find its factorial: ");
scanf("%d", &number);

// Call the recursive function and print the result


printf("Factorial of %d is: %d\n", number, factorial(number));
return 0;
}
3) Build a program that consists of a function ‘convert’ that accepts a character in lowercase and
returns its upper-case equivalent.
#include <stdio.h>
char convert(char ch) {
if (ch >= 'a' && ch <= 'z') {
return ch - ('a' - 'A');
} else {
return ch;
}
}
int main() {
char lower, upper;
printf("Enter a lowercase character: ");
scanf("%c", &lower);
upper = convert(lower);
printf("The uppercase equivalent of '%c' is '%c'.\n", lower, upper);
return 0;
}

4) Elaborate any five string handling functions with suitable code snippets.

 strlen(): Calculates the length of the string str.


 strcpy(): Copies the contents of src to dest.
 strcat(): Concatenates str2 to str1.
 strcmp(): Compares str1 and str2 and determines their lexical relationship.
 strchr(): Finds the first occurrence of the character ch in the string str.

Strlen()
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
int length = strlen(str);
printf("The length of the string is: %d\n", length);
return 0;
}

Strcpy()
#include <stdio.h>
#include <string.h>

int main() {
char src[] = "Hello, World!";
char dest[50];
strcpy(dest, src);
printf("Destination string is: %s\n", dest);
return 0;
}

Strcat():
#include <stdio.h>
#include <string.h>

int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated string is: %s\n", str1);
return 0;
}

Strcmp():
#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("The strings are equal.\n");
} else if (result < 0) {
printf("str1 is less than str2.\n");
} else {
printf("str1 is greater than str2.\n");
}
return 0;
}
strchr():
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
char ch = 'o';
char *ptr = strchr(str, ch);
if (ptr != NULL) {
printf("Character '%c' found at position: %ld\n", ch, ptr - str + 1);
} else {
printf("Character '%c' not found.\n", ch);
}
return 0
}
5) Build a C program to find the number of vowels, consonants and digits in the given string
#include <stdio.h>
int main() {
char str[100];
int vowels = 0, consonants = 0, digits = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (int i = 0; str[i] != '\0'; i++) {
char ch = str[i];
if (ch >= '0' && ch <= '9') {
digits++;
} else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
// Convert to lowercase to simplify vowel checking
ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else {
consonants++;
}
}
}

// Print the results


printf("Number of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
printf("Number of digits: %d\n", digits);

return 0;
}

6) Build a C program to merge two strings without using string functions#include <stdio.h>
int main() {
char str1[100], str2[100], merged[200];
int i = 0, j = 0;
printf("Enter the first string: ");
gets(str1);
printf("Enter the second string: ");
gets(str2);
while (str1[i] != '\0') {
merged[i] = str1[i];
i++;
}
while (str2[j] != '\0') {
merged[i] = str2[j];
i++;
j++;
}
merged[i] = '\0';
printf("The merged string is: %s\n", merged);
return 0;
}

7) Discuss various categories of functions with suitable code snippet.

1. Standard Library Functions


These are built-in functions provided by C's standard library. They are predefined and can be
used directly in programs.
Example: printf and scanf
#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
2. User-Defined Functions
These are functions created by the programmer to perform specific tasks. They help in
modularizing the code and reusing it.
Example: Function to Calculate the Sum of Two Numbers
#include <stdio.h>

// Function declaration
int sum(int a, int b);

int main() {
int num1 = 5, num2 = 10;
int result = sum(num1, num2);
printf("The sum is: %d\n", result);
return 0;
}

// Function definition
int sum(int a, int b) {
return a + b;
}
3. Recursive Functions
These are functions that call themselves to solve a problem. They are useful for problems that
can be broken down into smaller, similar subproblems.
Example: Function to Calculate Factorial Using Recursion
#include <stdio.h>

// Recursive function declaration


int factorial(int n);
int main() {
int num = 5;
printf("Factorial of %d is: %d\n", num, factorial(num));
return 0;
}

// Recursive function definition


int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}

4. Inline Functions
These functions are defined with the inline keyword. The compiler tries to expand the function
inline, which can potentially improve performance by reducing the overhead of function calls.
Example: Inline Function to Find Maximum of Two Numbers
#include <stdio.h>

// Inline function definition


inline int max(int a, int b) {
return (a > b) ? a : b;
}

int main() {
int x = 10, y = 20;
printf("The maximum is: %d\n", max(x, y));
return 0;
}
5. Static Functions
These are functions with static keyword, meaning they are restricted to the file in which they are
declared. They are not visible to other files in the program.
Example: Static Function to Calculate Square
#include <stdio.h>

// Static function definition


static int square(int n) {
return n * n;
}

int main() {
int num = 4;
printf("The square of %d is: %d\n", num, square(num));
return 0;
}

Explanation:
1. Standard Library Functions: printf and scanf are used to perform input/output operations.
2. User-Defined Functions: The sum function calculates the sum of two numbers.
3. Recursive Functions: The factorial function calculates the factorial of a number using
recursion.
4. Inline Functions: The max function finds the maximum of two numbers and is expanded
inline by the compiler.
5. Static Functions: The square function calculates the square of a number and is only visible
within the file it's declared in.

8) Elaborate call by value and call by reference mechanism with suitable examples.
Call by Value
Definition: In "call by value," the actual value of the argument is passed to the function. Any
changes made to the parameter inside the function do not affect the original value.
Example:
#include <stdio.h>

void swapByValue(int a, int b) {


int temp = a;
a = b;
b = temp;
printf("Inside swapByValue - a: %d, b: %d\n", a, b);
}
int main() {
int x = 5, y = 10;
printf("Before swapByValue - x: %d, y: %d\n", x, y);
swapByValue(x, y);
printf("After swapByValue - x: %d, y: %d\n", x, y);
return 0;
}

Call by Reference
Definition: In "call by reference," the address of the argument is passed to the function.
Changes made to the parameter inside the function affect the original value.
Example:
#include <stdio.h>
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
printf("Inside swapByReference - a: %d, b: %d\n", *a, *b);
}
int main() {
int x = 5, y = 10;
printf("Before swapByReference - x: %d, y: %d\n", x, y);
swapByReference(&x, &y);
printf("After swapByReference - x: %d, y: %d\n", x, y);
return 0;
}

You might also like