cs lab exp kani
cs lab exp kani
Algorithm:
CODE:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
printf("Characters in the string:\n");
for (int i = 0; str[i] != '\0'; i++)
{
printf("%c\n", str[i]);
}
return 0;
}
OUTPUT:
Case 1:
Case 2:
Enter a string: World123
Characters in the string:
W
o
r
l
d
1
2
3
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int vowels[5] = {0};
int i;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++)
{
char ch = tolower(str[i]);
switch (ch)
{
case 'a': vowels[0]++; break;
case 'e': vowels[1]++; break;
case 'i': vowels[2]++; break;
case 'o': vowels[3]++; break;
case 'u': vowels[4]++; break;
}
}
printf("\nFrequency of vowels:\n");
printf("a: %d\n", vowels[0]);
printf("e: %d\n", vowels[1]);
printf("i: %d\n", vowels[2]);
printf("o: %d\n", vowels[3]);
printf("u: %d\n", vowels[4]);
return 0;
}
OUTPUT:
Case 1:
Case 2:
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int is_vowel(char ch)
{
ch = tolower(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
int main()
{
char name[100], modifiedName[200];
int i, j = 0;
printf("Enter a name: ");
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = '\0';
for (i = 0; name[i] != '\0'; i++)
{
modifiedName[j++] = name[i];
if (is_vowel(name[i])) {
modifiedName[j++] = name[i];
}
}
modifiedName[j] = '\0';
printf("Modified name: %s\n", modifiedName);
return 0;
}
OUTPUT:
Case 1:
Case 2:
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
#include <string.h>
int main()
{
char firstName[50], middleName[50], lastName[50],
fullName[150];
scanf("%s", firstName);
printf("Enter the middle name: ");
scanf("%s", middleName);
printf("Enter the last name: ");
scanf("%s", lastName);
strcpy(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, middleName);
strcat(fullName, " ");
strcat(fullName, lastName);
printf("Full name (using library functions): %s\n", fullName);
return 0;
}
#include <stdio.h>
int main()
{
char firstName[50], middleName[50], lastName[50],
fullName[150];
int i, j;
printf("Enter the first name: ");
scanf("%s", firstName);
printf("Enter the middle name: ");
scanf("%s", middleName);
OUTPUT:
Input:
Output:
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
float add(float a, float b);
float subtract(float a, float b);
float multiply(float a, float b);
float divide(float a, float b);
int main() {
int choice;
float num1, num2, result;
while (1)
{
printf("\n--- Pocket Calculator ---\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice)
if (choice == 5) {
printf("Exiting the program. Goodbye!\n");
break;
}
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch (choice)
{
case 1:
result = add(num1, num2);
printf("Result: %.2f\n", result);
break;
case 2:
result = subtract(num1, num2);
printf("Result: %.2f\n", result);
break;
case 3:
result = multiply(num1, num2);
OUTPUT:
Input:
Output:
Result: 15.00
Input:
Enter your choice: 4
Enter two numbers: 10 0
Output:
Error: Division by zero is not allowed.
Input:
Enter your choice: 5
Output:
Exiting the program. Goodbye!
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
long long factorial(int n);
int main()
{
int number;
long long result;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number < 0)
{
printf("Error: Factorial of a negative number doesn't exist.\n");
}
else
{
result = factorial(number);
printf("Factorial of %d is %lld\n", number, result);
}
return 0;
}
long long factorial(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
OUTPUT:
Input:
Output:
Factorial of 5 is 120
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
void print_even(int n1, int n2);
int main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
print_even(num1, num2);
return 0;
}
void print_even(int n1, int n2)
{
int start = n1;
if (start % 2 != 0) {
start++;
}
printf("Even numbers between %d and %d are:\n", n1, n2);
for (int i = start; i <= n2; i += 2) {
printf("%d ", i);
}
printf("\n");
}
OUTPUT:
Input:
Output:
Input:
Output:
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
#include <stdbool.h>
bool is_prime(int num);
void print_prime(int n1, int n2);
int main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
print_prime(num1, num2);
return 0;
}
bool is_prime(int num)
{
if (num <= 1)
{
return false;
}
for (int i = 2; i * i <= num; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
void print_prime(int n1, int n2)
{
printf("Prime numbers between %d and %d are:\n", n1, n2); for
(int i = n1; i <= n2; i++)
{
if (is_prime(i))
{
printf("%d ", i);
}
}
printf("\n");
}
OUTPUT:
Input:
Output:
Input:
Output:
2357
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
struct Book
{
char title[100];
float price;
int number_of_pages;
};
int main()
{
struct Book book;
printf("Enter the title of the book: ");
fgets(book.title, sizeof(book.title), stdin);
printf("Enter the price of the book: ");
scanf("%f", &book.price);
printf("Enter the number of pages: ");
scanf("%d", &book.number_of_pages);
printf("\n--- Book Details ---\n");
printf("Title: %s", book.title);
printf("Price: %.2f\n", book.price);
printf("Number of Pages: %d\n", book.number_of_pages);
return 0;
}
OUTPUT:
Input:
Output:
RESULT:
8(b) Read 2 book details and print the details of the costlier
book.
Aim:
Algorithm:
CODE:
#include <stdio.h>
struct Book
{
char title[100];
float price;
int number_of_pages;
};
int main()
{
struct Book book1, book2;
printf("Enter the details of the first book:\n");
printf("Enter the title: ");
fgets(book1.title, sizeof(book1.title), stdin);
printf("Enter the price: ");
scanf("%f", &book1.price);
printf("Enter the number of pages: ");
scanf("%d", &book1.number_of_pages);
getchar();
printf("\nEnter the details of the second book:\n");
printf("Enter the title: ");
fgets(book2.title, sizeof(book2.title), stdin); // Read title with
spaces
printf("Enter the price: ");
scanf("%f", &book2.price);
printf("Enter the number of pages: ");
scanf("%d", &book2.number_of_pages);
printf("\n--- Costlier Book Details ---\n");
if (book1.price > book2.price) {
printf("Title: %s", book1.title);
printf("Price: %.2f\n", book1.price);
printf("Number of Pages: %d\n", book1.number_of_pages);
}
else if (book2.price > book1.price)
{
printf("Title: %s", book2.title);
OUTPUT:
Input:
Output:
Input:
Output:
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
int main() {
int n, i, mark, total = 0;
printf("Enter the number of questions: ");
scanf("%d", &n);
printf("Enter the marks for each question (0-16):\n");
for (i = 1; i <= n; i++) {
while (1) {
printf("Question %d: ", i);
if (scanf("%d", &mark) != 1) {
printf("Invalid input! Please enter a valid number.\n"); // Clear
the input buffer
while (getchar() != '\n');
continue;
}
if (mark < 0 || mark > 16) {
printf("Marks must be in the range 0-16. Please re-enter.\n");
continue;
}
if (total + mark > 100) {
printf("Adding this mark exceeds the total limit of 100. Please re-
enter.\n");
continue;
}
total += mark;
break;
}
}
printf("The total marks awarded: %d\n", total);
return 0;
}
OUTPUT:
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
int main()
{
int num = 10;
int *ptr;
ptr = #
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", (void*)&num);
printf("Value stored in ptr (Address of num): %p\n", (void*)ptr);
printf("Value pointed to by ptr: %d\n", *ptr);
return 0;
}
OUTPUT:
Value of num: 10
Address of num: 0x7ffee1d5e3dc
Value stored in ptr (Address of num): 0x7ffee1d5e3dc
Value pointed to by ptr: 10
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x, y;
printf("Enter the first number: ");
scanf("%d", &x);
printf("Enter the second number: ");
scanf("%d", &y);
printf("\nBefore swapping:\n");
printf("x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("\nAfter swapping:\n");
printf("x = %d, y = %d\n", x, y);
return 0;
}
OUTPUT:
Input:
Output:
Before swapping:
x = 5, y = 10
After swapping:
x = 10, y = 5
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
int main()
{
int num1, num2;
char operator;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter an operator (+, -, *, /, ++, --): ");
scanf(" %c", &operator); // Space before %c to avoid newline
character from previous input
if (operator == '++' || operator == '--')
{
if (operator == '++')
{
printf("Value before increment: %d\n", num1);
printf("Value after increment: %d\n", ++num1);
}
else if (operator == '--')
{
printf("Value before decrement: %d\n", num1);
printf("Value after decrement: %d\n", --num1);
}
}
else
{
printf("Enter the second number: ");
scanf("%d", &num2);
switch (operator)
{
case '+':
printf("Result: %d + %d = %d\n", num1, num2, num1 + num2);
break;
case '-':
printf("Result: %d - %d = %d\n", num1, num2, num1 - num2);
break;
case '*':
printf("Result: %d * %d = %d\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0)
{
printf("Result: %d / %d = %.2f\n", num1, num2, (float)num1 / num2);
}
else
{
printf("Error! Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid operator!\n");
}
}
return 0;
}
OUTPUT:
Input:
Output:
Result: 10 + 5 = 15
Input:
Output:
Input:
Output:
Input:
Output:
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
int main()
{
FILE *file;
char ch;
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Could not open the file.\n");
return 1;
}
printf("File contents:\n");
while ((ch = fgetc(file)) != EOF)
{
putchar(ch);
}
fclose(file);
return 0;
}
OUTPUT:
INPUT
Hello, World!
This is a test file.
OUTPUT
File contents:
Hello, World!
This is a test file.
RESULT:
Aim:
Algorithm:
CODE:
#include <stdio.h>
int main()
{
FILE *file;
char content[1000];
printf("Enter the content to write to the file (press Enter to end
input):\n");
fgets(content, sizeof(content), stdin);
file = fopen("output.txt", "w");
if (file == NULL)
{
printf("Could not open the file.\n");
return 1;
}
fprintf(file, "%s", content);
printf("Content written to the file 'output.txt'.\n");
fclose(file);
return 0;
}
OUTPUT:
Input:
Enter the content to write to the file (press Enter to end input):
Hello, this is a test content!
Output:
RESULT: