Develop a program to find the roots of quadratic equations
#include <stdio.h>
#include <math.h>
int main(void) {
double a, b, c;
double D, realPart, imagPart;
printf("Quadratic equation: a*x^2 + b*x + c = 0\n");
printf("Enter coefficients a, b and c (space separated): ");
if (scanf("%lf %lf %lf", &a, &b, &c) != 3) {
printf("Invalid input. Exiting.\n");
return 1;
}
/* Handle non-quadratic case */
if (a == 0.0) {
if (b == 0.0) {
if (c == 0.0) {
printf("Infinite number of solutions (0 = 0).\n");
} else {
printf("No solution (contradiction: %.6g = 0).\n", c);
}
} else {
double x = -c / b;
printf("Linear equation. Single root: x = %.6f\n", x);
}
return 0;
}
/* Quadratic case */
D = b * b - 4.0 * a * c;
if (D > 0.0) {
double sqrtD = sqrt(D);
double x1 = (-b + sqrtD) / (2.0 * a);
double x2 = (-b - sqrtD) / (2.0 * a);
printf("Two distinct real roots:\n");
printf("x1 = %.6f\nx2 = %.6f\n", x1, x2);
} else if (D == 0.0) {
double x = -b / (2.0 * a);
printf("One real repeated root:\n");
printf("x = %.6f\n", x);
} else { /* D < 0: complex roots */
double sqrtAbsD = sqrt(-D);
realPart = -b / (2.0 * a);
imagPart = sqrtAbsD / (2.0 * a);
printf("Two complex conjugate roots:\n");
printf("x1 = %.6f + %.6fi\n", realPart, imagPart);
printf("x2 = %.6f - %.6fi\n", realPart, imagPart);
}
return 0;
}
Develop a program to find whether a given number is prime or not.
#include <stdio.h>
int main(void) {
long long n; // Variable to store the input number
int isPrime = 1; // Assume number is prime until proven otherwise
// Ask the user for an integer input
printf("Enter an integer: ");
if (scanf("%lld", &n) != 1) { // Validate that input is an integer
printf("Invalid input.\n");
return 1; // Exit with error code 1 if input is invalid
}
// Step 1: Check for numbers <= 1 (not prime)
if (n <= 1) {
printf("%lld is NOT prime.\n", n);
return 0;
}
// Step 2: 2 is the only even prime number
if (n == 2) {
printf("%lld is PRIME.\n", n);
return 0;
}
// Step 3: Eliminate all other even numbers quickly
if (n % 2 == 0) {
printf("%lld is NOT prime (even).\n", n);
return 0;
}
// Step 4: Check for factors only up to √n using i <= n/i (avoids floating-point)
// Only odd divisors are tested because even ones are already skipped
for (long long i = 3; i <= n / i; i += 2) {
if (n % i == 0) { // If n is divisible by i, it's not prime
isPrime = 0;
break; // No need to check further, exit loop
}
}
// Step 5: Display result based on the flag
if (isPrime)
printf("%lld is PRIME.\n", n);
else
printf("%lld is NOT prime.\n", n);
return 0; // Successful program termination
}
Develop a C program to find key elements in an array using linear search.
Linear search is the simplest searching technique.
It checks each element of the array one by one until the key element is found or the end of the
array is reached.
Algorithm
1. Start from the first element of the array.
2. Compare the key element with each element of the array.
3. If a match is found, return the index (or position).
4. If the loop ends without finding the key, print that the element is not found.
Mathematical Explanation of Linear Search
Suppose we have:
• An array:
A= [a1, a2, a3, …, an]
• A key element (target):
k
We want to find whether k exists in the array A.
Basic Comparison Rule
We compare k with each element of the array one by one.
Mathematically:
For i=1 to n Check if ai=k
If a match is found, Position of element =i
Otherwise, continue checking the next element.
Using Conditional Expression
We can express this mathematically using the indicator function:
f(i)=1,if ai=k
f(i)= 0,if ai≠k
If for any f(i)=1 then the element is found at index i.
If f(i)=0 for all i=1,2,…,n then the element is not found in the array.
Example
A=[10,20,30,40,50], k=30
i ( ai ) Comparison ( ai = k )? ( f(i) )
1 10 10 = 30 → No 0
2 20 20 = 30 → No 0
3 30 30 = 30 → Yes 1
4 40 (stop, already found) —
So, f(3)=1, which means key found at position 3.
#include <stdio.h>
int main() {
int n, key, i, found = 0;
int arr[100];
// Step 1: Input array size
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Step 2: Input array elements
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
// Step 3: Input the key element to search
printf("Enter the key element to search: ");
scanf("%d", &key);
// Step 4: Perform linear search
for (i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Element %d found at position %d.\n", key, i + 1);
found = 1;
break; // Stop searching once found
// Step 5: If not found
if (!found) {
printf("Element %d not found in the array.\n", key);
return 0;
}
Given age and gender of a person, develop a program to categories senior citizen
(male & female).
#include <stdio.h> // Include standard input/output library
int main() {
int age; // Variable to store the person's age
char gender; // Variable to store gender (M/F)
// Ask user for gender
printf("Enter gender (M/F): ");
scanf(" %c", &gender); // The space before %c helps skip any leftover newline
// Ask user for age
printf("Enter age: ");
scanf("%d", &age);
// Check gender and age to determine senior citizen status
if ((gender == 'M' || gender == 'm') && age >= 60) {
printf("The person is a male senior citizen.\n");
else if ((gender == 'F' || gender == 'f') && age >= 58) {
printf("The person is a female senior citizen.\n");
else {
printf("The person is not a senior citizen.\n");
return 0; // End of program
}
Generate Floyd’s triangle for given rows
#include <stdio.h> // Include standard input/output library
int main() {
int rows, num = 1; // 'rows' stores number of rows, 'num' starts from 1
// Prompt the user to enter how many rows they want
printf("Enter number of rows: ");
scanf("%d", &rows); // Read user input and store it in 'rows'
// Outer loop: controls the number of rows
for (int i = 1; i <= rows; i++) {
// Inner loop: prints numbers in each row
for (int j = 1; j <= i; j++) {
printf("%d ", num); // Print the current number
num++; // Increment the number for next print
}
printf("\n"); // Move to the next line after finishing one row
}
return 0; // Return 0 indicates successful program execution
}
Case 1: Enter number of rows: 5
1
23
456
7 8 9 10
11 12 13 14 15
Case 2: Enter number of rows: 5
15 14 13 12 11
10 9 8 7
654
32
1
C Program: Floyd’s Triangle in Opposite (Reverse) Direction
#include <stdio.h>
int main() {
int rows, num = 1, lastNum = 0;
// Ask user for the number of rows
printf("Enter number of rows: ");
scanf("%d", &rows);
// Calculate the last number in the triangle
// Formula for total elements in Floyd's Triangle = n*(n+1)/2
lastNum = rows * (rows + 1) / 2;
// Print triangle in reverse
for (int i = rows; i >= 1; i--) { // Start from bottom row and go upward
for (int j = 1; j <= i; j++) {
printf("%d ", lastNum--); // Print number and decrement
}
printf("\n"); // Move to next line after each row
}
return 0;
}
Develop a program to find the transpose of a matrix
#include <stdio.h> // Include standard input/output library
int main() { // Start of main function
int a[10][10], transpose[10][10]; // Declare matrices (max size 10x10)
int rows, cols; // Variables to store number of rows and columns
printf("Enter number of rows: "); // Ask user for number of rows
scanf("%d", &rows); // Read number of rows
printf("Enter number of columns: "); // Ask user for number of columns
scanf("%d", &cols); // Read number of columns
printf("Enter elements of the matrix:\n"); // Prompt user for matrix input
for (int i = 0; i < rows; i++) { // Loop through rows
for (int j = 0; j < cols; j++) { // Loop through columns
scanf("%d", &a[i][j]); // Read matrix element
}
}
// Compute transpose: swap rows with columns
for (int i = 0; i < rows; i++) { // Loop through rows
for (int j = 0; j < cols; j++) { // Loop through columns
transpose[j][i] = a[i][j]; // Assign transposed element
}
}
// Print the original matrix
printf("\nOriginal Matrix:\n");
for (int i = 0; i < rows; i++) { // Loop through rows
for (int j = 0; j < cols; j++) { // Loop through columns
printf("%d ", a[i][j]); // Print each element
}
printf("\n"); // New line after each row
}
// Print the transpose of the matrix
printf("\nTranspose of the Matrix:\n");
for (int i = 0; i < cols; i++) { // Loop through transpose rows
for (int j = 0; j < rows; j++) { // Loop through transpose columns
printf("%d ", transpose[i][j]); // Print transposed element
}
printf("\n"); // New line after each row
}
return 0; // End program
}
Enter number of rows: 2
Enter number of columns: 3
Enter elements of the matrix:
123
456
Original Matrix:
123
456
Transpose of the Matrix:
14
25
36
Develop a program to concatenate two strings, find length of a string and copy one
string to other using string operations
#include <stdio.h> // Standard I/O library
#include <string.h> // String library for strcat, strlen, strcpy
int main() {
char str1[100], str2[100], copy[100]; // Declare character arrays
// Input first string
printf("Enter first string: ");
gets(str1); // Read string (unsafe but simple for demonstration)
// Input second string
printf("Enter second string: ");
gets(str2);
// 1. FIND LENGTH OF STRING
int len1 = strlen(str1); // Length of first string
int len2 = strlen(str2); // Length of second string
printf("\nLength of first string: %d\n", len1);
printf("Length of second string: %d\n", len2);
// 2. CONCATENATE STRINGS
strcat(str1, str2); // Concatenate str2 to str1
printf("\nAfter concatenation: %s\n", str1);
// 3. COPY STRING
strcpy(copy, str1); // Copy str1 into copy[]
printf("Copied string: %s\n", copy);
return 0;
}
Enter first string: Hello
Enter second string: World
Length of first string: 5
Length of second string: 5
After concatenation: HelloWorld
Copied string: HelloWorld
Develop a program to declare the structure of employees and display the
employee records with higher salary among two employees.
#include <stdio.h> // Include standard input/output header
// Declare a structure named employee
struct employee {
int id; // Employee ID
char name[50]; // Employee name
float salary; // Employee salary
};
int main() {
struct employee e1, e2; // Declare two employee variables
// Input details for first employee
printf("Enter Employee 1 ID: ");
scanf("%d", &[Link]);
printf("Enter Employee 1 Name: ");
scanf("%s", [Link]);
printf("Enter Employee 1 Salary: ");
scanf("%f", &[Link]);
// Input details for second employee
printf("\nEnter Employee 2 ID: ");
scanf("%d", &[Link]);
printf("Enter Employee 2 Name: ");
scanf("%s", [Link]);
printf("Enter Employee 2 Salary: ");
scanf("%f", &[Link]);
// Compare salaries
printf("\n--- Employee with Higher Salary ---\n");
if ([Link] > [Link]) {
// If first employee has higher salary
printf("ID: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("Salary: %.2f\n", [Link]);
} else if ([Link] > [Link]) {
// If second employee has higher salary
printf("ID: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("Salary: %.2f\n", [Link]);
} else {
// If both salaries are equal
printf("Both employees have equal salary.\n");
}
return 0; // End of program
}
Enter Employee 1 ID: 101
Enter Employee 1 Name: Aaron
Enter Employee 1 Salary: 55000
Enter Employee 2 ID: 102
Enter Employee 2 Name: Bharath
Enter Employee 2 Salary: 60000
--- Employee with Higher Salary ---
ID: 102
Name: Bharath
Salary: 60000.00
Develop a program to add two numbers using the pointers to the variables.
#include <stdio.h> // Standard input/output header
int main() {
int a, b, sum; // Declare three integer variables
int *p1, *p2; // Declare two integer pointers
// Input two numbers from the user
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
// Assign the addresses of variables to pointers
p1 = &a; // p1 now points to variable 'a'
p2 = &b; // p2 now points to variable 'b'
// Adding values using pointers
sum = *p1 + *p2; // Dereference pointers and add the values
// Display the result
printf("\nUsing pointers:\n");
printf("Value of a = %d\n", *p1);
printf("Value of b = %d\n", *p2);
printf("Sum = %d\n", sum);
return 0; // End of program
}
Enter first number: 15
Enter second number: 25
Using pointers:
Value of a = 15
Value of b = 25
Sum = 40
Develop a c program to find the sum of digits of a give number
#include <stdio.h> // Standard input/output library
int main() {
int num, temp, sum = 0; // Declare variables
// Get number from user
printf("Enter a number: ");
scanf("%d", &num);
temp = num; // Keep a copy of the original number
// Loop to extract and add digits
while (temp > 0) {
sum += temp % 10; // Add last digit to sum
temp = temp / 10; // Remove last digit
}
// Display result
printf("Sum of digits of %d = %d\n", num, sum);
return 0; // End of program
}
Enter a number: 9876
Sum of digits of 9876 = 30
Iteration 1
• temp = 9876
• Extract last digit:
temp % 10 = 6
• Add to sum:
sum = 0 + 6 = 6
• Remove last digit:
temp = 9876 / 10 = 987
Iteration 2
• temp = 987
• Extract last digit:
987 % 10 = 7
• Add to sum:
sum = 6 + 7 = 13
• Remove last digit:
temp = 987 / 10 = 98
Iteration 3
• temp = 98
• Extract last digit:
98 % 10 = 8
• Add to sum:
sum = 13 + 8 = 21
• Remove last digit:
temp = 98 / 10 = 9
Iteration 4
• temp = 9
• Extract last digit:
9 % 10 = 9
• Add to sum:
sum = 21 + 9 = 30
• Remove last digit:
temp = 9 / 10 = 0