Like Us On Facebook & Follow Us On Twitter

Tuesday, 19 November 2013

Calculate Roots Of a Quadratic Equation Using C Program


Hi everyone
In this post, we're going how to write a C program to calculate the roots of a quadratic equation. As we know that a quadratic equation is of a form like:

 ax^2+bx+c=0

So we won't go into much details. Also you guys must be knowing to calculate the roots of a quadratic equation directly, having formula:

Roots = ( -b + (b^2 - 4ac) ) / 2a ( -b - (b^2 - 4ac) ) / 2a

So we are just going to use the same formula in our C code. So Let's begin...

C Program To Calculate Roots Of a Quadratic Equation:

/* Double-Click To Select Code */

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

void main()
{
float a,b,c,root1,root2,x,real,im;
clrscr();

printf("-> Quadratic Equation Form: ax2+bx+c");
printf("\n\nEnter the value of a: ");
scanf("%f",&a);
printf("\nEnter the value of b: ");
scanf("%f",&b);
printf("\nEnter the value of c: ");
scanf("%f",&c);

x = (b*b)-(4*a*c);

if(x>0)
{
root1 = (-b+sqrt(x))/(2*a);
root2 = (-b-sqrt(x))/(2*a);       //ROOTS ARE UNIQUE
printf("\nThe roots of the equation are: %.2f and %.2f",root1,root2);
}

else if(x==0)
{
root1 = -b/(2*a);
root2 = root1;  //ROOTS ARE SAME
printf("\nThe roots of the equation are: %.2f and %.2f",root1,root2);
}

else
{
real = -b/(2*a);
im = sqrt(-x)/(2*a);  // ROOTS ARE COMPLEX
printf("\nThe roots of the equation are: %.2f+j%.2f and %.2f-j%.2f",real,im,real,im);
}

getch();
}

Program Explanation:

The program is self explanatory in itself. First we take the coefficients of the quadratic equation as input from the user. Then on the basis of its determinant,i.e, b^2 - 4*a*c, if its greater than 0 or equal to 0 or less than 0. In each of the case, we can get different types of roots, Unique, Equal or Complex respectively. Using the formula as I described in the beginning of this post, we calculate roots, with an exception in case of complex roots, whereby, we have to find real and imaginary parts of the roots individually, using the formula:
Real Part = -b/(2*a)
Imaginary Part = sqrt(-x)/(2*a) -> Since determinant is negative here, we make it positive by multiplying an extra '-'.


Program Output:

C Program To Calculate Roots Of a Quadratic Equation

Please Comment If You Liked The Post.

Tuesday, 6 August 2013

C Code To Concatenate Two Strings


Hello guys,
In this post, I'll explain the C Code To Concatenate Two Strings. 

As we all know that, the process of appending one string to the end of another string is called concatenation. If s1 and s2 are two strings and when s2 is appended to the end of s1, s1 and s2 are said to be concatenated. The string s1 then contains the original string plus the string contained in s2. Similar to copying and comparing, appending s2 to s1 should be done on a character by character basis. C provides strcat() built-in function to concatenate two strings.


The prototype of strcat() is as follows:

          strcat(s1,s2);

C Program To Concatenate Two Strings:

/* Double-Click To Select Code */

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

void main()
{ 
 char str1[20]="abc", str2[20]="def", str3[20]="ghi";
 int i,j;
 clrscr();

 printf("str1 = %s | str2 = %s \n",str1,str2);

/* Concatenation of str1 and str2 without using strcat() begins */
 
 for(i=0 ; str1[i]!='\0' ; i++);
 for(j=0 ; str2[j]!='\0' ; j++)
 {
  str1[i+j] = str2[j];
 }
 
 str1[i+j]='\0';
 printf("\nAfter concatenation str1 = %s \n",str1);

/* Concatenation of str1 and str2 without using strcat() begins */
 strcat(str1,str3);
 printf("\nstr1 = '%s' \n",str1);
 strcat(str1,"jkl");
 printf("\nstr1 = '%s' \n",str1);

 printf("\n\n *WWW.CODINGBOT.NET*");
 getch();
}

Program Explanation:

Str1, str2 and str3 are declared to be arrays of char type and all are initialized strings. The integer variables i and j are to traverse the strings. String in str2 is appended to the end of the string  in str1 by following segment of the program:

for(i=0 ; str1[i]!='\0' ; i++);
for(j=0 ; str2[j]!='\0' ; j++)
{
str1[i+j] = str2[j];
}
str1[i+j] = '\0';
The first for loop is to simply scan through the string str1. When the loop completes, the variable i points to the position of null character '\0' in str1. The second loop is to scan through the second string str2 till end of it is reached. When j takes 0, the first character in str2 is assigned to the position of null character in str1. So, the null character is str1 is overwritten by the first character in str2. Subsequently, the remaining characters in str2 are appended to str1 and lastly, the null character '\0' is assigned to the last position of str1. The string in str1, "abcdef" is then displayed.

Then two calls are mode to strcat(). In the final call, strcat(str1,str3), str3 is appended to str1. Since str3 had "ghi", sstr1 now becomes "abcdefghi" and is displayed. On the second call, strcat(str,"jkl"), the string constant "jkl" is appended to str1. The new string "abcdefghijkl" in str1 is again displayed.

Program Output:

C Code To Concatenate Two Strings


Please Comment If You Liked The Post.

Sunday, 4 August 2013

C Code To Find The Length Of a String


Hello guys,
In this post, I'll explain the C Code how to find the length of a string, by 2 methods, first by using strlen() function and second by normal programming.

Length of a string is defined to be the number of characters in it excluding the null character '\0'. To find the length of a string, we need to scan through the string, count the number of characters till the null character is reached. C provides a built-in function namely, strlen() to find the length of a string. 

C Program To Find The Length Of a String:


/* Double-Click To Select Code */

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

void main()
{ 
 char str[100];
 int i,length;
 clrscr();

 printf("Enter a String: ");
 gets(str);

/* Finding the length of str without using strlen() begins */

 length=0;
 for(i=0 ; str[i]!='\0' ; i++)
 length++;
 printf("\nLength of '%s' = %d\n",str,length);

/* Finding the length of str using strlen() begins */

 length = strlen(str);
 printf("Length of '%s' using strlen = %d \n",str,length);

 printf("\n\n *WWW.CODINGBOT.NET*");
 getch();
}

Program Explanation:

str is declared to be an array of char type and of size 100. Length and i are declared to be variables of int type. str is to collect the input string. Variable length is to collect the length of the string in str. The variable i is to scan through the string accessing each character.

A string is read into str through the keyboard  The following segment finds the length of str without strlen():

                          length = 0;
                          for(i=0 ; str[i] != '\0' ; i++)
                                  length++;

Length is initialized to 0 before scanning through the string begins. When the loop is entered, scanning through the string begins. During the course of scanning, if the character is not fund to be null character '\0', length is incremented by one. The loop is exited when the null character is reached. So, when the loop completes, length collects the length of str.
The length of str is found out using strlen() built-in function also.

Program Output:


C Code To Find The Length Of a String

Please Comment If You Liked The Post.