In: Computer Science
C Program
Input : A number from user
Output ; Any x such that x*x + x = num
Code is
#include <stdio.h>
#include <stdbool.h>
void main()
{
// declare number and x
int num,x;
// check is a boolean variable and initialize it to false
bool check =false;
//take number from user
printf("Enter the Number: ");
scanf("%d",&num);
// check if any x less than given number satisfy the x *x + x
=num
for( x=1;x< num;x++)
{
// if any x satisfy then make check as true and break the
loop
if( x*x + x==num)
{
check=true;
break;
}
}
// if check is true the print true and value of x else print false
.
if( check)
printf("True and The Value of x is %d",x);
else
{
printf( "False , No value of x exists ");
}
}
Code Screen Shot
Output
Another Approach ( As asked by You )
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
void main()
{// declare number and x1,x2
int num,x1,x2;
//take number from user
printf("Enter the Number: ");
scanf("%d",&num);
// find roots of quadratic equation x^2 + x - num = 0 using
quagratic formula
x1= (-1 - sqrt(1+ 4*1 * num) ) /2;
x2= (-1 + sqrt(1+ 4*1 * num) ) /2;
// if check any root x1 or x2 is >0 and satisfy the relation x^2
+ x - num = 0 then result is true the print true and value of x
else print false .
if( x1*x1+ x1==num && x1>0)
printf("True and The Value of x is %d",x1);
else if( x2*x2+ x2==num && x2>0)
{
printf("True and The Value of x is %d",x2);
}
else
{
printf( "False , No value of x exists ");
}
}
This is how we can write the program in C to check if the inputted number is equal to x*x+x
If u like the answer then Upvote it and have any doubt ask in comments
Thank You