Question

In: Computer Science

C programming Get a number from the user and write a program that checks to see...

C programming
Get a number from the user and write a program that checks to see if the inputted number is equal to x*x+x and x must be greater than 0.

For example, if the user inputs 12. The program should check to see if 12 = x*x+x. In this case it is true because 3*3+3 = 12.

Solutions

Expert Solution

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


Related Solutions

In PYTHON: Write a program that asks for a password from a user and checks if...
In PYTHON: Write a program that asks for a password from a user and checks if it satisfies the conditions: Contains at least one lowercase letter (a-z). Contains at least one uppercase letter (A-Z). Contains at least one digit (0-9). Minimum length 6 characters. Contains no spaces. If the password is valid, then the program prints “password accepted”. Otherwise, it keeps asking for more passwords from the user until a valid password is obtained. Hint: You might like to check...
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
c# language Write a program that takes in a number from the user. Then it prints...
c# language Write a program that takes in a number from the user. Then it prints a statement telling the user if the number is even or odd. If the number is odd, it counts down from the number to 0 and prints the countdown on the screen, each number on a new line. If the number is even, it counts down from the number to 0, only even numbers. For example, if the user enters 5, the output will...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube of the number. The program should be called CubeNumbers. Use a value returning method with parameter, call the method myCube.
2. Write a c++ program that takes from the user the ​number of courses​ and constructs...
2. Write a c++ program that takes from the user the ​number of courses​ and constructs 3 ​dynamic 1D arrays​ with size courses+1. Each array represents a student. Each cell in the array represents a student’s mark in a course. In the last cell of each 1D array you should calculate the average mark of that student. Then output the average mark of all students in each course. Delete any allocated memory. Example Number of courses : 4 50 60...
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT