Skip to content

Commit f8b7c3d

Browse files
committed
answers
1 parent bc43379 commit f8b7c3d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+585
-30
lines changed

src/q1

15.7 KB
Binary file not shown.

src/q1.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
// Write a C program to find the maximum and minimum elements in an array.
1+
// Write a C program to find the maximum and minimum elements in an array.
2+
3+
#include <stdio.h>
4+
5+
int main() {
6+
int n, i, max, min;
7+
printf("Enter number of elements: ");
8+
scanf("%d", &n);
9+
int arr[n];
10+
for(i=0; i<n; i++) scanf("%d", &arr[i]);
11+
12+
max = min = arr[0];
13+
for(i=1; i<n; i++) {
14+
if(arr[i] > max) max = arr[i];
15+
if(arr[i] < min) min = arr[i];
16+
}
17+
printf("Max: %d, Min: %d\n", max, min);
18+
return 0;
19+
}

src/q10

15.7 KB
Binary file not shown.

src/q10.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
// Write a C program to concatenate two strings without using the built-in string functions.
1+
// Write a C program to concatenate two strings without using the built-in string functions.
2+
3+
#include <stdio.h>
4+
5+
int main() {
6+
char str1[100], str2[100];
7+
printf("Enter first string: ");
8+
scanf("%s", str1);
9+
printf("Enter second string: ");
10+
scanf("%s", str2);
11+
12+
int i=0, j=0;
13+
while(str1[i] != '\0') i++;
14+
while(str2[j] != '\0') str1[i++] = str2[j++];
15+
str1[i] = '\0';
16+
17+
printf("Concatenated string: %s\n", str1);
18+
return 0;
19+
}

src/q11

15.7 KB
Binary file not shown.

src/q11.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
// Write a function named calculateAverage that takes an array of integers as input and returns the average of the numbers.
1+
// Write a function named calculateAverage that takes an array of integers as input and returns the average of the numbers.
2+
3+
#include <stdio.h>
4+
5+
float calculateAverage(int arr[], int n) {
6+
int sum = 0;
7+
for(int i=0; i<n; i++) sum += arr[i];
8+
return (float)sum / n;
9+
}
10+
11+
int main() {
12+
int n;
13+
printf("Enter number of elements: ");
14+
scanf("%d", &n);
15+
int arr[n];
16+
for(int i=0; i<n; i++) scanf("%d", &arr[i]);
17+
printf("Average: %.2f\n", calculateAverage(arr, n));
18+
return 0;
19+
}

src/q12

15.8 KB
Binary file not shown.

src/q12.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
// Write a function named isPalindrome that takes a string as input and returns 1 if it is a palindrome (reads the same forwards and backwards), and 0 otherwise.
1+
// Write a function named isPalindrome that takes a string as input and returns 1 if it is a palindrome (reads the same forwards and backwards), and 0 otherwise.
2+
3+
#include <stdio.h>
4+
5+
int isPalindrome(char str[]) {
6+
int i = 0, j;
7+
while(str[i] != '\0') i++;
8+
j = i - 1;
9+
for(i=0; i<j; i++, j--)
10+
if(str[i] != str[j]) return 0;
11+
return 1;
12+
}
13+
14+
int main() {
15+
char str[100];
16+
printf("Enter a string: ");
17+
scanf("%s", str);
18+
if(isPalindrome(str)) printf("Palindrome\n");
19+
else printf("Not a palindrome\n");
20+
return 0;
21+
}

src/q13

15.7 KB
Binary file not shown.

src/q13.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
// Write a function named findFactorial that takes an integer as input and returns its factorial.
1+
// Write a function named findFactorial that takes an integer as input and returns its factorial.
2+
3+
#include <stdio.h>
4+
5+
long findFactorial(int n) {
6+
long fact = 1;
7+
for(int i=1; i<=n; i++) fact *= i;
8+
return fact;
9+
}
10+
11+
int main() {
12+
int n;
13+
printf("Enter a number: ");
14+
scanf("%d", &n);
15+
printf("Factorial: %ld\n", findFactorial(n));
16+
return 0;
17+
}

0 commit comments

Comments
 (0)