Question

In: Computer Science

Write a C++ program that uses all the relational operators.

Write a C++ program that uses all the relational operators.

Solutions

Expert Solution

An operator is a symbol that is used to perform some manipulations with the values

There are several types of operators:

Arithematic operator

Relational operator

Bitwise operator ,etc

Relational operators are the operators that are used to compare two values.

Syntax:

value1 operator value2

Here , value1 and value2 can be of string , integer or any data type

operator used as part of relational operations are :

< : Less than operator - Checks if left value is less than right value , then the result is true otherwsie false

Example: 5<7 -> true , if 7<5 -> false

> : Greater than operator - Checks if left value is greater than right value , then the result is true otherwsie false

Example: 7>5 -> true , if 5>7 -> false

== : Equal to   - Checks if left value is equal to right value , then the result is true otherwsie false

Example: 7==7 -> true , if 5==7 -> false

<= :   Less than Equal to operator - Checks if left value is less than or equal to right value , then the result is true otherwsie false

Example: 5<=7 -> true , if 7<=7 -> true , if 7<=5 -> false

>= :   Greater than Equal to operator - Checks if left value is greater than or equal to right value , then the result is true otherwsie false

Example: 7>=5 -> true , if 7>=7 -> true , if 5>=7 -> false

!= : Not equal to  - Checks if left value is not equal to right value , then the result is true otherwsie false

Example: 5!=7 -> true , if 7!=7 -> false

Precedence of operators:

>= , > , <= ,< - Higher precedence operators

== , !=   - Lower precedence operators

Meaning , if an expression is having both high and low precedence operator , then higher precedence operators will be evaluated first and low precedence will be evaluated after that

\n -> is a formatting character used for new line . It is used to print ouput in new line.

Code in C++ :

#include <iostream>

using namespace std; //used for input output streams to printoutput /take input in console

int main()
{
cout<<"Relation operators example : "<<"\n";
int value1= 100;
int value2= 70;
//Not Equal to opertor usage
if(value1 != value2){
cout<<"Value1 and Value2 are not equal "<<"\n";
}
else{
cout<<"Value1 and Value2 are equal "<<"\n";
}
//Equal to opertor usage
if(value1 == value2){
cout<<"Value1 and Value2 are equal "<<"\n";
}
else{
cout<<"Value1 and Value2 are not equal "<<"\n";
}
//less than opertor usage
if(value1 < value2){
cout<<"Value1 is less than Value2 "<<"\n";
}
else{
cout<<"Value1 is not less than Value2 "<<"\n";
}
//Greater than opertor usage
if(value1 > value2){
cout<<"Value1 is greater than Value2 "<<"\n";
}
else{
cout<<"Value1 is not greater than Value2 "<<"\n";
}
//value change of value2 variable
value2= 100;
//less than Equal to opertor usage
if(value1 <= value2){
cout<<"Value1 is less than or equal to Value2 "<<"\n";
}
else{
cout<<"Value1 is not less than or equal to Value2 ";
}
//Greater than Equal to opertor usage
if(value1 >= value2){
cout<<"Value1 is greater than or equal to Value2 ";
}
else{
cout<<"Value1 is not greater than or equal to Value2 ";
}
}

OUTPUT:


Related Solutions

Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Relational Operators For Cpp Discuss why relational operators can be used with enumeration types. Provide at...
Relational Operators For Cpp Discuss why relational operators can be used with enumeration types. Provide at least 2 examples of relational expressions with enumeration types. Explain your examples
USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub...
USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub language to be as follows (NOTE: 5 has highest precedence and 0 lowest): 5 *, /, % 4 +, - 3 <, <=, >, >=, !=, == 2 not 1 and 0 or 1.  Infix-Postfix Conversion and Evaluation with Logical and Relational operators – Convert the following infix expression to a postfix expression and evaluate the result (assume that true=1 and false=0). Provide both the...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
In C++, write a program that uses array to calculate the factorial of a reasonable large...
In C++, write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).
In C++. Write a program that uses array to calculate the factorial of a reasonable large...
In C++. Write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).  
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT