Posts

with rand function simple subtraction quiz generates

 generating random number quiz  #include<iostream> #include<ctime> // for time function #include<cstdlib> // for rand and srand function using namespace std; int main() { // 1. generate two random single-digit integers srand(time(0)); int number1 = rand() % 10; int number2 = rand() % 10; // 2. if number1<number 2 ,sawp number if (number1 < number2) { int temp = number1; number1 = number2; number2 = temp; } //3. Prompt the student to "what is n1-n2?" cout << " what is " << number1 << "-" << number2 <<"? "; int answer; cin >> answer; //4. Grade the answer and display the result if(number1 - number2 == answer) cout << "you are correct!"; else cout << " your answer is wrong. " << number1 << " - " << number2 <<" should be " << (number1-number2) << end

Body Mass Index (BMI) is a measure of health based on height and weight. You can calculate your BMI by taking your weight in kilograms and dividing it by the square of your height in meters

 Body Mass Index (BMI) is a measure of health based on height and weight. You can calculate your BMI by taking your weight in kilograms and dividing it by the square of your height in meters. The interpretation of BMI for people 20 years or older is as follows: BMI Interpretation BMI < 18.5 Underweight 18.5 <= BMI < 25.0 Normal 25.0 <= BMI < 30.0 Overweight 30.0 <= BMI Obese #include<iostream> using namespace std; int main() { //weight in kilograms float kilograms; //height in meters float meters; //enter the values cout<<" enter your weight in kilograms ";cin>>kilograms; cout<<" enter your height in meters ";cin>>meters; //calculate BMI //formula: weight in kilo / (height in meter)*(height in meter) float BMI = kilograms / (meters*meters); cout<<" your BMI is "<<BMI<<endl; if(BMI<18.5) { //BMI < 18.5 Underweight cout<<" you are under

Write an if statement that increases pay by 3% if score is greater than 90.

 Write an if statement that increases pay by 3% if score is greater than 90. #include<iostream> using namespace std; int main() { float pay = 10; int score; cout<<" enter your score :"; cin>>score; if(score > 90) { pay = pay + pay* 3/100; } cout<<"you win price :"<<pay<<endl; return 0; } output:-  enter your score :29 you win price :10  enter your score :100 you win price :10.3

Common Error : Equality Test of Two Floating-Point Values

 Common Error : Equality Test of Two Floating-Point Values #include<iostream> using namespace std; int main() { double x = 0.1+0.1+0.1+0.1; if(x == 0.5) { cout<<"x is equal to 0.5"; } else { cout<<"x is not equal to 0.5"; } return 0; } output:- x is not equal to 0.5

Write an if statement that assigns 1 to x if y is greater than 0.

 Write an if statement that assigns 1 to x if y is greater than 0. /* Write an if statement that assigns 1 to x if y is greater than 0. */ #include<iostream> using namespace std; int main() { int x; int y; cout<<" enter positive or negative number :"; cin>>y; if(y > 0) { x=1; } if(y < 0) { x=0; } cout<<" x is "<<x; return 0; } output:-  enter positive or negative number :123  x is 1  enter positive or negative number :-123  x is 0

checking the input number is even or not

 checking the input number is even or not #include<iostream> using namespace std; int main() { //checking the input number is even or not int num; cout<<"enter a number :"; cin>>num; if(num%2 == 0) { cout<<num<<" number is even number"; } if(num%2 != 0) { cout<<num<<" number is odd number"; } return 0; } output :- enter a number :2 2 number is even number enter a number :1 1 number is odd number

logic operators in cpp

 logic operators in cpp #include<iostream> using namespace std; int main() { cout<<"logic operators"<<endl; if(!false) { cout<<"Not false  !"<<endl; } if(true || false) { cout<<"OR  ||"<<endl; } if(true && 1) { cout<<"And &&"<<endl; } return 0; }