Showing posts with label Roots. Show all posts
Showing posts with label Roots. Show all posts

FIND OUT GENERIC ROOT OF A NUMBER - C PROGRAM.

How to FIND OUT GENERIC ROOT OF A NUMBER BY C PROGRAM.


#include<stdio.h>
int main(){
long int num,sum,r;
printf("\nEnter a number:-");
scanf("%ld",&num);
while(num>10){
sum=0;
while(num){
r=num%10;
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
printf("\nSum of the digits in single digit is: %ld",sum);
return 0;
}

Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.

Solution For C Program - 

Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.


#include <iostream>
#include <math.h>
#include <windows.h>

using namespace std;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);



double findUnknown(int, int);
double findUnknown(double, double);

double findUnknown(int, int, int);
double findUnknown(double, double, double);

void testFindUnknow2IntType(int test, int a, int b , double expected)
{
double result = findUnknown(a,b);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow2DoubleType(int test, double a, double b, double expected)
{
double result = findUnknown(a, b);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow3IntType(int test, int a, int b, int c, double expected)
{
double result = findUnknown(a, b, c);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow3DoubleType(int test, double a, double b, double c, double expected)
{
double result = findUnknown(a, b, c);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}


void main()
{
int menuButton = 0;
int show = 0;

int a = 0;
int b = 0;
int c = 0;

cout << "1.Line\n2.Square\n";
cin >> menuButton;

if (menuButton == 1)
{
/*cout << "1. a*x + b = 0" << endl;
cin >> a >> b;

cout << findUnknown(a, b) << endl;*/

testFindUnknow2IntType(1, 2, 4, -2);
testFindUnknow2IntType(2, 1, 5, -5);
testFindUnknow2DoubleType(3, 4, 2, 0.5);
testFindUnknow2DoubleType(4, 5, 2.5 , 0.5);

}

if (menuButton == 2)
{
/*cout << "2. a*x^2 + b*x + c = 0" << endl;
cin >> a >> b >> c;

show = findUnknown(a, b, c);
cout << "X2 = " << show << endl;*/

testFindUnknow3IntType(1, 1, -2, -3, -1);
testFindUnknow3IntType(2, 1, 5, 6, -3);
testFindUnknow3DoubleType(3, 0.5, 6, 0, -12);
testFindUnknow3DoubleType(4, 12, 5, -0.5, -0.5);

}
}


double findUnknown(int a, int b)
{
double x = b * (-1) / a;
return x;
}

double findUnknown(double a, double b)
{
double x = b / a;
return x;
}

double findUnknown(int a, int b, int c)
{
int discriminant = b*b - 4 * a * c;

double x1 = (b*(-1) - sqrt(discriminant)) / 2 * a;
//double x2 = (b*(-1) - sqrt(discriminant)) / 2 * a;

return x1;
}

double findUnknown(double a, double b, double c)
{
double discriminant = b*b - 4 * a * c;

double x1 = (b*(-1) - sqrt(discriminant)) / ( 2 * a );
//double x2 = (b*(-1) - sqrt(discriminant)) / ( 2 * a );

return x1;
}

C program to find the roots of a quadratic equation

How To write a C program to find the roots of a quadratic equation in C Programming Language ?


Solution:


//To write a C program to find the roots of a quadratic equation
#include<stdio.h>
#include<math.h>
int main()
{ 
 float a,b,c,x1,x2;
 printf("Please enter three numbers: \na= \n");
 scanf("%f",&a);
 printf("b= \n");
 scanf("%f",&b);
 printf("c= \n");
 scanf("%f",&c);
 if(b*b-4*a*c < 0)
  printf("Complex");
 else
 {
  x1=(-b+sqrt((b*b)-(4*a*c)))/(2*a);
  x2=(-b-sqrt((b*b)-(4*a*c)))/(2*a);
  printf("The quadratic roots for the given values of a,b and c are = \nx1=%f \nx2=%f",x1,x2);
 } 
 return 0;
}