// #include "stdafx.h" #include #include #include "P2.h" int abs(int n) { if (n > 0) return n; return -n; } unsigned int numDigits(int n) { unsigned int digits; n = abs(n); for (digits = 1; n >= 10; digits++, n /= 10); return digits; } unsigned int nextPrime(int start) { unsigned int next = start; unsigned int divisor = 0; if (start == 0) return 1; if (start == 1) return 2; if (start == 2) return 3; if (start % 2 == 0) next += 1; else next += 2; while (true) { for (divisor = 3; next % divisor != 0; divisor += 2); if (next == divisor) return next; next += 2; } return next; } unsigned int Nth(unsigned int n) { unsigned int value, count; for (count = 0, value = 1; count < n; value = nextPrime(value), count++); return value; } void printStars() { unsigned int i, rows, stars, spaces; for (rows = 0, stars = 15, spaces = 0; rows < 8; rows++, stars -= 2, spaces++) { for (i = 0; i < spaces; i++) cout << " "; for (i = 0; i < stars; i++) cout << "*"; cout << endl; } } void rotate(int & a, int & b, int & c) { int temp = a; a = b; b = c; c = temp; } int main() { cout << numDigits(0) << endl; cout << numDigits(1) << endl; cout << numDigits(10) << endl; cout << numDigits(100) << endl; cout << numDigits(1000) << endl; cout << numDigits(-1) << endl; cout << numDigits(-10) << endl; cout << numDigits(-100) << endl; cout << numDigits(-1000) << endl; cout << Nth(1) << endl; cout << Nth(2) << endl; cout << Nth(3) << endl; cout << Nth(5) << endl; cout << Nth(20) << endl; printStars(); int a = 1, b = 2, c = 3; cout << a << " " << b << " " << c << endl; rotate(a, b, c); cout << a << " " << b << " " << c << endl; rotate(a, b, c); cout << a << " " << b << " " << c << endl; rotate(a, b, c); cout << a << " " << b << " " << c << endl; return 0; }