TCS - CODEVITA
EASY:
ANSWER:
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2 && num1 > num3) {
printf("The largest number is %d.\n", num1);
} else if (num2 > num1 && num2 > num3) {
printf("The largest number is %d.\n", num2);
} else if (num3 > num1 && num3 > num2) {
printf("The largest number is %d.\n", num3);
} else {
printf("All numbers are equal.\n");
}
return 0; }
ANSWER:
#include <stdio.h>
int main() {
char input[100];
int vowelCount = 0;
printf("Enter a string: ");
fgets(input, sizeof(input), stdin);
for (int i = 0; input[i] != '\0'; i++) {
switch (input[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowelCount++;
break;
}
}
printf("Number of vowels entered: %d\n", vowelCount);
return 0;
}
MEDIUM:
ANSWER:
#include <stdio.h>
#include <stdbool.h>
// Function to check if a number is prime
bool isPrime(int n) {
if (n <= 1) return n == 1; // 1 is prime
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
int main() {
int start, end;
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);
printf("Non-prime numbers in the range: %d to %d\n", start,
end); for (int i = start; i <= end; i++) {
if (!isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
ANSWER:
#include <stdio.h>
int main() {
int arr[] = {5, 7, 9, 2, 4, 6, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int elementToSearch;
printf("Enter the element to search: ");
scanf("%d", &elementToSearch);
int index = -1;
for (int i = 0; i < n; i++) {
if (arr[i] == elementToSearch) {
index = i;
break; // Exit the loop when the element is found }
}
if (index != -1) {
printf("Element %d found at index %d.\n", elementToSearch,
index); } else {
printf("Element %d not found in the array.\n",
elementToSearch); }
return 0;
}
ALTERNATIVE: (user input)
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Input array elements from the user
for (int i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
int elementToSearch;
printf("Enter the element to search: ");
scanf("%d", &elementToSearch);
int index = -1;
for (int i = 0; i < n; i++) {
if (arr[i] == elementToSearch) {
index = i;
break; // Exit the loop when the element is found }
}
if (index != -1) {
printf("Element %d found at index %d.\n", elementToSearch,
index); } else {
printf("Element %d not found in the array.\n",
elementToSearch); }
return 0;
}
HARD:
ANSWER:
#include <stdio.h>
#include <string.h>
#define MAX_ATTEMPTS 6
void playHangman(char* word) {
int wordLength = strlen(word);
char guessedWord[wordLength];
int attempts = 0;
// Initialize guessedWord with underscores
for (int i = 0; i < wordLength; i++) {
guessedWord[i] = '_';
}
while (attempts < MAX_ATTEMPTS) {
char guess;
printf("Enter a letter: ");
scanf(" %c", &guess);
// Check if the guessed letter is in the word
int found = 0;
for (int i = 0; i < wordLength; i++) {
if (word[i] == guess) {
guessedWord[i] = guess;
found = 1;
}
}
if (found) {
printf("Correct guess!\n");
} else {
attempts++;
printf("Incorrect guess. Attempts remaining: %d\n", MAX_ATTEMPTS -
attempts);
}
printf("Current word: %s\n", guessedWord);
if (strcmp(guessedWord, word) == 0) {
printf("Congratulations! You guessed the word: %s\n", word);
return;
}
}
printf("You ran out of attempts. The word was: %s\n",
word); }
int main() {
char word[] = "mango";
playHangman(word);
return 0;
}
ANSWER:
#include <stdio.h>
int main() {
int start, end;
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);
printf("Numbers in the range (excluding multiples of 7):\
n"); for (int i = start; i <= end; i++) {
if (i % 7 == 0) {
continue; // Skip multiples of 7
}
printf("%d ", i);
}
printf("\n");
return 0;
}