Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new

 Categories >> Code Snippets >> Programming Code >> C Code

 C Code Interview Questions (401) C++ Code Interview Questions (121) VC++ Code Interview Questions (2) Java Code Interview Questions (58) Dot Net Code Interview Questions (113) Visual Basic Code Interview Questions (8) Programming Code AllOther Interview Questions (37)
Question
`program to find the roots of a quadratic equation`
Question Submitted By :: C-Code
I also faced this Question!!     Answer Posted By

# 1
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
float a,b,c,x1,x2,disc;
clrscr();
printf("Enter the co-efficients\n");
scanf("%f%f%f",&a,&b,&c);
disc=b*b-4*a*c;/*to find discriminant*/
if(disc>0)/*distinct roots*/
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("The roots are distinct\n");
exit(0);
}
if(disc==0)/*Equal roots*/
{
x1=x2=-b/(2*a);
printf("The roots are equal\n");
printf("x1=%f\nx2=%f\n",x1,x2);
exit(0);
}
x1=-b/(2*a);/*complex roots*/
x2=sqrt(fabs(disc))/(2*a);
printf("The roots are complex\n");
printf("The first root=%f+i%f\n",x1,x2);
printf("The second root=%f-i%f\n",x1,x2);
getch();
}

 Is This Answer Correct ? 325 Yes 117 No
Dhananjay

# 2
//program to find roots of given equation
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float d,p,q,r,s;
printf("\n\n Enter the values of a,b,c")
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d=0)
{
printf("\n The roots are real and equal");
p=(-b)/(2*a);
q=(-b)/(2*a);
printf("\n The roots of the equation are %f,%f",p,q);
}
if(d>0)
{
printf("\n The roots are real and distinct");
p=((-b)+sqrt((b*b)-(4*a*c)))/(2*a);
q=((-b)-sqrt((b*b)-(4*a*c)))/(2*a);
printf("\n The roots of the equation are %f,%f",p,q);
}
if(d<0)
{
printf("\n The roots are imaginary");
r=(-b)/(2*a);
s=(sqrt((4*a*c)-(b*b)))/(2*a);
printf("\n The roots of the equation are
p=%f=%fi,q=%f-%fi",r,s,r,s);
}
}

 Is This Answer Correct ? 109 Yes 41 No
Pratibha

# 3
Lets i see first
 Is This Answer Correct ? 97 Yes 54 No
Pankaj

# 4
#include <iostream.h>
#include <conio.h>
#include <math.h>
int main()
{
clrscr();
float a,b,c,d,root1,root2;
cout << "Enter the 3 coefficients a, b, c : " << endl;
cin>>a>>b>>c;
if(!a){
if(!b)
cout << "Both a and b cannot be 0 in ax^2 + bx + c = 0" << "\n";
else
{
d=-c/b;
cout << "The solution of the linear equation is : " << d << endl;
}
}
else
{
d=b*b-4*a*c;
if(d>0)
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
cout << "The first root = " << root1 << endl;
cout << "The second root = " << root2 << endl;
}
getch();
return 0;
}

 Is This Answer Correct ? 31 Yes 20 No
Vikas Tiwari

# 5
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int main ()
{
float r, a, b, c, x1, x2, d;
printf ("Enter 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);
printf("\n");
d= b*b-4*a*c;
r= sqrt(d);
x1= (-b + r)/(2*a);
x2=(-b- r)/(2*a);
if (d>0)
{
printf ("The real roots of the equation are %f",
x1);
printf (" and %f", x2);
}
else if (x1==x2)
{
x1= (-b+r)/(2*a);
printf ("x1 and x2 are equal roots %f", x1);
}
else if (a<0)
{
x1= (-b + r)/(2*a);
x2=(-b- r)/(2*a);
printf ("The roots of the equation are %f", x1);
printf (" and %f", x2);
}
else
printf ("The roots of the equation are
complex");
getch ();
return 0;
}

 Is This Answer Correct ? 12 Yes 6 No

# 6
//program to find roots of given equation
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float d,p,q,r,s;
printf("\n\n Enter the values of a,b,c")
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d=0)
{
printf("\n The roots are real and equal");
p=(-b)/(2*a);
q=(-b)/(2*a);
printf("\n The roots of the equation are %f,%f",p,q);
}
if(d>0)
{
printf("\n The roots are real and distinct");
p=((-b)+sqrt((b*b)-(4*a*c)))/(2*a);
q=((-b)-sqrt((b*b)-(4*a*c)))/(2*a);
printf("\n The roots of the equation are %f,%f",p,q);
}
if(d<0)
{
printf("\n The roots are imaginary");
r=(-b)/(2*a);
s=(sqrt((4*a*c)-(b*b)))/(2*a);
printf("\n The roots of the equation are
p=%f=%fi,q=%f-%fi",r,s,r,s);
}
}

 Is This Answer Correct ? 32 Yes 28 No
Bawaaaa

# 7
/* PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION WITH
APPROPRIATE ERROR MESSAGE */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,disc,root1,root2,part_r,part_i;
int flag;
clrscr();
printf("Enter the coefficients of quadratic equation \n");
scanf("%f%f%f",&a,&b,&c);
printf("coefficients are a=%f\t b=%f\t c=%f\n",a,b,c);
if((a==0)||(b==0)||(c==0))
printf("\nZero coefficeints");
else
{
disc=(b*b)-(4*a*c);
if(disc==0)
flag=0;
if(disc>0)
flag=1;
if(disc<0)
flag=2;
switch(flag)
{
case0 : printf("\n Roots are real and equal");
root1=-(-b)/(2*a);
root2=root1;
printf("\n root1=%f",root1);
printf("\n root2=%f",root2);
break;
case1 : printf("\n Roots are real and distinct");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("\n root1=%f",root1);
printf("\n root2=%f",root2);
break;
case2 : printf("\n Roots are real and Imaginary");
part_r=(-b)/(2*a);
part_i=(sqrt(-disc))/(2.0*a);
printf("\n root1=%f +i%f",part_r,part_i);
printf("\n root2=%f -i%f",part_r,part_i);
break;
}
}
getch();
}

 Is This Answer Correct ? 2 Yes 0 No
Asif Ahmed

# 8
//program to find roots of given equation
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float d,p,q,r,s;
printf("\n\n Enter the values of a,b,c=");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d==0)
{
printf("\n The roots are real and equal.");
p=(-b)/(2*a);
q=(-b)/(2*a);
printf("\n The roots of the equation are %.0fand%.0f",p,q);
}
if(d>0)
{
printf("\n The roots are real and distinct");
p=((-b)+sqrt((b*b)-(4*a*c)))/(2*a);
q=((-b)-sqrt((b*b)-(4*a*c)))/(2*a);
printf("\n The roots of the equation are %.0fand%.0f",p,q);
}
if(d<0)
{
printf("\n The roots are imaginary");
r=(-b)/(2*a);
s=(sqrt((4*a*c)-(b*b)))/(2*a);
printf("\n The roots of the equation arep=%.2f=%.2fi,q=%.2f-%.2fi",r,s,r,s);
}
getch();
}

 Is This Answer Correct ? 2 Yes 1 No
Rupamjit

# 9
#include<stdio.h>
void main()
{
int a,b,c;
float d,r1,r2;

printf("\n\t\t********************************");

printf("\n\n\tEnter The Coefficent");
printf("\n\t======================");

/*Out put screen */

printf("\n\tCoefficent Of 'X^2' : ");
scanf(%d",&a);

printf("\n\tCoefficent Of 'X' : ");
scanf("%d",&b);

printf("\n\tConstant Value : ");
scanf("%d",&c);

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

if(d<0)
printf('\n\n\tImaginary Roots ");
else if(d==0)
{
r1=(-b)*(2*a);
printf("\n\tEqual Roots");
}
else
{
printf("\n\tReal roots : ");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("\t %g , %g",r1,r2);
}
getch();
}

 Is This Answer Correct ? 0 Yes 0 No
Jijo Alexander

# 10
#include<stdio.h>
int main()
{
int a,b,c,r1,r2,disc;
printf("Enter the number of a,b,c:");
scanf("%d\n%d\n%d",&a,&b,&c);
disc= b*b-4*a*c;
if(disc<0)
{
printf("Roots are imaginary");
}
else if(disc==0)
{
r1=(-b)/a;
r2=r1;
printf("The Root1 and Root2 are equal and roots are
%d\n,%d",r1,r2);
}
else
{
r1=(-b+((b*b)-4*a*c))/2*a;
r2=(-b-((b*b)-4*a*c))/2*a;
printf("The Root1 and Root2 are %d\n,%d\n",r1,r2);
}
}

 Is This Answer Correct ? 0 Yes 0 No
Sachin Kumar

  Question Asked @ Answers Other C Code Interview Questions union u { union u { int i; int j; }a[10]; int b[10]; }u; main() { printf("\n%d", sizeof(u)); printf(" %d", sizeof(u.a)); // printf("%d", sizeof(u.a[4].i)); } a. 4, 4, 4 b. 40, 4, 4 c. 1, 100, 1 d. 40 400 4 HCL 2 struct Foo { char *pName; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); strcpy(obj->pName,"Your Name"); printf("%s", obj->pName); } a. Your Name b. compile error c. Name d. Runtime error HCL 3 main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } Ibm 2 main() { int i = 100; clrscr(); printf("%d", sizeof(sizeof(i))); } a. 2 b. 100 c. 4 d. none of the above HCL 5 There were 10 records stored in “somefile.dat” but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(“somefile.dat”,”r”); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } } 1 void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; } 2 main() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16 HCL 4 What is the subtle error in the following code segment? void fun(int n, int arr[]) { int *p=0; int i=0; while(i++