Posts

Showing posts with the label Beginner

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...

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

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]

bool data type in c++ program

 Boolean data type is true or false (0 or 1) . This type is use in all of condition statements Example : 5>3 is True but 5<3 is false. The boolean data returns 0 or 1 it is more useful for computer to think on statement. In gcc c++ compiler using namespace std; #include<iostream> using namespace std; int main() { bool number; //main value number=0; cout<< "bool hold " <<number<<endl; number=1; cout<< "bool hold " <<number<<endl; //assign the negative and postive value number = -3; cout<< "bool hold " <<number<<endl; number = 3; cout<< "bool hold " <<number<<endl; return 0; } copy output: bool hold 0 bool hold 1 bool hold 1 bool hold 1 Need of Programmer Need of Programmer

Write a program that prompts the user to enter the min- utes (e.g., 1 billion), and displays the number of years and days for the minutes. For simplicity, assume a year has 365 days

Image
(Find the number of years) Write a program that prompts the user to enter the min- utes (e.g., 1 billion), and displays the number of years and days for the minutes. For  simplicity, assume a year has 365 days. #Beginner In gcc c++ compiler using namespace std; #include<iostream> using namespace std; int main() { int minutes; cout<< "enter the minutes : " ; cin>>minutes; const int year_per_minutes=365*24*60; const int day_per_minutes=24*60; int year=minutes/year_per_minutes; int day=(minutes%year_per_minutes)/(day_per_minutes); cout<< " " <<minutes<< " minutes is aproximately " <<year<< " years and " <<day<< " days" ; return 0; } copy Output enter the minutes : 1000000000  1000000000 minutes is aproximately 1902 years and 214 days [Program finished] Need of Programmer Need of Programmer ...

ShowCurrenttime gives a program that displays the current time in GMT. Revise the program so that it prompts the user to enter the time zone offset to GMT and displays the time in the specified time zone.

Image
(Current time) ShowCurrenttime gives a program that displays the  current time in GMT. Revise the program so that it prompts the user to enter the time  zone offset to GMT and displays the time in the specified time zone.  #Beginner #Intermediate In gcc c++ compiler using namespace std; #include<iostream> #include<ctime> using namespace std; int main() { int totalsec = time(0); int currentsec = totalsec % 60; int totalminutes= totalsec / 60; int currentminutes=totalminutes % 60; int totalhrs=totalminutes / 60; int currenthrs=totalhrs % 24; int gmt; cout<< "enter GMT zone -12 to +12 :" ; cin>>gmt; int currentgmthrs=(totalhrs+gmt)%24; cout<< "main time  " <<currenthrs<< " :" <<currentminutes<< " :" <<currentsec<< " GMT +0" <<endl; cout<< "your time " <<currentgmthrs<< " :" <<currentm...

Average acceleration is defined as the change of velocity divided by the time taken to make the change, as shown in the following formula:

(Physics: acceleration) Average acceleration is defined as the change of velocity  divided by the time taken to make the change, as shown in the following formula: a = (v1 - v0)/t  Write a program that prompts the user to enter the starting velocity v0 in meters/ second, the ending velocity v1 in meters/second, and the time span t in seconds, and  displays the average acceleration.  #Beginner In gcc c++ compiler using namespace std; #include<iostream> using namespace std; int main() { float  velocity1,velocity0,time,acceleration; cout<< "Enter velocity0 m/s velocity1 m/s time /s :" ; cin>>velocity0>>velocity1>>time; acceleration=(velocity1-velocity0)/time; cout<< "The avrage acceleration is " <<acceleration; return 0; } copy Output Enter velocity0 m/s velocity1 m/s time /s :5 50 4 The avrage acceleration is 11.25 [Program finished] Need of Programmer Need of...

Write a program that calculates the energy needed to heat water from an initial temperature to a final temperature. Your program should prompt the user to enter the amount of water in kilograms and the initial and final temperatures of the water.

Image
(Science:calculating energy ) Write a program that calculates the energy needed to  heat water from an initial temperature to a final temperature. Your program should  prompt the user to enter the amount of water in kilograms and the initial and final  temperatures of the water. The formula to compute the energy is Q = M * (finalTemperature – initialTemperature) * 4184  where M is the weight of water in kilograms, temperatures are in degrees Celsius, and  energy Q is measured in joules.  #Beginner In gcc c++ compiler using namespace std; #include<iostream> using namespace std; int main() { float M,finalTemperature,initialTemperature; cout<< "Enter te amount of water in kilograms :" ; cin>>M; cout<< "Enter the initial temperature :" ; cin>>initialTemperature; cout<< "Enter te finalTemperature :" ; cin>>finalTemperature; float Q=M*(finalTemperature-initialTemperature)*4184; cout<< "Th...

Write a program that prompts the user to enter v in meters/second (m/s) and the acceleration a in meters/second squared (m/s²), and displays the minimum runway length.

Image
(Physics: finding runway length) Given an airplane’s acceleration a and take-off  speed v, you can compute the minimum runway length needed for an airplane to take  off using the following formula: length = v²/2a  Write a program that prompts the user to enter v in meters/second (m/s) and the accel- eration a in meters/second squared (m/s²), and displays the minimum runway length. #Beginner In gcc c++ compiler using namespace std; #include<iostream> using namespace std; int main() { float speed,acceleration,length; cout<< "Enter speed and acceleration :" ; cin>>speed>>acceleration; length=( float )(speed*speed)/(2*acceleration); cout<< "The minimum runway length for this airplane is :" <<length; return 0; } copy Output Enter speed and acceleration :60 3.5 The minimum runway length for this airplane is :514.286 [Program finished] Need of Programmer Need of Programmer

Financial application c++ program

Image
(Financial application: compound value) Suppose you save $100 each month into a  savings account with the annual interest rate 5%. Thus, the monthly interest rate is  0.05/12 = 0.00417. After the first month, the value in the account becomes 100 * (1 + 0.00417) = 100.417   After the second month, the value in the account becomes (100 + 100.417) * (1 + 0.00417) = 201.252   After the third month, the value in the account becomes (100 + 201.252) * (1 + 0.00417) = 302.507   and so on.  Write a program that prompts the user to enter a monthly saving amount and displays  the account value after the sixth month #Beginner In gcc c++ compiler using namespace std; #include<iostream> using namespace std; int main() { float amount,total=0; cout<< "Enter te monthly saving amount $ :" ; cin>>amount; total=amount*(1+0.00417); //1st total=(amount+total)*(1+0.00417); //2nd total=(amount+total)*(1+0.00417); //3rd total=(...

Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their distance between them.The formula for computing the distance is

Image
(Geometry: distance of two points) Write a program that prompts the user to enter  two points (x1, y1) and (x2, y2) and displays their distance between them.The formula for computing the distance is  √ (x2 - x1)²+ (y2 - y1)² . Note that you  can use pow(a, 0.5) to compute √a.  #Beginner In gcc c++ compiler using namespace std; #include<iostream> #include<cmath> using namespace std; int main() { float x1,y1,x2,y2; cout<< "Enter x1 and y1 :" ; cin>>x1>>y1; cout<< "Enter x2 and y2 :" ; cin>>x2>>y2; float distance=pow((pow(x2-x1,2))+pow(y2-y1,2),0.5); cout<< "The distance between the two points is :" <<distance; return 0; } copy Output Enter x1 and y1 :1 9 Enter x2 and y2 :3 -5 The distance between the two points is :14.1421 [Program finished] Need of Programmer Need of Programmer