Posts

13: A shop will give discount of 10% if the cost of purchased items is equal to 1000 or more. Each item in shop costs Rs.100. Ask user for quantity of items he got. Judge and print total cost for user (with discount deducted if any).

     // A shop will give discount of 10% if the cost of purchased items is equal to 1000 or more. // Each item in shop costs Rs.100 // Ask user for quantity of items he got. // Judge and print total cost for user (with discout deducted if any). #include <iostream> using namespace std ; int main (){     int quantity , gross , net , discount ;         cout << "Enter the quantity of items: " ;         cin >> quantity ;         gross = quantity * 100 ;             if ( gross >= 1000 ){                 discount = gross * 0.1 ;                 net = gross - discount ;                 cout << "Total payable amount: Rs." << net << endl <<                   ...

12: A company decided to give bonus of 5% to employee if his/her year of service is less than 5 years, and 10% if more than 5 years. Ask user for their salary and year of service and print the net bonus amount.

    // A company decided to give bonus of 5% to employee if his/her year of service is less than 5 years, // and 10% if more than 5 years. // Ask user for their salary and year of service and print the net bonus amount. #include <iostream> using namespace std ; int main (){         int salary , year , annual_sal , bonus , bonus5 ;             cout << "Enter your monthly salary: Rs." ;             cin >> salary ;             cout << "Enter your years of service: " ;             cin >> year ;             annual_sal = salary * 12 ;             if ( year >= 5 ){                 bonus5 = 0.1 * annual_sal ;                 cout << "You got bonus of R...

11: Write a program that takes an integer as input and prints whether it is even or odd

    // Write a program that takes an integer as input and prints whether it is even or odd #include <iostream> using namespace std ; int main (){         int num ;         cout << "Enter the number: " ;         cin >> num ;                     if ( num % 2 == 0 ){             cout << "The number is 'even' \n \n " ;}                         else cout << "The number is 'odd' \n \n " ;                 cout << "Now you have your answer \n " ;         return 0 ; } Input 1: Enter the number: 54 Output 1: The number is 'even' Now you have your answer   Input 2: Enter the number: 67 Output 2: The number is 'odd' Now you have your answer

10: Write a program that takes salary amount from the user and applies tax to it. If salary is greater than 100000 deduct 10% tax otherwise deduct 5%. Print the net salary, and amount of tax deducted.

   // Write a program that takes salary amount from the user and applies tax to it. // If salary is greater than 100000 deduct 10% tax otherwise deduct 5%. // Print the net salary, and amount of tax deducted. #include <iostream> using namespace std ; int main (){         float gross_salary , five_tax , ten_tax , net_sal_five , net_sal_ten ;         cout << "Enter your monthly gross salary: Rs." ;         cin >> gross_salary ;             if ( gross_salary >= 100000 ){                 ten_tax = gross_salary * 0.1 ;                 net_sal_ten = gross_salary - ten_tax ;                 cout << "Your net salary is: Rs." << net_sal_ten << endl <<                   ...

9: Write a program that takes three sides of a triangle from the user and print if the triangle is equilateral, isosceles or scalene.

  // Write a program that takes three sides of a triangle from the user and // print if the triangle is equilateral or isosceles. #include <iostream> using namespace std ; int main () {         float numa, numb, numc;                     cout << "Enter first side length: " ;             cin >> numa;                         cout << "Enter second side length: " ;             cin >> numb;                         cout << "Enter third side length: " ;             cin >> numc;                             if (numa==numb&&numb==numc)                 { ...

8: Write a C++ program for 2nd equation of motion

  #include <iostream> using namespace std ; int main (){     float vi , t , a ;     cout << "Enter the value of initial velocity (in meter per second) = " ;     cin >> vi ;     cout << "Enter the value of time (in seconds) = " ;     cin >> t ;     cout << "Enter the value of acceleretion (in meter per square second) =" ;     cin >> a ;     float d = vi * t + 0.5 * a * t * t ;     cout << "The distance thus obtained is = " << d ;     return 0 ; } Input: Enter the value of initial velocity (in meter per second) = 2.5 Enter the value of time (in seconds) = 5 Enter the value of acceleretion (in meter per square second) = 1.75 Output: The distance thus obtained is = 34.375

7: Write a C++ program to calculate aggregate of MBBS

  #include <iostream> using namespace std ; int main (){     float matric_obt , matric_outof ;     cout << "Marks you obtained in Matric = " ;     cin >> matric_obt ;     cout << "out of = " ;     cin >> matric_outof ;     float matric = matric_obt / matric_outof * 10 ;     float inter_obt , inter_outof ;     cout << "Marks you obtained in Intermediate = " ;     cin >> inter_obt ;     cout << "out of = " ;     cin >> inter_outof ;     float inter = inter_obt / inter_outof * 40 ;     float mcat_obt , mcat_outof ;     cout << "Marks you obtained in MDCAT = " ;     cin >> mcat_obt ;     cout << "out of = " ;     cin >> mcat_outof ;     float mdcat = mcat_obt / mcat_outof * 50 ;     float aggregate = matric + int...

6: Ali's father gave him some money and his mother gave him some money. Take input of both amount and print the sum

  #include <iostream> using namespace std ; main (){     int father , mother ;     cout << "Father gave Ali rupees= " ;     cin >> father ;     cout << "Mother  gave Ali rupees= " ;     cin >> mother ;     int total = father + mother ;     cout << "Ali have total=" << total << "rupees" ;     return 0 ; } Input: Father gave Ali rupees= 500 Mother  gave Ali rupees= 678 Output: Ali have total= 1178rupees

5: Program to check size of data types

#include <iostream> using namespace std ; main (){     int a;     cout<< "The size of int is " << sizeof (a)<<endl;     float b;     cout<< "The size of float is " << sizeof (b)<<endl;     char c;     cout<< "The size of char is " << sizeof (c)<<endl;     double d;     cout<< "The size of double is " << sizeof (d)<<endl;     bool e;     cout<< "The size of bool is " << sizeof (e)<<endl;     return 0 ; } Output:     The size of int is 4           The size of float is 4         The size of char is 1           The size of double is 8         The size of bool is 1 Thus:     The size of int is 4 byte           The size of float ...

4(c): ***(Syntax)*** Take full name ====>(with spaces)<==== as input and print it

  // Take full name ====>(with spaces)<==== as input and print it #include <iostream> using namespace std ; int main (){         string name ;         cout << "Enter your full name: " ;         getline (cin >> ws, name );     // Use ws to avoid input buffer             cout << "Your name is: " << name << endl ;     return 0 ; } Input:     Enter your full name: Muhammad Umar Chaudhry Output:     Your name is: Muhammad Umar Chaudhry

4(b): ***(Syntax)*** Take name ====>(without spaces)<==== as input and print it

    // Take name ===>(without spaces)<=== and print it. #include <iostream> using namespace std ; int main (){     string name ;     cout << "Enter the name: " ;     cin >> name ;     cout << name ;     return 0 ; } Input:     Enter the name: Umar Output:     Umar

4(a): ***(Syntax)*** Constants, Variables and Keywords

Image
  Keywords:                   Keywords are  reserved words whose meaning is already known to the compiler  and they can not be used elsewhere in the program for naming a variable or a function.  ==> List of C++ keywords is:  https://en.cppreference.com/w/cpp/keyword Constants: Constants are very similar to a variable and they can also be of any data type. The only difference between a constant and a variable is that a  constant’s value never changes . Types of constants:          There are three types of constants:    Integer Constants  ===================>    -1,    6,    7,    9  (all non-fraction numbers)    Real Constants  =====================>    -30.1,     2.5,    7.0   (all fraction numbers)    Character Constants  ==============...