Q1.
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b, c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Roots are real and different: %.2f and %.2f", root1, root2);
} else if (discriminant == 0) {
root1 = -b / (2*a);
printf("Roots are real and equal: %.2f and %.2f", root1, root1);
} else {
float real = -b / (2*a);
float imag = sqrt(-discriminant) / (2*a);
printf("Roots are complex: %.2f + %.2fi and %.2f - %.2fi", real, imag, real, imag);
}
return 0;
}
OUTPUT
2. Sum of Digits and Occurrence of a Digit
#include <stdio.h>
int main() {
int num, sum = 0, digit, count = 0, search;
printf("Enter a number: ");
scanf("%d", &num);
printf("Enter digit to find occurrence: ");
scanf("%d", &search);
int temp = num;
while (temp != 0) {
digit = temp % 10;
sum += digit;
if (digit == search)
count++;
temp /= 10;
}
printf("Sum of digits = %d\n", sum);
printf("Occurrence of %d = %d times\n", search, count);
return 0;
}
3. GCD and LCM using Euclid’s Method
#include <stdio.h>
int main() {
int a, b, temp, gcd, lcm;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
int x = a, y = b;
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
gcd = a;
lcm = (x * y) / gcd;
printf("GCD = %d\nLCM = %d\n", gcd, lcm);
return 0;
}
4. Print Prime Numbers in a Range
#include <stdio.h>
int main() {
int low, high, i, j, flag;
printf("Enter the range (low high): ");
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are:\n", low, high);
for (i = low; i <= high; i++) {
if (i < 2) continue;
flag = 0;
for (j = 2; j * j <= i; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", i);
}
return 0;
}
5. Binary Search
#include <stdio.h>
int main() {
int n, i, key, low, high, mid, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements in ascending order:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter key to search: ");
scanf("%d", &key);
low = 0;
high = n - 1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) {
printf("Element found at position %d\n", mid + 1);
found = 1;
break;
} else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
if (!found)
printf("Element not found.\n");
return 0;
}
6. Sort Books by ISBN (Bubble Sort)
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter number of books: ");
scanf("%d", &n);
int isbn[n];
printf("Enter ISBN numbers:\n");
for (i = 0; i < n; i++)
scanf("%d", &isbn[i]);
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (isbn[j] > isbn[j + 1]) {
temp = isbn[j];
isbn[j] = isbn[j + 1];
isbn[j + 1] = temp;
}
}
}
printf("Books sorted by ISBN:\n");
for (i = 0; i < n; i++)
printf("%d ", isbn[i]);
return 0;
}
7. Palindrome String Check
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
printf("Enter a string: ");
scanf("%s", str);
int len = strlen(str);
for (int i = 0; i < len; i++)
rev[i] = str[len - i - 1];
rev[len] = '\0';
if (strcmp(str, rev) == 0)
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");
return 0;
}