ODD 2024 Tutorial 11
ODD 2024 Tutorial 11
Fundamentals -I (15B11CI111)
1.
#include <stdio.h>
int sumStars(int n) {
if (n == 1) {
return 1;
}
return n + sumStars(n - 1);
}
int main() {
int totalStars;
totalStars = sumStars(50);
printf("The total number of stars counted is: %d\n", totalStars);
return 0;
}
2.
#include <stdio.h>
void printEvenNumbers(int start, int end) {
if (start > end) {
return;
}
if (start % 2 == 0) {
printf("%d ", start);
}
printEvenNumbers(start + 1, end);
}
void printOddNumbers(int start, int end) {
if (start > end) {
return;
}
if (start % 2 != 0) {
printf("%d ", start);
}
printOddNumbers(start + 1, end);
}
int main() {
int start, end, choice;
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);
printf("Enter 1 for even numbers or 2 for odd numbers: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Even numbers in the range [%d, %d] are: ", start, end);
printEvenNumbers(start, end);
} else if (choice == 2) {
printf("Odd numbers in the range [%d, %d] are: ", start, end);
printOddNumbers(start, end);
} else {
printf("Invalid choice!\n");
}
return 0;
}
3.
#include <stdio.h>
int isPrime(int n, int i) {
if (n <= 1) {
return 0;
}
if (i * i > n) {
return 1;
}
if (n % i == 0) {
return 0;
}
return isPrime(n, i + 1);
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isPrime(number, 2)) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}
return 0;
}
4.
#include <stdio.h>
void copyString(char *source, char *destination) {
if (*source == '\0') {
*destination = '\0';
return;
}
*destination = *source;
copyString(source + 1, destination + 1);
}
int main() {
char source[100], destination[100];
printf("Enter a string: ");
fgets(source, sizeof(source), stdin);
copyString(source, destination);
printf("Copied string: %s", destination);
return 0;
}
5.
#include <stdio.h>
int findLargest(int arr[], int n) {
// Base case: If only one element, return it
if (n == 1) {
return arr[0];
} int largest = findLargest(arr, n - 1);
if (arr[n - 1] > largest) {
return arr[n - 1];
} else {
return largest;
}
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int largest = findLargest(arr, n);
printf("The largest element in the array is: %d\n", largest);
return 0;
}