Module 5 respository for lab
Aim:
To determine whether a number entered by the user is even or odd using a pointer.
Algorithm:
- Start the program.
- Declare an integer variable
nand a pointerp. - Read the number from the user and store it in
n. - Assign the address of
nto the pointerp. - Check the value pointed by
p:- If divisible by 2 → the number is even.
- Otherwise → the number is odd.
- Print the result.
- End the program.
Source Code:
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int *p;
p = &n;
if (*p % 2 == 0) {
printf("%d is even.", *p);
} else {
printf("%d is odd.", *p);
}
return 0;
}Result: The program uses a pointer to determine and display whether the entered number is even or odd.
2.# Program: Sum of Digits Using Recursion
Aim:
To write a C program that calculates the sum of digits of a given number using recursion.
Algorithm:
- Start the program.
- Define a recursive function
sumOfDigits(int n)that:- Returns
0ifnis0(base case). - Otherwise, returns
(n % 10) + sumOfDigits(n / 10)to sum the last digit with the recursive call for the remaining digits.
- Returns
- In the
main()function:- Read an integer
nfrom the user. - Call the recursive function
sumOfDigits(n)and store the result. - Print the sum of digits.
- Read an integer
- End the program.
Source Code:
#include <stdio.h>
// Recursive function
int sumOfDigits(int n) {
if (n == 0) // base case
return 0;
return (n % 10) + sumOfDigits(n / 10); // last digit + sum of remaining
}
int main() {
int n;
scanf("%d", &n);
int result = sumOfDigits(n);
printf("Sum of digits of %d = %d\n", n, result);
return 0;
}Result: The program successfully computes and displays the sum of digits of a given number using a recursive function.
3.# Program: Maximum Element in Each Row of a Matrix
Aim:
To write a C program that finds and prints the maximum element of each row in a given matrix.
Algorithm:
- Start the program.
- Declare a 2D array
ma[m][n]to store the matrix elements. - Read the number of rows
mand columnsnfrom the user. - Input all elements of the matrix using nested
forloops. - For each row:
- Initialize
maxas the first element of that row. - Compare each element in the row with
max. - If any element is greater than
max, updatemax.
- Initialize
- After scanning each row, print the maximum element of that row.
- End the program.
Source Code:
#include<stdio.h>
int main()
{
int m, n, row, col;
scanf("%d%d", &m, &n);
int ma[m][n];
for(row = 0; row < m; row++) {
for(col = 0; col < n; col++) {
scanf("%d", &ma[row][col]);
}
}
for(row = 0; row < m; row++) {
int max = ma[row][0];
for(col = 0; col < n; col++) {
max = (ma[row][col] > max) ? ma[row][col] : max;
}
printf("Maximum element of the row %d is: %d\n", row + 1, max);
}
return 0;
}output
4.# Program: Reverse a String Using Pointer
Aim:
To write a C program that reverses a given string using a pointer.
Algorithm:
- Start the program.
- Declare a character array
strto store the input string and a pointerptr. - Read a string using
fgets()from the user. - Use
strlen()to calculate the length of the string. - Remove the newline character (
\n) added byfgets(), if present. - Initialize the pointer
ptrto point to the last character of the string. - Using a
forloop, print characters in reverse order by:- Printing the character pointed to by
ptr. - Decrementing the pointer in each iteration.
- Printing the character pointed to by
- Print the reversed string and end the program.
Source Code:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
char *ptr;
int length, i;
fgets(str, sizeof(str), stdin);
// Remove newline character if present
length = strlen(str);
if (str[length - 1] == '\n') {
str[length - 1] = '\0';
length--;
}
// Set pointer to the last character of the string
ptr = &str[length - 1];
// Print characters in reverse using the pointer
for (i = length - 1; i >= 0; i--) {
printf("%c", *ptr);
ptr--; // Move pointer backward
}
printf("\n");
return 0;
}output
Result: The program successfully reverses the input string using a pointer and displays it in reverse order.
5.# Program: Remove Duplicate Characters from a String
Aim:
To write a C program that removes duplicate characters from a given string.
Algorithm:
- Start the program.
- Declare a character array
strto store the input string. - Read the string from the user using
fgets(). - Calculate the length of the string using
strlen(). - Use nested
forloops to compare each character with the rest of the string:- Outer loop iterates through each character (
i). - Inner loop checks for duplicate characters (
j).
- Outer loop iterates through each character (
- If a duplicate character is found:
- Shift all characters after the duplicate one position to the left.
- Decrease the length of the string by 1.
- Decrement
jto recheck from the shifted position.
- Print the final string after removing duplicates.
- End the program.
Source Code:
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, j, k;
fgets(str, sizeof(str), stdin);
int len = strlen(str);
for (i = 0; i < len; i++) {
for (j = i + 1; j < len; j++) {
if (str[i] == str[j]) {
for (k = j; k < len; k++) {
str[k] = str[k + 1];
}
len--;
j--;
}
}
}
printf("String after removing characters: %s", str);
return 0;
}output

