Tuesday, March 31, 2015

Circle area and cylinder volume --- calling functions


Script

#include <iostream>
using namespace std;
#include <string>
#include <cmath>

const double PI=3.14159;


double getPositiveValueRadius(string message1)
{
        double number=0;

        do
        {
        cout << message1 << endl;
        cin >> number;
        }
        while (number <= 0);
        return number;
}

double getPositiveValueHeight(string message2)
{
        double number=0;

        do
        {
        cout << message2 << endl;
        cin >> number;
        }
        while (number <= 0);
        return number;

}

double areaOfCircle(double center_to_end)
{
        return PI*pow(center_to_end,2);
}

double volOfCylinder(double rad,double hght)
{
return PI*pow(rad,2)*hght;
}


int main()
{
        double radius = getPositiveValueRadius("Enter the radius of the circle/cylinder: ");
        double height = getPositiveValueHeight("Enter the height of the cylinder: ");
        double aCircle = areaOfCircle(radius);
        double vCylinder = volOfCylinder(radius,height);

        cout << "The area of the circle that has a radius of " << radius << " inches is " << aCircle << " square inches." << endl;
        cout << "The volume of the cylinder that has a radius of " << radius << " inches and ";
        cout << "height of " << height << " inches is " << vCylinder << " cubic inches." << endl;

}


Execution

Enter the radius of the circle/cylinder:
5
Enter the height of the cylinder:
10
The area of the circle that has a radius of 5 inches is 78.5397 square inches.
The volume of the cylinder that has a radius of 5 inches and height of 10 inches is 785.398 cubic inches.





Enter the radius of the circle/cylinder:
7.87
Enter the height of the cylinder:
21.39
The area of the circle that has a radius of 7.87 inches is 194.58 square inches.
The volume of the cylinder that has a radius of 7.87 inches and height of 21.39 inches is 4162.07 cubic inches.


Getting into UCees --- nested if statements

Script

// If you have a GPA of more than 3.75 in high school and if you also have a SAT score of more than 2250, you can get into
// UC Berkeley.  Provided you have the above two criteria, if your essay is outstanding, you can get into Computer Science major
//  in UC Berkeley.  If your essay is deemed excellent, you can get into Political Science.  If your essay is anything less than 
// excellent, you will be admitted to Liberal Arts.
// If you have a GPA between 3.25 and 3.75 and if you have also scored more than 2250 in SAT, you will get admission 
// into UC Los Angeles.
// Provided you have the two above-mentioned criteria, if your essay is outstanding you will be admitted to Psychology.
// If you essay is excellent but not outstanding you will be able to take up Economics as major.  
// If the essay is anything less than excellent,
// you will be able to get into Neuroscience as your major.
// If your GPA in High School is greater than or equal to 3.25, you will be required to get into Community college.  
// Less than 3.25 on your GPA being the case, if you score 2100 or more in SAT, you will be able to 
// pursue Anthropology in Community college.
// However if you get less than 2100, you will have to settle for Social Sciences in Community college.

#include <iostream>
using namespace std;
#include <string>

int main()
{
        string name="";
        cout << "What is your name, please ? : ";
        getline(cin,name);

        double gpaScore=0;
        int satScore=0;
        string essayRating="";
        cout << "What is your High School GPA score ? : ";
        cin >> gpaScore;
        cout << "What is your SAT score ? : ";
        cin >> satScore;
        cout << "Specify what rating you have received for your Essay (Outstanding, Excellent, Good, Moderate, Poor) : ";
        cin >> essayRating;

        if (gpaScore > 3.75 && satScore > 2250)
        {
                if (essayRating == "Outstanding")
                cout << "Congratulations, you are admitted to Computer Science in UC Berkeley.\n";
                else if (essayRating == "Excellent")
                cout << "You are admitted to Political Science in UC Berkeley.\n";
                else
                cout << "You need to take up Liberal Arts in UC Berkeley.\n";

        }

        else if (gpaScore >= 3.25 && gpaScore <= 3.75 && satScore > 2250)
        {
                if (essayRating == "Outstanding")
                cout << "You are admitted to Psychology in UC Los Angeles.\n";
                else if (essayRating == "Excellent")
                cout << "You are eligible to take up Economics as major in UC Los Angeles.\n";
                else
                cout << "You can join UC Los Angeles and do Neuroscience as your major.\n";

        }

        else if (gpaScore >= 3.25 && satScore >= 2100 && satScore <= 2250)
                cout << "You are admitted to Community college for majoring in Anthropology.\n";
                else
                cout << "Please get enrolled into Social Sciences in this Community college.\n";


}


Execution

What is your name, please ? : Kiran DS
What is your High School GPA score ? : 3.8
What is your SAT score ? : 2254
Specify what rating you have received for your Essay (Outstanding, Excellent, Good, Moderate, Poor) : Excellent
You are admitted to Political Science in UC Berkeley.




What is your name, please ? : Roger Smith
What is your High School GPA score ? : 3.87
What is your SAT score ? : 2360
Specify what rating you have received for your Essay (Outstanding, Excellent, Good, Moderate, Poor) : Outstanding
Congratulations, you are admitted to Computer Science in UC Berkeley.



What is your name, please ? : Bill Blofeld
What is your High School GPA score ? : 3.9
What is your SAT score ? : 2240
Specify what rating you have received for your Essay (Outstanding, Excellent, Good, Moderate, Poor) : Excellent
You are admitted to Community college for majoring in Anthropology.


What is your name, please ? : Vikram Kashyap
What is your High School GPA score ? : 3.6
What is your SAT score ? : 2270
Specify what rating you have received for your Essay (Outstanding, Excellent, Good, Moderate, Poor) : Excellent
You are eligible to take up Economics as major in UC Los Angeles.



Specific character count in a word --- for loop and do while loop

Script

// Ask the user to input a word that has a minimum of 8 characters
// Ask the user to type in a specific character
// Print out the number of occurrences of that specific character in the word

#include <iostream>
using namespace std;
#include <string>

main()
{
        string word = "";

        do
        {
        cout << "Enter a word that has 8 or more characters:  ";
        cin >> word;
        }
        while (word.size() < 8);

        char searchCharacter = '0';
        cout << "Enter a specific character and the program will count the number of occurrences of that letter in the word:  ";
        cin >> searchCharacter;

        int i=0;
        int counter = 0;
        for (i=0; i < word.size(); i++)
        {
        char specific = word.at(i);
        if (searchCharacter == specific)
        counter++;
        }

        cout << "The number of occurrences of the character " << searchCharacter << " in the word " << word << " is " << counter << "." << endl ;

}


Execution

Enter a word that has 8 or more characters:  Fluorescent
Enter a specific character and the program will count the number of occurrences of that letter in the word:  e
The number of occurrences of the character e in the word Fluorescent is 2.



Enter a word that has 8 or more characters:  Eagle
Enter a word that has 8 or more characters:  Cheetah
Enter a word that has 8 or more characters:  Enlightenment
Enter a specific character and the program will count the number of occurrences of that letter in the word:  t
The number of occurrences of the character t in the word Enlightenment is 2.



Enter a word that has 8 or more characters:  Counter-intuitive
Enter a specific character and the program will count the number of occurrences of that letter in the word:  i
The number of occurrences of the character i in the word Counter-intuitive is 3.



Enter a word that has 8 or more characters:  Extravagance
Enter a specific character and the program will count the number of occurrences of that letter in the word:  a
The number of occurrences of the character a in the word Extravagance is 3.



Finding the square root

Script

#include <iostream>
using namespace std;
#include <cmath>
int main()
{
        double cover;

        cout << "How many square feet of sheets do you have ? :  ";
        cin >> cover;
        double side;
        side = sqrt(cover);
        cout << "You can cover a square with sides of " << side << " feet with your sheets. \n";
        return 0;
}


Execution

How many square feet of sheets do you have ? :  81
You can cover a square with sides of 9 feet with your sheets.



How many square feet of sheets do you have ? :  144
You can cover a square with sides of 12 feet with your sheets.


How many square feet of sheets do you have ? :  10000
You can cover a square with sides of 100 feet with your sheets.


How many square feet of sheets do you have ? :  34678
You can cover a square with sides of 186.22 feet with your sheets.

Simple if example

Script

#include <iostream>
using namespace std;
#include <cmath>

int main()
{
        double yourage;

        cout << "Please enter your age:  ";
        cin >> yourage;

        cout << "You are " << yourage << " years old\n";

        if (yourage < 18)
                cout << "Oh!!! You are VERY young \n";
        return 0;
}


Execution

Please enter your age:  17
You are 17 years old
Oh!!! You are VERY young



Please enter your age:  47
You are 47 years old



Please enter your age:  35
You are 35 years old






Hello World

Script

#include <iostream>
using namespace std;

int main()
{
        cout << "Hello World!!!!! \n";
        return 0;
}


Execution

Hello World!!!!!












Fitness level --- example for if else

Script

// This is a program that asks a user for their input on how much time
// he or she took to walk 5 miles.  Based on the input information and the
// table below, the program will print the appropriate message.
// More than 90 minutes   You are very slow, need to get fitter
// Between 75 and 90 minutes   Your fitness level needs improvement
// Between 60 and 74 minutes   You are quite fit, doing well
// Between 45 and 59 minutes   You are extremely fit, keep it up
// Lesser than 45 minutes  Oh my goodness, you are super fit !!!
// Lesser than 0 minutes   Invalid entry, program will terminate.

#include <iostream>
using namespace std;

main()
{
        int minutes=0;
        cout << "Enter how many minutes you took to walk five miles: ";
        cin >> minutes;

        if (minutes < 0)
        {
        cout << "Invalid entry, program will terminate." << endl;
        return 0;
        }

        else if (minutes < 45)
        {
        cout << "Oh my goodness, you are super fit !!!" << endl;
        }

        else if (minutes >= 45 && minutes <= 59)
        {
        cout << "You are extremely fit, keep it up" << endl;
        }

        else if (minutes >= 60 && minutes <= 74)
        {
        cout << "You are quite fit, doing well" << endl;
        }

        else if (minutes >= 75 && minutes <= 90)
        {
        cout << "Your fitness level needs improvement" << endl;
        }

        else
        {
        cout << "You are very slow, need to get fitter" << endl;
        }

}


Execution

Enter how many minutes you took to walk five miles: -75
Invalid entry, program will terminate.


Enter how many minutes you took to walk five miles: 37
Oh my goodness, you are super fit !!!


Enter how many minutes you took to walk five miles: 150
You are very slow, need to get fitter

Enter how many minutes you took to walk five miles: 47
You are extremely fit, keep it up

Enter how many minutes you took to walk five miles: 63
You are quite fit, doing well



Commonly used Math functions

Script

#include <iostream>
using namespace std;
#include <cmath>

int main()
{
        double rampit;
        rampit = pow(25,3);
        cout << "25 to the power of 3 is: " << rampit << endl;

        double squary;
        squary = sqrt(169.0);
        cout << "The square root of 169.0 is: " << squary << endl;

        double cuby;
        cuby = cbrt(216.57);
        cout << "The cube root of 216.57 is: " << cuby << endl;

        double hype;
        hype = hypot(10,15);
        cout << "The hypotenuse of a triangle with sides 10 and 15 is: " << hype << endl;

        return 0;
}


Execution

25 to the power of 3 is: 15625
The square root of 169.0 is: 13
The cube root of 216.57 is: 6.00527
The hypotenuse of a triangle with sides 10 and 15 is: 18.0278

How to run a program in C++

pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / > cat sqrt.cpp
#include <iostream>
using namespace std;
#include <cmath>
int main()
{
        double cover;

        cout << "How many square feet of sheets do you have ? :  ";
        cin >> cover;
        double side;
        side = sqrt(cover);
        cout << "You can cover a square with sides of " << side << " feet with your sheets. \n";
        return 0;
}
pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / > g++ sqrt.cpp -o square-root
pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / > ./square-root
How many square feet of sheets do you have ? :  169
You can cover a square with sides of 13 feet with your sheets.
pqr47.us.company.com: / >
pqr47.us.company.com: / >
pqr47.us.company.com: / >

Series average for a player in three One Day Internationals

Script

#include <iostream>
using namespace std;
#include <string>
#include <cmath>

int main()
{
        string player = "";
        cout << "What is the name of the player ?  :  ";
        getline(cin,player);

        int first_ODI=0;
        cout << "How many runs did " << player << " score in the first _ODI ? : ";
        cin >> first_ODI;

        int second_ODI=0;
        cout << "How many runs did " << player << " score in the second _ODI ? : ";
        cin >> second_ODI;

        int third_ODI=0;
        cout << "How many runs did " << player << " score in the third _ODI ? : ";
        cin >> third_ODI;

        double average = (double)(first_ODI + second_ODI + third_ODI)/3;
        cout << "" << player << "'s average score for the series is " << average << ".\n\n";
        return 0;
}


Execution

What is the name of the player ?  :  Rahul Dravid
How many runs did Rahul Dravid score in the first _ODI ? : 55
How many runs did Rahul Dravid score in the second _ODI ? : 56
How many runs did Rahul Dravid score in the third _ODI ? : 57
Rahul Dravid's average score for the series is 56.


What is the name of the player ?  :  Virender Sehwag
How many runs did Virender Sehwag score in the first _ODI ? : 45
How many runs did Virender Sehwag score in the second _ODI ? : 2
How many runs did Virender Sehwag score in the third _ODI ? : 39
Virender Sehwag's average score for the series is 28.6667.


What is the name of the player ?  :  VVS Laxman
How many runs did VVS Laxman score in the first _ODI ? : 79
How many runs did VVS Laxman score in the second _ODI ? : 63
How many runs did VVS Laxman score in the third _ODI ? : 5
VVS Laxman's average score for the series is 49.


Thursday, March 26, 2015

Check if it is a leap year


Script

#include <iostream>
using namespace std;

main()
{
        int year, remainder, divby100, divby400;

        cout << "Enter an year: ";
        cin >> year;

        remainder = year % 4;
        divby100 = year % 100;
        divby400 = year % 400;

        cout << "\n";

        if ((remainder == 0) && (divby100 != 0) || (divby400 == 0))
        cout << year << " is a leap year \n\n";
        else
        cout << "\n" << year << " is not a leap year \n\n";

        return 0;
}


Execution


Enter an year: 2015

2015 is not a leap year



Enter an year: 2020

2020 is a leap year



Enter an year: 2100

2100 is not a leap year



Enter an year: 2200

2200 is not a leap year



Enter an year: 2300

2300 is not a leap year



Enter an year: 2400

2400 is a leap year







Sunday, March 8, 2015

Prime number --- yes or no

Script

#include <iostream>
using namespace std;

main()
{
        int number, k, flag, remainder;
        cout << "Enter a number:  ";
        cin >> number;
        k = 2;
        flag = 1;

        while ((k <= number / 2) && (flag == 1))
        {
                remainder = number % k;
                if (remainder == 0)
                        flag = 0;
                k++;
        }

        if (flag == 1)
                cout << number << " is a prime number." << endl;
        else
                cout << number << " is not a prime number." << endl;

        return 0;
}

Execution

Enter a number:  37
37 is a prime number.


Enter a number:  41
41 is a prime number.


Enter a number:  36
36 is not a prime number.

Sum of its digits

Script

#include <iostream>
using namespace std;

main()
{
        int number, remainder, sum;

        cout << "Enter an integer: ";
        cin >> number;

        sum = 0;

        while (number > 0)
        {
                remainder = number % 10;
                sum = sum + remainder;
                number = number/10;
        }

        cout << "The sum of the digits is:  " << sum;
        cout << "\n\n";

        return 0;
}

Execution

Enter an integer: 123456789
The sum of the digits is:  45


Enter an integer: 76381634
The sum of the digits is:  38


Enter an integer: 65432
The sum of the digits is:  20

Multiplication table for a number

Script

#include <iostream>
#include <iomanip>
using namespace std;

main()
{
        int number, i, product;

        cout << "Enter a number: ";
        cin >> number;

        for(i = 1; i <= 20; i++)
        {
                product = number * i;
                cout << number << " * " << i << " = " << product << endl;
        }

        return 0;
}

Execution

Enter a number: 7
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
7 * 11 = 77
7 * 12 = 84
7 * 13 = 91
7 * 14 = 98
7 * 15 = 105
7 * 16 = 112
7 * 17 = 119
7 * 18 = 126
7 * 19 = 133
7 * 20 = 140