//Develop a program to calculate the temperature converter from degree to Fahrenheit
#include <stdio.h>
int main() {
float celsius, fahrenheit;
// Input temperature in Celsius
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
// Convert Celsius to Fahrenheit
fahrenheit = (celsius * 9 / 5) + 32;
// Display the result
printf("Temperature in Fahrenheit = %.2f\n", fahrenheit);
return 0;
}
// Develop a program to find the roots of quadratic equations.
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2, realPart, imagPart;
// Input coefficients
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
// Calculate discriminant
discriminant = b * b - 4 * a * c;
// Check the nature of roots
if (discriminant > 0) {
// Real and distinct roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct.\n");
printf("Root1 = %.2f and Root2 = %.2f\n", root1, root2);
}
else if (discriminant == 0) {
// Real and equal roots
root1 = root2 = -b / (2 * a);
printf("Roots are real and equal.\n");
printf("Root1 = Root2 = %.2f\n", root1);
}
else {
// Complex roots
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and imaginary.\n");
printf("Root1 = %.2f + %.2f i and Root2 = %.2f - %.2f i\n",
realPart, imagPart, realPart, imagPart);
}
}
// Develop a program to find whether a given number is prime or not.
/* What Is a Prime Number?
A prime number is a number greater than 1 that has no positive divisors other than 1 and
itself.
Examples: 2, 3, 5, 7, 11, 13, ... */
#include <stdio.h>
int main() {
int num, i, flag = 0;
// Input number
printf("Enter a positive integer: ");
scanf("%d", &num);
// 0 and 1 are not prime numbers
if (num <= 1) {
printf("%d is not a prime number.\n", num);
return 0;
}
// Check divisibility from 2 to num/2
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
// Display result
if (flag == 0)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}
/*What is Linear Search?
Linear search sequentially compares each element of the array with the target key:
If found, it returns the position.
If not found, it indicates that the element is not present*/
// Develop a program to find key elements in an array using linear search
#include <stdio.h>
int main() {
int arr[100], n, i, key, found = 0;
// Input number of elements
printf("Enter number of elements in the array: ");
scanf("%d", &n);
// Input array elements
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Input the element to search
printf("Enter the key element to search: ");
scanf("%d", &key);
// Perform linear search
for (i = 0; i < n; i++)
{
if (arr[i] == key)
{
printf("Key element %d found at position %d.\n", key, i + 1);
found = 1;
break;
}
}
// If key not found
if (!found)
{
printf("Key element %d not found in the array.\n", key);
}
return 0;
}
// Given age and gender of a person, develop a program to categorise senior citizen (male & female).
// Male: Age ≥ 60 Female: Age ≥ 58
#include <stdio.h>
int main() {
char name[50], gender;
int age;
// Input name, gender, and age
printf("Enter the person's name: ");
scanf("%s", name); // Reads name (no spaces)
printf("Enter gender (M/F): ");
scanf(" %c", &gender); // space before %c to avoid newline issue
printf("Enter age: ");
scanf("%d", &age);
// Check category
if ((gender == 'M' || gender == 'm') && age >= 60) {
printf("%s is a senior citizen (Male).\n", name);
}
else if ((gender == 'F' || gender == 'f') && age >= 58) {
printf("%s is a senior citizen (Female).\n", name);
}
else {
printf("%s is NOT a senior citizen.\n", name);
}
return 0;
}
// Given age and gender of a person, develop a program to categorise senior citizen (male & female).
// Male: Age ≥ 60 Female: Age ≥ 58
#include <stdio.h>
int main() {
char name[50], gender;
int age;
// Input name, gender, and age
printf("Enter the person's name: ");
scanf("%s", name); // Reads name (no spaces)
printf("Enter gender (M/F): ");
scanf(" %c", &gender); // space before %c to avoid newline issue
printf("Enter age: ");
scanf("%d", &age);
// Check category
if ((gender == 'M' || gender == 'm') && age >= 60) {
printf("%s is a senior citizen (Male).\n", name);
}
else if ((gender == 'F' || gender == 'f') && age >= 58) {
printf("%s is a senior citizen (Female).\n", name);
}
else {
printf("%s is NOT a senior citizen.\n", name);
}
return 0;
}
/* Generate Floyd’s triangle for given rows.
1
23
456
7 8 9 10
11 12 13 14 15 */
#include <stdio.h>
int main()
{
int rows, i, j, num = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Generate Floyd’s Triangle
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ", num);
num++;
}
printf("\n"); // Move to next line after each row
}
return 0;
}
// Develop a program to find the transpose of a matrix.
/* The transpose of a matrix is obtained by flipping rows into columns (or vice versa).
If you have a matrix A of size m x n, its transpose A^T will be of size n x m. */
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10];
int row, col, i, j;
// Input number of rows and columns
printf("Enter number of rows and columns: ");
scanf("%d %d", &row, &col);
// Input elements of matrix
printf("Enter elements of the matrix:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
scanf("%d", &a[i][j]);
}
}
// Find transpose
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
transpose[j][i] = a[i][j];
}
}
// Display original matrix
printf("\nOriginal Matrix:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
// Display transpose matrix
printf("\nTranspose of the Matrix:\n");
for (i = 0; i < col; i++) {
for (j = 0; j < row; j++) {
printf("%d\t", transpose[i][j]);
}
printf("\n");
}
return 0;
}
/* Develop a program to concatenate two strings, find length of a string and copy one string
to other using string operations.
Operations:
1. Concatenate two strings
2. Find the length of a string
3. Copy one string to another */
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], str3[100];
int length;
// Step 1: Input two strings
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
// Step 2: Concatenate str1 and str2
strcat(str1, str2);
printf("\nAfter concatenation: %s\n", str1);
// Step 3: Find length of concatenated string
length = strlen(str1);
printf("Length of concatenated string: %d\n", length);
// Step 4: Copy concatenated string into another string
strcpy(str3, str1);
printf("Copied string: %s\n", str3);
return 0;
}
/* Function to calculate GCD using while loop
The gcd() function uses a while loop based on Euclid’s algorithm. The loop continues until the remainder
becomes zero.
The lcm() function uses the formula:LCM(a, b) = (a × b) / GCD(a, b).
*/
#include <stdio.h>
int gcd(int a, int b)
{
int temp;
while (b != 0)
{
temp = b;
b = a % b;
a = temp;
}
return a;
}
/* Function to calculate LCM */
int lcm(int a, int b)
{
return (a * b) / gcd(a, b);
}
//---------------------------------------------------------
int main()
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d = %d\n", num1, num2, gcd(num1, num2));
printf("LCM of %d and %d = %d\n", num1, num2, lcm(num1, num2));
return 0;
}
/*Develop a program to declare the structure of employees and display the employee records with
higher salary among two employees.*/
#include <stdio.h>
/* Structure declaration */
struct employee
{
int emp_id;
char emp_name[30];
float salary;
};
int main()
{
struct employee e1, e2;
/* Input details of first employee */
printf("Enter details of Employee 1\n");
printf("ID: ");
scanf("%d", &e1.emp_id);
printf("Name: ");
scanf("%s", e1.emp_name);
printf("Salary: ");
scanf("%f", &[Link]);
/* Input details of second employee */
printf("\nEnter details of Employee 2\n");
printf("ID: ");
scanf("%d", &e2.emp_id);
printf("Name: ");
scanf("%s", e2.emp_name);
printf("Salary: ");
scanf("%f", &[Link]);
/* Compare salaries and display result */
printf("\nEmployee with higher salary:\n");
if ([Link] > [Link])
{
printf("ID: %d\nName: %s\nSalary: %.2f\n",
e1.emp_id, e1.emp_name, [Link]);
}
else if ([Link] > [Link])
{
printf("ID: %d\nName: %s\nSalary: %.2f\n",
e2.emp_id, e2.emp_name, [Link]);
}
else
{
printf("Both employees have equal salary: %.2f\n", [Link]);
}
return 0;
}
// Develop a program to add two numbers using the pointers to the variables.
#include <stdio.h>
int main()
{
int num1, num2, sum;
int *ptr1, *ptr2;
// Input two numbers
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Assign addresses to pointers
ptr1 = &num1;
ptr2 = &num2;
// Add values using pointers
sum = *ptr1 + *ptr2;
// Display result
printf("Sum = %d\n", sum);
return 0;
}
// Develop a program to find the sum of digits of a given number.
#include <stdio.h>
int main() {
int num, sum = 0, remainder;
// Input number
printf("Enter a number: ");
scanf("%d", &num);
// Loop to extract digits and add them
while (num != 0) {
remainder = num % 10; // Get last digit
sum += remainder; // Add digit to sum
num /= 10; // Remove last digit
}
// Display result
printf("Sum of digits = %d\n", sum);
return 0;
}
// Develop a program to perform Matrix Multiplication.
#include <stdio.h>
int main() {
int m1, n1, m2, n2;
int i, j, k;
// Input dimensions of first matrix
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &m1, &n1);
// Input dimensions of second matrix
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &m2, &n2);
// Check if multiplication is possible
if (n1 != m2) {
printf("Matrix multiplication not possible!\n");
return 0;
}
int A[m1][n1], B[m2][n2], C[m1][n2];
// Input first matrix
printf("Enter elements of first matrix:\n");
for (i = 0; i < m1; i++) {
for (j = 0; j < n1; j++) {
scanf("%d", &A[i][j]);
}
}
// Input second matrix
printf("Enter elements of second matrix:\n");
for (i = 0; i < m2; i++) {
for (j = 0; j < n2; j++) {
scanf("%d", &B[i][j]);
}
}
// Initialize result matrix to 0
for (i = 0; i < m1; i++) {
for (j = 0; j < n2; j++) {
C[i][j] = 0;
}
}
// Matrix multiplication
for (i = 0; i < m1; i++) {
for (j = 0; j < n2; j++) {
for (k = 0; k < n1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
// Display result
printf("Resultant Matrix:\n");
for (i = 0; i < m1; i++) {
for (j = 0; j < n2; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
// Develop a program to create an array of structures to store book details and check whether
// a specific book, as requested by the user, is available or not.
#include <stdio.h>
#include <string.h>
// Define structure for book
struct Book {
char title[50];
char author[50];
int id;
};
int main() {
int n, i;
char searchTitle[50];
int found = 0;
// Input number of books
printf("Enter number of books: ");
scanf("%d", &n);
// Declare array of structures
struct Book library[n];
// Input book details
for (i = 0; i < n; i++) {
printf("\nEnter details of book %d:\n", i + 1);
printf("Enter Book ID: ");
scanf("%d", &library[i].id);
getchar(); // consume newline
printf("Enter Book Title: ");
fgets(library[i].title, sizeof(library[i].title), stdin);
library[i].title[strcspn(library[i].title, "\n")] = '\0'; // remove newline
printf("Enter Author Name: ");
fgets(library[i].author, sizeof(library[i].author), stdin);
library[i].author[strcspn(library[i].author, "\n")] = '\0'; // remove newline
}
// Ask user for book to search
printf("\nEnter the title of the book to search: ");
getchar(); // consume newline if needed
fgets(searchTitle, sizeof(searchTitle), stdin);
searchTitle[strcspn(searchTitle, "\n")] = '\0'; // remove newline
// Search for the book
for (i = 0; i < n; i++) {
if (strcmp(library[i].title, searchTitle) == 0) {
found = 1;
printf("\nBook Found!\n");
printf("Book ID: %d\n", library[i].id);
printf("Title: %s\n", library[i].title);
printf("Author: %s\n", library[i].author);
break;
}
}
if (!found) {
printf("\nSorry, the book '%s' is not available in the library.\n", searchTitle);
}
return 0;
}