Assignment - 5
1. Write suitable function and corresponding program to test them for the following:
(a) Compute xnx^n, where x is any valid number and n is an integer value.
#include <stdio.h>
double power(double x, int n) {
double result = 1;
for (int i = 0; i < n; i++) {
result *= x;
}
return result;
}
int main() {
double x;
int n;
printf("Enter base x and exponent n: ");
scanf("%lf %d", &x, &n);
printf("%.2lf ^ %d = %.2lf\n", x, n, power(x, n));
return 0;
}
(b) Swap values of two integer variables.
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x, y;
printf("Enter two integers: ");
scanf("%d %d", &x, &y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
(c) Compute the GCD of two integers and return the result to the calling function.
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("GCD is %d\n", gcd(x, y));
return 0;
}
(d) Compute and return the sum of n elements of an integer array.
#include <stdio.h>
int sumArray(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Sum = %d\n", sumArray(arr, n));
return 0;
}
(e) Remove white spaces (blank spaces) from a string.
#include <stdio.h>
void removeSpaces(char *str) {
int i = 0, j = 0;
while (str[i]) {
if (str[i] != ' ')
str[j++] = str[i];
i++;
}
str[j] = '\0';
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
removeSpaces(str);
printf("String without spaces: %s\n", str);
return 0;
}
2. Write a C function reverse(s) to reverse the string s (in place reversal).
#include <stdio.h>
#include <string.h>
void reverse(char *s) {
int i, j;
char temp;
int len = strlen(s);
for (i = 0, j = len - 1; i < j; i++, j--) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
reverse(str);
printf("Reversed string: %s\n", str);
return 0;
}
3. Write a C function subs(s1, s2) which returns 1 if s2 is a substring of s1 otherwise
0.
#include <stdio.h>
#include <string.h>
int subs(char *s1, char *s2) {
return strstr(s1, s2) != NULL;
}
int main() {
char s1[100], s2[100];
printf("Enter string 1: ");
fgets(s1, sizeof(s1), stdin);
printf("Enter string 2: ");
fgets(s2, sizeof(s2), stdin);
printf("Result: %d\n", subs(s1, s2));
return 0;
}
4. Write a C function that takes input a two dimensional array of integers and find
the largest integer among them and return it to calling function.
#include <stdio.h>
int findMax(int arr[10][10], int rows, int cols) {
int max = arr[0][0];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] > max)
max = arr[i][j];
}
}
return max;
}
int main() {
int arr[10][10], rows, cols;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
printf("Enter elements:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &arr[i][j]);
printf("Largest element = %d\n", findMax(arr, rows, cols));
return 0;
}
5. Write a C function that finds the largest number from each row and column
individually.
#include <stdio.h>
int main() {
int arr[10][10], rowMax[10], colMax[10];
int rows, cols, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &rows, &cols);
printf("Enter elements:\n");
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
scanf("%d", &arr[i][j]);
for (i = 0; i < rows; i++) {
rowMax[i] = arr[i][0];
for (j = 1; j < cols; j++) {
if (arr[i][j] > rowMax[i])
rowMax[i] = arr[i][j];
}
}
for (j = 0; j < cols; j++) {
colMax[j] = arr[0][j];
for (i = 1; i < rows; i++) {
if (arr[i][j] > colMax[j])
colMax[j] = arr[i][j];
}
}
printf("Row-wise maximum elements:\n");
for (i = 0; i < rows; i++)
printf("%d ", rowMax[i]);
printf("\nColumn-wise maximum elements:\n");
for (j = 0; j < cols; j++)
printf("%d ", colMax[j]);
printf("\n");
return 0;
}
6. Write a C function int reverseInteger(int n) that returns the reverse of n.
#include <stdio.h>
int reverseInteger(int n) {
int reversed = 0;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return reversed;
}
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
printf("Reversed integer = %d\n", reverseInteger(n));
return 0;
}