From 938c6a265afb4de2f8be1fbfb51ac10e24a8c26c Mon Sep 17 00:00:00 2001 From: juan Date: Tue, 12 Oct 2021 11:58:30 +0800 Subject: [PATCH] add first 2 problems --- pset3/1-rhombus.c | 31 +++++++++++++++++++++++++++++++ pset3/2-power.c | 17 +++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 pset3/1-rhombus.c create mode 100644 pset3/2-power.c diff --git a/pset3/1-rhombus.c b/pset3/1-rhombus.c new file mode 100644 index 0000000..084ad5a --- /dev/null +++ b/pset3/1-rhombus.c @@ -0,0 +1,31 @@ +#include + +int main(void) { + // Initialize variables + int level; + scanf("%d", &level); + + // Iterate over lines and print rhombus + // Upper half + for (int i = 1; i <= level; i++) { + for (int k = 0; k < level - i; k++) { + printf(" "); + } + for (int j = 0; j < i * 2 - 1; j++) { + printf("*"); + } + printf("\n"); + } + // Lower half + for (int i = level - 1; i > 0; i--) { + for (int k = 0; k < level - i; k++) { + printf(" "); + } + for (int j = 0; j < i * 2 - 1; j++) { + printf("*"); + } + printf("\n"); + } + + return 0; +} diff --git a/pset3/2-power.c b/pset3/2-power.c new file mode 100644 index 0000000..50c842f --- /dev/null +++ b/pset3/2-power.c @@ -0,0 +1,17 @@ +#include + +int main(void) { + // Initialize values and get input + int a, b, c = 1; + scanf("%d%d", &a, &b); + + // Iterate and calculate result + for (int i = 0; i < b; i++) { + c *= a; + } + + // Print answer + printf("%d\n", c); + + return 0; +}