Skip to content
View jrg94's full-sized avatar
🏒
Watching hockey
🏒
Watching hockey

Organizations

@TheRenegadeCoder

Block or report jrg94

Block user

Prevent this user from interacting with your repositories and sending you notifications. Learn more about blocking users.

You must be logged in to block users.

Please don't include any personal information such as legal names or email addresses. Maximum 100 characters, markdown supported. This note will be visible to only you.
Report abuse

Contact GitHub support about this user’s behavior. Learn more about reporting abuse.

Report abuse
jrg94/README.md

Welcome to My Profile!

This week's code snippet, Factorial in C, is brought to you by Subete and the Sample Programs repo.

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>

int uint64_overflow(long, long);

int main (int argc, char *argv[])
{
	long n;
	char *endptr;
	uint64_t result = 1;
	long i;

	/* Correct nmber of args? */
	if (argc != 2) {
		printf("Usage: please input a non-negative integer\n");
		exit(0);
	}

	/* Convert argument to number */
	errno = 0;
	n = strtol(argv[1], &endptr, 10);
	if ((errno == ERANGE && (n == LONG_MAX || n == LONG_MIN))
			|| (errno != 0 && n == 0)) {
		perror("strtol");
		exit(1);
	}

	if (endptr == argv[1] ||
			!(*endptr == '\0' || *endptr == '\n') ||
			n < 0) {
		fprintf(stderr, "Usage: please input a non-negative integer\n");
		exit(1);
	}

	/* For non-zero numbers, compute factorial */
	if (n) {
		for (i = 1; i <= n; i++) {
			if (uint64_overflow(result, i)) {
				printf("Results overflowed\n");
				exit(1);
			}

			result *= i;
		}
	}

	/* Priunt result */
	printf("%ld\n", result);

	return 0;
}

/*
 * Detects overflow from 64-bit multiplication.
 *
 * Returns 1 if an overflow occurs, 0 if not
 */
int uint64_overflow(long a, long b) {
	uint64_t x;
	uint64_t b_64;

	x = a * b;
	b_64 = b;

	return (a != 0 && x / a != b_64) ? 1 : 0;
}

Below you'll find an up-to-date list of articles by me on The Renegade Coder. For ease of browsing, emojis let you know the article category (i.e., blog: ✒️, code: 💻, meta: 💭, teach: 🍎)

Also, here are some fun links you can use to support my work.


This document was automatically rendered on 2025-09-05 using SnakeMD.

Pinned Loading

  1. TheRenegadeCoder/sample-programs TheRenegadeCoder/sample-programs Public

    Sample Programs in Every Programming Language

    BASIC 602 593

  2. TheRenegadeCoder/how-to-python-code TheRenegadeCoder/how-to-python-code Public

    A collection of Jupyter Notebooks from the How to Python series

    Jupyter Notebook 87 30

  3. TheRenegadeCoder/SnakeMD TheRenegadeCoder/SnakeMD Public

    A markdown generation library for Python.

    Python 46 11

  4. TheRenegadeCoder/image-titler TheRenegadeCoder/image-titler Public

    An image title generator using The Renegade Coder style

    Python 17 6

  5. JuxtaMIDI JuxtaMIDI Public

    Pinpointing Mistakes in MIDI Practice Recordings

    JavaScript 16 2

  6. Orpheus Orpheus Public archive

    An adventure game with a focus on 3D audio

    Python 3