Posts

Showing posts from 2021

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; }

Relation Operators in cpp

 Relation Operators in cpp #include<iostream> using namespace std; int main() { cout<<"Relation Operators"<<endl; cout<<"less than "<<"( < )"<<" 5<5  ans "<<(5<5)<<endl; cout<<"greater than "<<"( > )"<<" 5>5 ans "<<(5>5)<<endl; cout<<"less than or equal to"<<"( <= )"<<" 5<=5 ans "<<(5<=5)<<endl; cout<<"greater than or equal to"<<"( >= )"<<" 5>=5 ans "<<(5>=5)<<endl; cout<<"equal to "<<"( == )"<<" 5==5 ans "<<(5==5)<<endl; cout<<"not equal to "<<"( != )"<<" 5!=5 ans "<<(5!=5)<<endl; bool condition = 4>5; cout<<"4>5 ="<<cond...

if else if else syntax in cpp

 if else if else syntax in cpp #include<iostream> using namespace std; int main() { if(true) { cout<<"if true"<<endl; } else if(false) { cout<<"if false"<<endl; } else { cout<<"not true or false"<<endl; } return 0; }

if nested in Cpp

 if inside if syntax in cpp #include<iostream> using namespace std; int main() { if(true) { cout<<"first true"<<endl; if(true) { cout<<"second true"<<endl; if(true) {    cout<<"third true"<<endl; } if(false) { //not show output } } if(false) { //not show output } } return 0; }

if syntax in c++

 if syntax in c++ #include<iostream> using namespace std; int main() { //this statment is show output if(true) { cout<<"it is true"<<endl; } if(1) { cout<<"it is also true"<<endl; } //this statment not show output if(false) { cout<<"it is false "<<endl; } if(0) { cout<<"it is also false"<<endl; } return 0; }

if else in c++

 if else syntax in c++ #include<iostream> using namespace std; int main() { //if statement is true then not go to else if(true) { cout<<"if is ture"<<endl; } else { cout<<"else is false"<<endl; } //if statment is false then go to else if(false) { cout<<"if is false"<<endl; } else { cout<<"else is true"<<endl; } if(1) { cout<<"if is true"<<endl; } else { cout<<"else is false"<<endl; } if(0) { cout<<"if is false"<<endl; } else { cout<<"else is true"<<endl; } return 0; }

Write a program that reads the subtotal and the gratuity rate, then computes the gratuity and total. For example, if the user enters 10 for subtotal and 15% for gratuity rate, the program displays $1.5 as gratuity and $11.5 as total.

Image
(Financial application: calculate tips) Write a program that reads the subtotal and  the gratuity rate, then computes the gratuity and total. For example, if the user enters  10 for subtotal and 15% for gratuity rate, the program displays $1.5 as gratuity and  $11.5 as total. #Beginner In gcc c++ compiler using namespace std; #include<iostream> using namespace std; int main() { float total , gratuity_rate ; cout<< " enter sub total and gratuity rate :" ; cin>>total>>gratuity_rate; float gratuity = total*gratuity_rate/100; float final_total = gratuity + total; cout<< "gratuity $" <<gratuity<< " final total is $" <<final_total; return 0; } Output enter sub total and gratuity rate :10 15 gratuity $1.5 final total is $11.5 [Program finished]

Write a program that converts pounds into kilo- grams. The program prompts the user to enter a number in pounds, converts it to kilograms, and displays the result. One pound is 0.454 kilograms.

Image
(Convert pounds into kilograms) Write a program that converts pounds into kilo- grams. The program prompts the user to enter a number in pounds, converts it to  kilograms, and displays the result. One pound is 0.454 kilograms. #Beginner In gcc c++ compiler using namespace std; #include<iostream> using namespace std; int main() { float pound; cout<< "enter the pound :" ; cin>>pound; float kilogram = pound * 0.454 ; cout<< "pound " <<pound<< " is kilogram " <<kilogram; return 0; } Output enter the pound :55.5 pound 55.5 is kilogram 25.197 [Program finished]