C programming using printf, scanf, while loop, array, expressions, and variables
ID: 639907 • Letter: C
Question
C programming using printf, scanf, while loop, array, expressions, and variables are all of the options we can use to make this program.
Look, count and say (LCS) numbers have the pattern :
1, 11, 21, 1211, 111221, etc
Write a C program to print the first 15 LCS numbers.
This program is not requiring a input from a user, just printing out the first 15 LCS numbers automatically.
The pattern is that you start with 1 and the next line you say "one 1" which would be 11, then take 11 and say "two 1" which is 21 and so forth.
1="one 1"=11
11="two 1" =21
this is done for 15 lines
Explanation / Answer
#include #include int main() { char *a = malloc(2), *b = 0, *x, c; int cnt, len = 1; for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) { puts(x = a); for (len = 0, cnt = 1; (c = *a); ) { if (c == *++a) cnt++; else if (c) { len += sprintf(b + len, "%d%c", cnt, c); cnt = 1; } } } return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.