0% found this document useful (0 votes)
53 views14 pages

Write A C Program To Compute Roots of Quadratic Equation Ax2+bx+c 0, Where A, B, and C Are Three Coefficients of A Quadratic Equation Are Inputs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views14 pages

Write A C Program To Compute Roots of Quadratic Equation Ax2+bx+c 0, Where A, B, and C Are Three Coefficients of A Quadratic Equation Are Inputs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

Write a C program to compute roots of quadratic equation ax2+bx+c=0,


where a, b, and c are three coefficients of a quadratic equation are inputs.

#include <stdio.h>
#include <math.h>

int main() {
float a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b, and 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 distinct: %.2f and %.2f\n", root1, root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("Roots are real and equal: %.2f\n", root1);
} else {
float realPart = -b / (2 * a);
float imagPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex: %.2f + %.2fi and %.2f - %.2fi\n", realPart, imagPart, realPart,
imagPart);
}
return 0;
}

Output:
2. Design and develop an algorithm to find the reverse of an integer number.

#include <stdio.h>

int main() {
int num, reverse = 0;
printf("Enter an integer: ");
scanf("%d", &num);

while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
}
printf("Reversed number: %d\n", reverse);
return 0;
}

Output:
3. Design and develop an algorithm to check whether given number is
PALINDROME or NOT.
Implement a C program for the developed algorithm that takes an integer number
as input and output the reverse of the same with suitable messages. Ex: Num:
2019, Reverse: 9102, Not a Palindrome.

#include <stdio.h>

int main() {
int num, reverse = 0, temp;
printf("Enter an integer: ");
scanf("%d", &num);

temp = num;
while (temp != 0) {
reverse = reverse * 10 + temp % 10;
temp /= 10;
}

printf("Reverse: %d\n", reverse);


if (num == reverse)
printf("The number is a palindrome.\n");
else
printf("The number is not a palindrome.\n");
return 0;
}

Output:
4. Design and develop a c program to implement simple calculator using
switch case statement.

#include <stdio.h>

int main() {
int choice;
float num1, num2, result;

printf("1. Add\n2. Subtract\n3. Multiply\n4. Divide\nEnter your choice: ");


scanf("%d", &choice);

printf("Enter two numbers: ");


scanf("%f %f", &num1, &num2);

switch (choice) {
case 1: result = num1 + num2; break;
case 2: result = num1 - num2; break;
case 3: result = num1 * num2; break;
case 4:
if (num2 != 0)
result = num1 / num2;
else {
printf("Division by zero error.\n");
return 1;
}
break;
default:
printf("Invalid choice.\n");
return 1;
}

printf("Result: %.2f\n", result);


return 0;
}

Output:
5. Develop, implement and execute a C program to search a Number in a list
using linear searching Technique.

#include <stdio.h>

int main() {
int n, i, key, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Enter the number to search: ");


scanf("%d", &key);

for (i = 0; i < n; i++) {


if (arr[i] == key) {
printf("Number found at index %d.\n", i);
found = 1;
break;
}
}

if (!found)
printf("Number not found.\n");

return 0;
}

Output:
6. Develop an algorithm, implement and execute a C program that reads N
integer numbers and arrange them in ascending order using Bubble Sort.

#include <stdio.h>

int main() {
int n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

for (i = 0; i < n - 1; i++) {


for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

printf("Sorted array: ");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

printf("\n");
return 0;
}

Output:
7. Design and develop a C program to read and print a matrix and check
whether a given Matrix is a sparse Matrix or not.

#include <stdio.h>

int main() {
int rows, cols, i, j, count = 0;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);

int matrix[rows][cols];
printf("Enter elements of the matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
if (matrix[i][j] == 0)
count++;
}
}

if (count > (rows * cols) / 2)


printf("The matrix is a sparse matrix.\n");
else
printf("The matrix is not a sparse matrix.\n");

return 0;
}

Output:
8. Write a C program to implements the following string manipulation
functions till the use wishes to continue (infinite loop): (i) strcpy() (ii) srrlen()
(iii) strrev () (iv) strcmp() (v) strcat().

#include <stdio.h>
#include <string.h>

int main() {
char str1[100], str2[100];
int choice;

while (1) {
printf("\n1. strcpy\n2. strlen\n3. strrev\n4. strcmp\n5. strcat\n6. Exit\nEnter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter a string: ");
scanf("%s", str1);
strcpy(str2, str1);
printf("Copied string: %s\n", str2);
break;

case 2:
printf("Enter a string: ");
scanf("%s", str1);
printf("Length of string: %ld\n", strlen(str1));
break;

case 3:
printf("Enter a string: ");
scanf("%s", str1);
strrev(str1);
printf("Reversed string: %s\n", str1);
break;

case 4:
printf("Enter two strings: ");
scanf("%s %s", str1, str2);
int cmp = strcmp(str1, str2);
if (cmp == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
break;

case 5:
printf("Enter two strings: ");
scanf("%s %s", str1, str2);
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
break;

case 6:
return 0;

default:
printf("Invalid choice.\n");
}
}
return 0;
}

Output:
9. Design and develop a C function RightRotate (x, n) that takes two integers x
and n as input and returns value of the integer x rotated to the right by n
positions. Assume the integers are unsigned.

#include <stdio.h>

unsigned int RightRotate(unsigned int x, int n) {


int totalBits = sizeof(x) * 8;
n %= totalBits;
return (x >> n) | (x << (totalBits - n));
}

int main() {
unsigned int x, n;
printf("Enter the number and positions to rotate: ");
scanf("%u %u", &x, &n);
printf("Result after right rotation: %u\n", RightRotate(x, n));
return 0;
}

Output:
10. Draw the flowchart and write a recursive C function to find the factorial
of a number, n!, define by fact(n)=1, if n=0. Otherwise fact (n) =n*fact (n-1).
Using this function, write a C program to compute the binomial coefficient
nCr. Tabulate the results for different values of n and r with suitable messages

#include <stdio.h>

int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}

int binomialCoeff(int n, int r) {


return factorial(n) / (factorial(r) * factorial(n - r));
}

int main() {
int n, r;
printf("Enter n and r: ");
scanf("%d %d", &n, &r);

printf("Factorial of %d: %d\n", n, factorial(n));


printf("Binomial Coefficient C(%d, %d): %d\n", n, r, binomialCoeff(n, r));
return 0;
}

Output:
11. a. Write a C program to maintain a record of n student details using an
array of structures with fourfields (Roll number, Name, Marks, and Grade).
Assume appropriate data type for each field. Input & Print the members of
the structure

#include <stdio.h>
#include <string.h>

struct Student {
int rollNumber;
char name[50];
float marks;
char grade;
};

int main() {
int n, i;
printf("Enter number of students: ");
scanf("%d", &n);

struct Student students[n];


for (i = 0; i < n; i++) {
printf("Enter roll number, name, marks, and grade for student %d: ", i + 1);
scanf("%d %s %f %c", &students[i].rollNumber, students[i].name, &students[i].marks,
&students[i].grade);
}

printf("\nStudent Records:\n");
for (i = 0; i < n; i++) {
printf("Roll: %d, Name: %s, Marks: %.2f, Grade: %c\n", students[i].rollNumber,
students[i].name, students[i].marks, students[i].grade);
}
return 0;
}

Output:
11.b. Write a C program to take 2 structures HH:MM: SS as T1 & T2 &
display the time difference as structure as T3.

#include <stdio.h>

struct Time {
int hours, minutes, seconds;
};

struct Time calculateDifference(struct Time t1, struct Time t2) {


struct Time diff;

int t1_seconds = t1.hours * 3600 + t1.minutes * 60 + t1.seconds;


int t2_seconds = t2.hours * 3600 + t2.minutes * 60 + t2.seconds;

int total_seconds = t1_seconds - t2_seconds;


if (total_seconds < 0)
total_seconds += 24 * 3600;

diff.hours = total_seconds / 3600;


total_seconds %= 3600;
diff.minutes = total_seconds / 60;
diff.seconds = total_seconds % 60;

return diff;
}

int main() {
struct Time t1, t2, diff;
printf("Enter time 1 (HH MM SS): ");
scanf("%d %d %d", &t1.hours, &t1.minutes, &t1.seconds);
printf("Enter time 2 (HH MM SS): ");
scanf("%d %d %d", &t2.hours, &t2.minutes, &t2.seconds);

diff = calculateDifference(t1, t2);


printf("Time difference: %02d:%02d:%02d\n", diff.hours, diff.minutes, diff.seconds);
return 0;
}

Output:
12. Write a C program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of n real numbers.

#include <stdio.h>
#include <math.h>

int main() {
int n, i;
float sum = 0, mean, stddev = 0;

printf("Enter number of elements: ");


scanf("%d", &n);

float arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%f", &arr[i]);
sum += arr[i];
}

mean = sum / n;
for (i = 0; i < n; i++) {
stddev += pow(arr[i] - mean, 2);
}
stddev = sqrt(stddev / n);

printf("Sum: %.2f\n", sum);


printf("Mean: %.2f\n", mean);
printf("Standard Deviation: %.2f\n", stddev);

return 0;
}

Output:

You might also like