C (programming language)

C Program to print your name 10 times without using loops

Printing Your Name 10 Times: Beyond the Loop in C

While loops are the go-to method for repetitive tasks in C, achieving the same outcome without them can be an interesting challenge. This guide explores three creative approaches to print your name 10 times in C, ditching the conventional loop approach.

1. Harnessing the Power of Recursion

Recursion involves a function calling itself until a base case is reached. We can leverage this for our purpose by defining a function that prints your name and then calls itself nine more times:

C

void printName(int times) {
  if (times == 0) {
    return; // Base case: stop recursion
  }
  printf("%s\n", "Your Name");
  printName(times - 1); // Recursive call with reduced counter
}

int main() {
  printName(10); // Start the recursion with 10 calls
  return 0;
}

Here, the printName function checks if the provided times counter reaches 0 (base case). If not, it prints your name and then calls itself again with the counter decreased by 1. This continues until the base case is met, effectively printing your name 10 times.

2. Concatenating Strings with Style

C allows string concatenation using the + operator. We can exploit this to build a single string containing your name repeated 10 times:

C

#include <stdio.h>

int main() {
  char nameString[110]; // Allocate space for 10 repetitions + null terminator
  for (int i = 0; i < 10; i++) {
    strcat(nameString, "Your Name"); // Concatenate your name 10 times
    if (i < 9) {
      strcat(nameString, " "); // Add spaces between names (except last)
    }
  }
  printf("%s\n", nameString); // Print the final concatenated string
  return 0;
}

This approach utilizes a loop, but not for the actual printing. Instead, it iterates 10 times, each time concatenating your name to a pre-allocated string (nameString). Finally, it prints the complete string containing your name repeated 10 times.

3. Preprocessor Magic with Macros

C preprocessor directives can be used to define macros that expand to specific code during compilation. We can leverage this to create a macro that prints your name a set number of times:

C

#define PRINT_NAME_10_TIMES \
  printf("Your Name\n"); \
  printf("Your Name\n"); \
  printf("Your Name\n"); \
  printf("Your Name\n"); \
  printf("Your Name\n"); \
  printf("Your Name\n"); \
  printf("Your Name\n"); \
  printf("Your Name\n"); \
  printf("Your Name\n"); \
  printf("Your Name\n");

int main() {
  PRINT_NAME_10_TIMES; // Expand the macro, printing your name 10 times
  return 0;
}

This code defines a macro called PRINT_NAME_10_TIMES that contains the actual printing statements repeated 10 times. By invoking this macro in the main function, we essentially execute the printing code 10 times, achieving our desired outcome.

These three approaches showcase alternative methods to print your name 10 times without relying on traditional loops. Each method offers its own advantages and trade-offs in terms of code readability, efficiency, and maintainability. The choice of approach depends on your specific needs and preferences, adding a touch of creativity and flexibility to your C programming repertoire.

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button