Skip to content

Instantly share code, notes, and snippets.

@robleh
Created March 10, 2015 03:14
Show Gist options
  • Select an option

  • Save robleh/515c7f47b9b0886f1515 to your computer and use it in GitHub Desktop.

Select an option

Save robleh/515c7f47b9b0886f1515 to your computer and use it in GitHub Desktop.
Crackle Pop in C
#include <stdio.h>
#include <stdlib.h>
/*I'm learning C and I thought this would be a fun chance to practice.
I aimed to make a Crackle Pop program which could take any pair of integers as input.
My strategy was to process number pairs containing a factor and a multiple
differently than those which didn't. Thanks for taking the time to check it out!*/
int numbers_not_related(int crackle, int pop);
int numbers_related(int larger_abs, int smaller_abs, char const *crackle_or_pop);
int main(void)
{
int crackle_input;
int pop_input;
char const *crackle_or_pop_string;
/*Assign user input to variable address*/
printf("\nEnter factor you'd like to crackle:\n");
scanf("%d", &crackle_input);
printf("Enter factor you'd like to pop:\n");
scanf("%d", &pop_input);
printf("\n");
/*Check if the input's absolute values are factors of one another*/
if (abs(crackle_input) % abs(pop_input) == 0)
{
/*Pass the corresponding string to the function to display later*/
crackle_or_pop_string = "Pop";
numbers_related(crackle_input, pop_input, crackle_or_pop_string);
}
else if (abs(pop_input) % abs(crackle_input) == 0)
{
crackle_or_pop_string = "Crackle";
numbers_related(pop_input, crackle_input, crackle_or_pop_string);
}
else
{
numbers_not_related(crackle_input, pop_input);
}
return 0;
}
/*Function to run if user inputs are not factors of each other*/
int numbers_not_related(int crackle, int pop)
{
int count;
for (count = 1; count < 101; count++)
{
/*The intersection of multiples happens here*/
if (count % (crackle * pop) == 0)
{
printf("CracklePop\n");
}
else if (count % crackle == 0)
{
printf("Crackle\n");
}
else if (count % pop == 0)
{
printf("Pop\n");
}
else
{
printf("%d\n", count);
}
}
return 0;
}
/*Function to run if user inputs are factors of each other*/
int numbers_related(int larger_abs, int smaller_abs, char const *crackle_or_pop)
{
int count;
for (count = 1; count < 101; count++)
{
if (count % larger_abs == 0)
{
/*Factor and multiple will always overlap*/
printf("CracklePop\n");
}
else if (count % smaller_abs == 0)
{
/*Print the string we determined in the main loop*/
printf("%s\n", crackle_or_pop);
}
else
{
printf("%d\n", count);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment