Skip to content

Instantly share code, notes, and snippets.

@bor2com
Last active December 20, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bor2com/6079692 to your computer and use it in GitHub Desktop.
Save bor2com/6079692 to your computer and use it in GitHub Desktop.
Sample sources.
// Just printing a message.
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << " " << "world";
cout << "Hello world";
return 0;
}
// Demonstration of modulo capabilities.
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 10;
b = 7;
cout << a / b << endl;
cout << a % b << endl;
return 0;
}
// Popping next hour on a 12-hour clock.
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
a = a - 1;
a = a + 1;
a = a % 12;
a = a + 1;
cout << a << endl;
return 0;
}
// Paying with a minimum amount of bills.
#include <iostream>
using namespace std;
int main() {
int bills[] = { 200, 100, 50, 20, 10, 5, 2, 1 }, n, i;
i = 0;
cin >> n;
while (n != 0) {
while (bills[i] > n) {
i = i + 1;
}
cout << bills[i] << ' ';
n = n - bills[i];
}
return 0;
}
// This one shows why the naive approach is wrong.
#include <iostream>
using namespace std;
int main() {
int bills[] = { 5, 4, 1 }, n, i;
cin >> n;
while (n != 0) {
i = 0;
while (bills[i] > n) {
i = i + 1;
}
cout << bills[i] << ' ';
n = n - bills[i];
}
return 0;
}
// Program for calculating the sum of an integer sequence.
#include <iostream>
using namespace std;
int main() {
int marks[10], n, i, sum;
// Reading stuff.
cin >> n;
i = 0;
while (i < n) {
cin >> marks[i];
i = i + 1;
}
// Summing up.
sum = 0;
i = 0;
while (i < n) {
sum = sum + marks[i];
i = i + 1;
}
cout << sum;
return 0;
}
// It figurs out the sum of digits.
#include <iostream>
using namespace std;
int main() {
int kriakoziabra, sum;
sum = 0;
cin >> kriakoziabra;
while (kriakoziabra != 0) {
sum = sum + kriakoziabra % 10; // Retrieving the last digit.
kriakoziabra = kriakoziabra / 10; // Chopping it off.
}
cout << sum; // Voila!
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment