PART A
1. Write a C program to find the roots of a quadratic equation ax2+bx+c=0.
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c;
float discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
if (a == 0) {
printf("This is not a quadratic equation.\n");
return 0;
}
discriminant = b * b - 4 * a * c;
if (discriminant > 0) // Roots are real and distinct.
{
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct.\n");
printf("Root 1 = %.2f\n", root1);
printf("Root 2 = %.2f\n", root2);
}
else if (discriminant == 0) // Roots are real and equal.
{
root1 = -b / (2 * a);
printf("Roots are real and equal.\n");
printf("Root = %.2f\n", root1);
}
else // Roots are complex and imaginary
{
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and imaginary.\n");
printf("Root 1 = %.2f + %.2fi\n", realPart, imagPart);
printf("Root 2 = %.2f - %.2fi\n", realPart, imagPart);
}
return 0;
}
Output:
1
Enter coefficients a, b, and c: 1 2 3
Roots are complex and imaginary.
Root 1 = -1.00 + 1.41i
Root 2 = -1.00 - 1.41i
Enter coefficients a, b, and c: 2 4 2
Roots are real and equal.
Root = -1.00
Enter coefficients a, b, and c: 1 4 2
Roots are real and distinct.
Root 1 = -0.59
Root 2 = -3.41
Enter coefficients a, b, and c: 0 2 4
This is not a quadratic equation.
2. Write a C program to find the sum of all the digits and occurrence of a digit in
the number.
#include <stdio.h>
int main() {
long number, temp;
int digit, sum = 0, count = 0, lastDigit;
printf("Enter a number: ");
scanf("%ld", &number);
printf("Enter the digit to count (0-9): ");
scanf("%d", &digit);
//for valid digit
if (digit < 0 || digit > 9) {
printf("Invalid digit! Please enter a digit between 0 and 9.\n");
return 1;
}
temp = number;
//extract digit from the number, add and repeat
while (temp != 0) {
lastDigit = temp % 10;
sum += lastDigit;
if (lastDigit == digit) {
count++;
}
2
temp /= 10;
}
printf("Sum of all digits in %ld = %d\n", number, sum);
printf("Occurrence of digit %d = %d\n", digit, count);
return 0;
}
Output:
Enter a number: 99988
Enter the digit to count (0-9): 9
Sum of all digits in 99988 = 43
Occurrence of digit 9 = 3
3. Write a C program to find the GCD and LCM of given two numbers using
Euclid‘s method.
// Euclid‘s method GCD and LCM without using functions
#include <stdio.h>
int main() {
int num1, num2, a, b, temp, gcd, lcm;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
if (num1 <= 0 || num2 <= 0) {
printf("Please enter positive integers only.\n");
return 1;
}
a = num1;
b = num2;
// Euclid's method (iterative)
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
gcd = a; // Final value of 'a' is GCD
3
// LCM formula
lcm = (num1 * num2) / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
return 0;
}
OUTPUT:
Enter two positive integers: 4 16
GCD of 4 and 16 = 4
LCM of 4 and 16 = 16
Enter two positive integers: 5 3
GCD of 5 and 3 = 1
LCM of 5 and 3 = 15
Enter two positive integers: -5 15
Please enter positive integers only.
// Euclid‘s method-GCD and LCM using Functions
#include <stdio.h>
// Function to compute GCD using Euclid's method
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Function to compute LCM using the formula: LCM = (a * b) / GCD
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2, g, l;
printf("Enter two integers: ");
4
scanf("%d %d", &num1, &num2);
if (num1 <= 0 || num2 <= 0) {
printf("Please enter positive integers only.\n");
return 1;
}
g = gcd(num1, num2); //calling gcd() function
l = lcm(num1, num2);// calling lcm() function
printf("GCD of %d and %d = %d\n", num1, num2, g);
printf("LCM of %d and %d = %d\n", num1, num2, l);
return 0;
}
OUTPUT:
Enter two positive integers: 4 16
GCD of 4 and 16 = 4
LCM of 4 and 16 = 16
Enter two integers: -2 -4
Please enter positive integers only.
4. Write a C program to print the prime numbers in a given range.
// Without using function
#include <stdio.h>
int main() {
int start, end, i, j, isPrime;
printf("Enter the start and end of the range: ");
scanf("%d %d", &start, &end);
if (start < 2) // Prime numbers start from 2
start = 2;
printf("Prime numbers between %d and %d are:\n", start, end);
for (i = start; i <= end; i++) {
isPrime = 1; // Assume prime
// Check divisibility
for (j = 2; j * j <= i; j++) {
if (i % j == 0) {
isPrime = 0; // Not prime
break;
5
}
}
// If still prime, print it
if (isPrime == 1) {
printf("%d ", i);
}
}
return 0;
}
Output:
Enter the start and end of the range: 1 100
Prime numbers between 2 and 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
//Using function
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0)
return false;
}
return true;
}// end of function
int main() {
int start, end;
printf("Enter the start and end of the range: ");
scanf("%d %d", &start, &end);
printf("Prime numbers between %d and %d are:\n", start, end);
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
return 0;
}
6
OUTPUT:
Enter the start and end of the range: 2 200
Prime numbers between 2 and 200 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
5. Write a C program to perform a binary search for a given key integer in a single
dimensional array of numbers in ascending order and report success or failure in the
form of a suitable message
#include <stdio.h>
// Function to perform binary search
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid; // Found, return index
else if (arr[mid] < key)
low = mid + 1; // Search right half
else
high = mid - 1; // Search left half
}
return -1; // Not found
}
int main() {
int arr[100], n, key, i, pos;
printf("Enter number of elements in array: ");
scanf("%d", &n);
printf("Enter %d elements in ascending order:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input key to search
printf("Enter the key to search: ");
scanf("%d", &key);
pos = binarySearch(arr, n, key);// perform binary search
if (pos != -1)
7
printf("Success: %d found at position %d (index %d).\n", key, pos + 1, pos);
else
printf("Failure: %d not found in the array.\n", key);
return 0;
}
Output:
Enter number of elements in array: 6
Enter 6 elements in ascending order:
3 4 5 56 70 80
Enter the key to search: 4
Success: 4 found at position 2 (index 1).
Enter number of elements in array: 5
Enter 5 elements in ascending order:
12345
Enter the key to search: 7
Failure: 7 not found in the array.
6. The books in the library are randomly placed on the shelves. Design a C
program that sorts the books based on ISBN, Use bubble sort to implement the
program.
#include <stdio.h>
int main() {
int n, i, j, temp;
long isbn[100]; // Array to store ISBN numbers
// Input number of books
printf("Enter the number of books: ");
scanf("%d", &n);
// Input ISBN numbers
printf("Enter %d ISBN numbers:\n", n);
for (i = 0; i < n; i++) {
scanf("%ld", &isbn[i]);
}
// Bubble Sort
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;
}
}
}
// Output sorted ISBNs
8
printf("\nBooks sorted by ISBN (ascending order):\n");
for (i = 0; i < n; i++) {
printf("%ld\n", isbn[i]);
}
return 0;
}
Output:
Enter the number of books: 4
Enter 4 ISBN numbers:
4321
Books sorted by ISBN (ascending order):
1
2
3
4
7. Write a C program to find if a given string is a palindrome or not using string
manipulation functions.
#include <stdio.h>
#include <string.h>
//function to reverse a string
void str_rev(char str[]) {
int i, j;
char temp;
int len = strlen(str);
for (i = 0, j = len - 1; i < j; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
int main() {
char str[100], rev[100];
printf("Enter a string: ");
scanf("%s", str);
strcpy(rev, str);
str_rev(rev);
printf("The reversed string is:%s\n", rev);
if (strcmp(str, rev) == 0) {
printf("The string %s is a palindrome.\n", str);
} else {
9
printf("The string %s is not a palindrome.\n", str);
}
return 0;
}
Output:
Enter a string: malayalam
The reversed string is:malayalam
The string malayalam is a palindrome.
Enter a string: NMIT
The reversed string is:TIMN
The string NMIT is not a palindrome.
8. An event registration system takes inputs from the command line:
Participant Name, Age, and Event Name. Write a C program to display the
details.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// Expecting 3 arguments: Name Age Event
if (argc != 4) {
printf("Usage: %s <Name> <Age> <Event>\n", argv[0]);
return 1;
}
// Extract command-line arguments
char *name = argv[1];
int age = atoi(argv[2]); // Convert age to integer
char *event = argv[3];
//Display
printf("\n---- Event Registration Details ----\n");
printf("Participant Name: %s\n", name);
printf("Age : %d\n", age);
printf("Event Name : %s\n", event);
return 0;
}
OUTPUT:
Usage: ./[Link] <Name> <Age> <Event>
./[Link] Ram 25 wedding
---- Event Registration Details ----
Participant Name: Ram
Age : 25
Event Name : wedding
10