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

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# 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....
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...
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 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..
PYTHON Write a program that accepts a range of input from the user and checks whether...
PYTHON Write a program that accepts a range of input from the user and checks whether the input data is sorted or not. If the data series is already sorted your program should print “True” or should print “False” otherwise. You should not use any sort function for this program. Input: How many numbers you want to input: 3 # user input 3 Input the number: 5 Input the number: 2 Input the number: 7 Output: False
Python Programming 4th edition: Write a program that asks the user for the number of hours...
Python Programming 4th edition: Write a program that asks the user for the number of hours (float) and the pay rate (float) for employee pay role sheet. The program should display the gross pay with overtime if any and without overtime. Hints: Given base_hr as 40 and ovt_multiplier as1.5, calculate the gross pay with and Without overtime. The output should look like as follows: Enter the number of hours worked: Enter the hourly pay rate: The gross pay is $XXX.XX
C programming. Write a program that prompts the user to enter a 6x6 array with 0...
C programming. Write a program that prompts the user to enter a 6x6 array with 0 and 1, displays the matrix, and checks if every row and every column have the even number of 1’s.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT