Skip to content

Instantly share code, notes, and snippets.

@jibachhydv
Created February 1, 2021 06:14
Show Gist options
  • Save jibachhydv/a666aa621b86d2604e584571ba1d475c to your computer and use it in GitHub Desktop.
Save jibachhydv/a666aa621b86d2604e584571ba1d475c to your computer and use it in GitHub Desktop.
Population Lab CS50x
#include <cs50.h>
#include <stdio.h>
int main(void)
{
// Prompt for the start size
int startSize;
do {
startSize = get_int("Start size: ");
} while (startSize <= 8);
// Prompt for end size
int endSize;
do {
endSize = get_int("End size: ");
} while (endSize < startSize);
// Calculate number of years until we reach threshold
int currentNum = startSize;
int numberOfYears = 0;
// handling the same size input case
if (startSize == endSize){
numberOfYears = 0;
} else {
do {
int born = currentNum / 3;
int death = currentNum / 4;
currentNum = currentNum + born -death;
numberOfYears = numberOfYears + 1;
} while (currentNum != endSize && currentNum <= endSize);
}
// TODO: Print number of years
printf("Years: %i\n", numberOfYears);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment