 # C Programming Code Examples

## C > If Else and Switch Case Code Examples

### C program to find all roots of a quadratic equation

``` C program to find all roots of a quadratic equation Write a C program to find all roots of a quadratic equation using if else. How to find all roots of a quadratic equation using if else in C programming. Quadratic equation Wikipedia states, in elementary algebra a quadratic equation is an equation in the form of ax^2+bx+c=0 Logic to find all roots of a quadratic equation Based on the above formula let us write step by step descriptive logic to find roots of a quadratic equation. Input coefficients of quadratic equation from user. Store it in some variable say a, b and c. Find discriminant of the given equation, using formula discriminant = (b*b) - (4*a*c). Compute roots based on the nature of discriminant. If discriminant > 0 then, root1 = (-b + sqrt(discriminant)) / (2*a) and root2 = (-b - sqrt(discriminant)) / (2*a). If discriminant == 0 then, root1 = root2 = -b / (2*a). Else if discriminant < 0 then, there are two distinct complex roots where root1 = -b / (2*a) and root2 = -b / (2*a). Imaginary part of the root is given by imaginary = sqrt(-discriminant) / (2*a). After this much reading let us finally code the solution of this program. #include <stdio.h> #include <math.h> /* Used for sqrt() */ int main() { float a, b, c; float root1, root2, imaginary; float discriminant; printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): "); scanf("%f%f%f", &a, &b, &c); /* Find discriminant of the equation */ discriminant = (b * b) - (4 * a * c); /* Find the nature of discriminant */ if(discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2*a); root2 = (-b - sqrt(discriminant)) / (2*a); printf("Two distinct and real roots exists: %.2f and %.2f", root1, root2); } else if(discriminant == 0) { root1 = root2 = -b / (2 * a); printf("Two equal and real roots exists: %.2f and %.2f", root1, root2); } else if(discriminant < 0) { root1 = root2 = -b / (2 * a); imaginary = sqrt(-discriminant) / (2 * a); printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f", root1, imaginary, root2, imaginary); } return 0; } ```

Program Solve The 0-1 Knapsack Problem - C++ Program to solve 0-1 'knapsack problem'. The 'Knapsack' problem or Rucksack problem is a problem in "Combinatorial Optimization": Given a Set of items, each with a mass, value,

C Count Occurrences of a Word in a String - Write a C program to Count Occurrences of a word in a given string using loop. Input string and word from user. Get the total number of occurrences of a word in a string. Print count

C++ Implement Stack using Two Queues - This C++ program implements a "stack data" structure using two queue data structures. A stack data structure foolows the principle of LIFO(last element in first element out). Enter

Find sum of array elements using pointers - C Program code find sum of array elements using pointers, recursion & functions. Sum of array elements using Recursion: Function calling itself. This program calls user defined

Code Passing Array to Function using Call - When we pass the address of an array while calling a function then this is called function call by reference. When we pass an address as an argument, the function declaration...

C++ Program Implements Fibonacci Heap - Link nodes in fibonnaci heap. Union nodes in fibonnaci heap. Extract min node in fibonnaci heap. Consolidate node in fibonnaci heap and Decrease key of nodes in fibonnaci heap. Find

Code Display the IP Address of the System - Create a socket to define network interface IPv4. Define the IPv4 address type. Define the port name where network is attached. Access the network interface information...

Find the Frequency of Substring in Strings - Take a string and a substring as input. Store it in the arrays 'str[] & str1[]' respectively. Using For Loop to 'compare' str1[] with the str[]. Do step2 until the end of main string. During the