Skip to main content

Overview

This module builds on the fundamentals of functions and loops, introducing more complex nested loop patterns, character validation functions, and practical exercises like the FizzBuzz problem. You’ll create functions that draw shapes and validate character types.

Function prototypes

The main.h header file defines the following function prototypes:
main.h
int _putchar(char c);
int _isupper(int c);
int _isdigit(int c);
int mul(int a, int b);
void print_numbers(void);
void print_most_numbers(void);
void more_numbers(void);
void print_line(int n);
void print_diagonal(int n);
void print_square(int size);
void print_triangle(int size);

Character validation functions

Checking for uppercase characters

The _isupper() function checks if a character is uppercase by comparing its ASCII value:
0-isupper.c
#include "main.h"

/**
 * _isupper - Entry point
 * @c: The character to check
 * Description: checks for uppercase character
 * Return: 1 if c is uppercase and 0 otherwise
 */
int _isupper(int c)
{
    return (c >= 65 && c <= 90);
}
ASCII values 65-90 represent uppercase letters A-Z. This function returns 1 (true) if the character is uppercase, 0 (false) otherwise.

Checking for digits

Similar to _isupper(), the _isdigit() function validates whether a character is a numeric digit (0-9).

Mathematical operations

Simple multiplication

2-mul.c
#include "main.h"

/**
 * mul - Entry point
 * @a: integer
 * @b: integer
 * Description: multiply two integers
 * Return: result
 */
int mul(int a, int b)
{
    return (a * b);
}
This straightforward function demonstrates how to create utility functions that perform basic mathematical operations.

Pattern printing with nested loops

Drawing squares

The print_square() function uses nested loops to draw a square of a given size:
8-print_square.c
#include "main.h"

/**
 * print_square - print square
 * @n: # to be printed
 * Return: void
 */
void print_square(int n)
{
    int i = 0, s;

    while (i < n && n > 0)
    {
        s = 0;
        while (s < n)
        {
            _putchar('#');
            s++;
        }
        _putchar('\n');
        i++;
    }
    if (i == 0)
        _putchar('\n');
}
The outer while loop controls the rows (running n times), while the inner while loop prints n hash characters for each row. The special case handles when n <= 0 by printing a single newline.

Drawing triangles

The print_triangle() function creates a right-aligned triangle pattern:
10-print_triangle.c
#include "main.h"

/**
 * print_triangle - print triangle
 * @size: size of triangle
 * Return: void
 */
void print_triangle(int size)
{
    int i = 1, t;

    while (i <= size && size > 0)
    {
        t = 0;
        while (t < size - i)
        {
            _putchar(' ');
            t++;
        }
        t = 0;
        while (t < i)
        {
            _putchar('#');
            t++;
        }
        _putchar('\n');
        i++;
    }
    if (i == 1)
        _putchar('\n');
}
This function demonstrates how to create patterns with both spaces and characters. For each row i, it prints (size - i) spaces followed by i hash characters, creating a right-aligned triangle.

The FizzBuzz challenge

FizzBuzz is a classic programming interview question that tests your understanding of conditionals and loops:
9-fizz_buzz.c
#include <stdio.h>

/**
 * main - entry point
 * Description: fizzbuzz
 * Return: 0
 */
int main(void)
{
    int numbers;

    for (numbers = 1; numbers <= 100; numbers++)
    {
        if (numbers % 3 == 0 && numbers % 5 == 0)
        {
            printf("FizzBuzz");
        }
        else if (numbers % 3 == 0)
        {
            printf("Fizz");
        }
        else if (numbers % 5 == 0)
        {
            printf("Buzz");
        }
        else
        {
            printf("%d", numbers);
        }
        if (numbers != 100)
        {
            printf(" ");
        }
    }
    printf("\n");
    return (0);
}

FizzBuzz rules

1

Check divisibility by both 3 and 5

If the number is divisible by both 3 and 5 (i.e., divisible by 15), print “FizzBuzz”
2

Check divisibility by 3

Else if divisible by 3 only, print “Fizz”
3

Check divisibility by 5

Else if divisible by 5 only, print “Buzz”
4

Default case

Otherwise, print the number itself
The order of conditionals matters! Always check for divisibility by both numbers first, before checking individual divisors. Otherwise, numbers divisible by both would only match the first condition.

Key concepts

Character validation

Use ASCII value ranges to validate character types (uppercase, digits, etc.)

Nested loops

Combine multiple loops to create 2D patterns and shapes

Conditional logic

Order your conditionals carefully, especially when checking multiple conditions

Pattern building

Break complex patterns into rows and columns, handling each with separate loop iterations

Compilation

Compile these programs with the standard flags:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 <file.c> -o <output>
For example:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 9-fizz_buzz.c -o fizz_buzz
./fizz_buzz

Next steps

Pointers & arrays

Move on to memory addresses and pointer manipulation

Debugging

Learn debugging techniques for C programs

Build docs developers (and LLMs) love