Q1. Write the output of the following C codes, with proper justification for each.
- i)) int main() { int n = 123, x = 0; while (n>0) {x += n % 10; n/= 10;} printf("%d", x); } (200 words)
- ii)) int f(int n) { if (n < 1) return n; else return n*n + f(n-1); } main() { printf("%d", f(5)); } (200 words)
- iii)) void show(); int main() {show(); printf("BREAD"); return 0; } void show() { printf("Butter"); } (200 words)
- iv)) struct book { char *author; char *title; int pages; }mybook = {"ANSI C", "Kernighan & Ritchie", 288}; int main() { printf("Book Info \n"); printf("Title : %s \n", mybook.title); printf("Author : %s \n", mybook.author); printf("Pages : %d \n", mybook.pages); return 0; } (200 words)
- v)) void texas(int*, int*); int main() { int a = 11, b = 22; printf("Before = %d %d",a,b); texas (&a, &b) printf("After = %d %d",a,b); return 0; } void texas(int *i, int *j) { *i = 55,*j = 65; } (200 words)
- Digit Sum Algorithm: `n % 10` extracts last digit, `n /= 10` removes it; loop until `n` is `0`.
- Recursion Base Case: Essential for terminating recursive functions; prevents infinite recursion.
- Function Prototype: Declares a function's signature before its definition, enabling calls.
- Structure Initialization: `struct` members initialized in declaration order using an initializer list.
Answer: This document provides a detailed analysis of five distinct C programming code snippets, demonstrating fundamental concepts such as iterative digit manipulation, recursive function execution, function declaration and definition order, structure initialization and member access, and pointer-based pass-by-reference. Each sub-question includes the expected output, a step-by-step justification of the code's execution, and references to common C programming principles taught in the MMT-001 course. U...