/** * Get an integer from the command line (use atoi(3) to convert the parameter
ID: 3749773 • Letter: #
Question
/**
* Get an integer from the command line (use atoi(3) to convert the parameter
* to a int) or print a usage message ("Usage: h3 number") if none is provided.
* If the number is evenly divided by 3 print "Foo", if it's evenly divided by
* 5 print "Bar" and if it's evenly divided by both 3 and 5 print "FooBar".
* If it is not evenly divided by either, just print the number itself.
*
* Example input/output:
* ./h3 15
* FooBar
* ./h3 5
* Bar
* ./h3 43
* 43
*/
int main(int argc, char *argv[])
{
return 0;
}
Explanation / Answer
#include #include /** * Get an integer from the command line (use atoi(3) to convert the parameter * to a int) or print a usage message ("Usage: h3 number") if none is provided. * If the number is evenly divided by 3 print "Foo", if it's evenly divided by * 5 print "Bar" and if it's evenly divided by both 3 and 5 print "FooBar". * If it is not evenly divided by either, just print the number itself. * * Example input/output: * ./h3 15 * FooBar * ./h3 5 * Bar * ./h3 43 * 43 */ int main(int argc, char *argv[]) { if(argc == 2) { int n = atoi(argv[1]); if(n % 3 == 0 && n % 5 == 0) { printf("FooBar "); } else if(n % 3 == 0) { printf("Foo "); } else if(n % 5 == 0) { printf("Bar "); } else { printf("%d ", n); } } else { printf("Usage: h3 number "); } return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.