#include <cstdio> const int SIZE = 24; int main() { int sum = 0; int position = 0; for (int row = 0; row < SIZE; row++) { for (int column = 0; column < SIZE; column++) { printf("%d ", position); sum = sum + position; position++; } printf("\n"); } printf( "The sum is %d\n", sum); } | #include <iostream> using namespace std; const int SIZE = 24; int main() { int sum = 0; int position = 0; for (int row = 0; row < SIZE; row++) { for (int column = 0; column < SIZE; column++) { cout << position; sum = sum + position; position++; } cout << endl; } cout << "The sum is " << sum << endl; } |
#include <cstdio> int main() { // get the positive integer from the user // (no error checking done here) printf( "Please enter a positive integer\n"); int N; scanf("%d", &N); // for each value 1..N see if it evenly divides N // i.e. remainder of 0 for (int n = 1; n <= N; n++) { if ((N % n) == 0) { printf("%d is a factor of %d\n", n, N); } } return 0; }
int factorial(int N) { // result will be the product 1*2*3*...*N int result = 1; for (int n = 1; n <= N; n++) { result *= n; } return result; } void choose() { // get N and M from the user printf( "Please enter two positive integers\n"); int N, M; scanf("%d", &N); scanf("%d", &M); // result = N choose M int result = 0; if (N > M) { result = factorial(N) / (factorial(M) * factorial(N-M)); } printf("%d choose %d is %d\n", N, M, result); }
#include <cstdio> const int SIZE = 24; int main() { int sum = 0; int position = 0; for (int row = 0; row < SIZE; row++) { for (int column = 0; column < SIZE; column++) { printf("%d ", position); if ((row == column) || ((row+column) == (SIZE-1))) sum = sum + position; position++; } printf("\n"); } printf("The sum is %d\n", sum); }