Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,149,880 members, 7,806,531 topics. Date: Tuesday, 23 April 2024 at 05:55 PM

C++ Drop Your Code. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / C++ Drop Your Code. (929 Views)

Share Your Code / Drop Your Skype Contact if you're programmer / Drop Your Advise.....please (2) (3) (4)

(1) (Reply) (Go Down)

C++ Drop Your Code. by prudentexpress: 10:25pm On Feb 22, 2015
I decided to create this post because I was looking around on Nairaland if there is a post where anyone can just drop their code, so to exchange intelligence. I will drop couple of codes here and feel free to drop yours.
Re: C++ Drop Your Code. by prudentexpress: 10:32pm On Feb 22, 2015
This program convert base ten number into any base between base 2 and base 9.


===================================================================================

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


int main()
{
int baseTen = 0; // holds the value of base 10
int anyBase = 0; // holds the value of a base the value will be converted to.
string display; // display the output
cout << "Please enter number in \"base 10 \": "; //Prompt user for input for base 10
cin >> baseTen; // get base 10 input from user

while (baseTen <0) // validate the input of base 10
{
cout << "Please enter number in \"base 10 \" and has to be greater than 0"": ";
cin >> baseTen;
}
cout << endl; // create newline for readability

cout << "Please enter the converted based between \"base 2 - 9 \": "; // prompt user for input for the converted base
cin >> anyBase; //get converted base from user

while (anyBase <2 || anyBase >9) // validate the input for converted base
{
cout << "Please enter the converted based between \"base 2 - 9 \": ";
cin >> anyBase;
}
cout << endl; // create newline for readability

if (baseTen == 0) // Check if baseTen is 0
{
cout << "The output of " << baseTen << " in base " << anyBase << " is 0" << endl << endl; exit(1); // Display the output if baseTen is 0
}
while (baseTen)
{
display = (char)(baseTen % anyBase + '0') + display; // Convert from based 10 to desired based
baseTen /= anyBase;
}
cout << "The output in base " << anyBase << " is : " << display << endl << endl; // display the result

}
Re: C++ Drop Your Code. by prudentexpress: 10:44pm On Feb 22, 2015
This program sums up all the digits inputted on the same line. for example, if user input 1234, the answer will be 10.



=================================================================================

#include <iostream>
using namespace std;


int main()
{
int input; // holds input
int sum = 0; // Will hold the sum of digits to be added together

cout << "Please enter the digits you want to sum: "; // Prompt user for input
cin >> input; // get input from user

cout << endl; // create a newline for readability
while (input) // checks if the condition is not false
{
sum += input % 10; // get the last digit of the number and add it to the variable sum
input /= 10; // get rid of the last number added to sum.

}
cout << "The sum of the digits are: " << sum << endl << endl; // display the answer.


}
Re: C++ Drop Your Code. by prudentexpress: 10:48pm On Feb 22, 2015
This program changes the order a number is inputted into reverse order. For example, 1234 will be 4321


======================================================================================

#include <iostream>
using namespace std;

int main()
{
int input; // holds the input
const int BASE = 10; // hold the base
int remainder; // holds the remainder
int reverseNumber = 0; //holds the reverse number


cout << "Please enter an integer to be reversed: "; //Prompt user for input
cin >> input; // get input from user
cout << endl; // Create newline for readability

while (input)
{
remainder = input % BASE; // Get the last digit from the number
reverseNumber = (reverseNumber*BASE) + remainder; // Change the reverse number digit by digit
input /= BASE; // take off the last digit from number and ready for the next.

}
cout << "The reverse order is " << reverseNumber << endl << endl; // Display the reversed order.



return 0;
}
Re: C++ Drop Your Code. by prudentexpress: 11:04pm On Feb 22, 2015
This program calculates the occupancy rate for a hotel. The program asks user for how many floors the hotel has.
The loop iterate once for each floor, and ask how many rooms on the each floor. It also ask how many rooms occupied on each floor.
After iteration, the program display how total rooms the hotel has, and how many are occupied. Also, it display percentage of rooms between occupied and unoccupied rooms.


======================================================================================

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




int main()
{
int numberOfFloor; // holds number of floors
int numberOfRoom = 0; // holds number of rooms
int numberOfRoomOcuppied = 0; //holds room occupied
int totalRoom = 0; // holds total room
int totalUnocuppied = 0; // holds total room not occuppied
int Unocuppiedroom = 0; //holds the difference between totalroom and unoccuppied room

cout << "Enter floor: "; //Prompt user to input of number of floor
cin >> numberOfFloor; // gets the input

while (numberOfFloor <= 0) //validates the input
{
cout << "Number of floor should be greater than " << numberOfFloor << endl;
cout << "Enter floor: ";
cin >> numberOfFloor;

}

cout << endl << endl; // create double newline.
for (int floor = 1; floor <= numberOfFloor; floor++) // loops through the number of floor inputed by user
{
if (floor == 13) // skips floor step as required by the client
{
continue;
}
cout << "Floor " << floor << " rooms are ?: "; // ask for number of rooms on the a particular floor
cin >> numberOfRoom; // gets the input

while (numberOfRoom < 10) // validates the input to make sure it's more than 10 rooms
{
cout << "Number of room must be more than 10 " << endl;
cout << "Floor " << floor << " rooms are ?: ";
cin >> numberOfRoom;
}
cout << "Floor " << floor << " occupied rooms are ?: "; //ask for number of rooms occuppeid on a particular floor
cin >> numberOfRoomOcuppied; // gets the input
cout << "--------------------------------" << endl; // create a line just for readability
cout << endl; // create a newline

totalRoom += numberOfRoom; // adds number of rooms together
totalUnocuppied += numberOfRoomOcuppied; //adds number of unoccuppied rooms together
}

Unocuppiedroom = totalRoom - totalUnocuppied;

cout << "Hotel total rooms are " << totalRoom << endl; //displays total rooms
cout << "Hotel occuppied rooms are " << totalUnocuppied << endl; // displays occuppied rooms
cout << "Hotel unoccupied rooms are " << Unocuppiedroom << endl; // displays unoccuppied rooms
cout << fixed << setprecision(2) << "Hotel average is " << (double)totalUnocuppied / totalRoom * 100 << "%" << endl << endl;//calculate the average percentage




return 0;
}
Re: C++ Drop Your Code. by prudentexpress: 11:21pm On Feb 22, 2015
This program generates a random number and ask user to guess what the number is between 1 -10. You can easily modify it to any number you
desire.

=================================================================================

#include <iostream>
using namespace std;
#include <stdlib.h>
#include <ctime>




int main()
{
int randomNumber; // holds random number
int guess; // holds guess
int counter = 0; // holds number of guesses

cout << " \t \t Welcome To Random Number Guessing Game" << endl; // Display title
cout << " \t \t \t Guess between 1 - 10 " << endl; /// Display title
cout << endl << endl; // create double newline

while (true)
{
counter++; // count the number of guesses
srand(time(0)); // seed with time
randomNumber = 1 + (rand() % 10); // generate random numbers between 1 to 10 for easy guess



cout << "Guess what the number is ? "; //Prompt user for input
cin >> guess; // get input

if (guess == randomNumber) // check to see if they are equal
{
cout << "You are right !!! The random number was " << randomNumber << endl;
break;

}
else if (guess > randomNumber) // check to see guess is greater than randomNumber
{
cout << "Too high, try again. The random number was " << randomNumber << endl;
}
else // Check to see if guess is less than randomNumber
{
cout << "Too low, try again. The random number was " << randomNumber << endl;
}

cout << endl; // create a newline

//counter++;
}

cout << "You guessed right in " << counter << " guesses. keep up the great job !!!" << endl;
cout << endl << endl; // create double newline
return 0;
}

(1) (Reply)

. / ASP.NET HELP!ยก! Any Programmer Can Give Suggestion. / Looking For Someone To Do SEO Work For Your Site Or Manage Your SM Account?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 29
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.