Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Low_level_programming

alx-low_level_programming/0x04-more_functions_nested_loops

Mkdir 0x04-more_functions_nested_loopsΒ 

README.md

To make the README.md file;

echo "project description " > README.md

main.h

To create the main.h file, do touch main.h or vi main.h and copy these codes;

#ifndef MAIN_H
#define 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);
void print_number(int n);

#endif /* MAIN_H */

_putchar.c

To make this file, touch _putchar.c or vi _putchar.c and copy these files;

#include <unistd.h>
/**
 * _putchar - writes the character c to stdout
 * @c: The character to print
 *
 * Return: On success 1.
 * On error, -1 is returned, and errno is set appropriately.
 */
int _putchar(char c)
{
return (write(1, &c, 1));
}

TASK 0 – 0-isupper.c

#include "main.h"

/**
 * _isupper - a function that checks for uppercase character
 * @c: input
 *
 * Return: 1 if c is uppercase, 0 otherwise
 */
int _isupper(int c)
{
	if ((c >= 'A') && (c <= 'Z'))
		return (1);
	else
		return (0);
}

TASK 1 – 1-isdigit.c

#include "main.h"

/**
 * _isdigit - a function that checks for a digit 0-9
 * @c: input
 * Return: 1 if c is a digit, 0 otherwise
 */
int _isdigit(int c)
{
	if ((c >= 48) && (c <= 57))
		return (1);
	else
		return (0);
}

TASK 2 – 2-mul.c

#include "main.h"

/**
 * mul - a function that multiplies two integers
 * @a: first input
 * @b: second input
 * Return: multiplication result of the 2 inputs
 */
int mul(int a, int b)
{
	return (a * b);
}

TASK 3 – 3-print_numbers.c

#include "main.h"

/**
 * print_numbers - prints 0123456789\n
 *
 * Return: void
 */
void print_numbers(void)
{
	char c;

	for (c = '0'; c <= '9'; c++)
	{
		_putchar(c);
	}
	_putchar('\n');
}

TASK 5 – 4-print_most_numbers.c

#include "main.h"

/**
 * print_most_numbers - a function that prints the numbers, from 0 to 9
 *
 * Return 0-9, excluding 2 and 4, followed by a new line
 */
void print_most_numbers(void)
{
	int i;

	for (i = 0; i <= 9; i++)
	{
		if ((i == 2) || (i == 4))
			continue;
		else
			_putchar(i + '0');
	}
	_putchar('\n');
}

TASK 5 – 5-more_numbers.c

#include "main.h"

/**
 * more_numbers - a function that prints 10 times the numbers, from 0 to 14
 * _putchar only 3 times
 * Return: 0-14 x10 followed by a new line
 */
void more_numbers(void)
{
	int i, ro;

	for (ro = 0; ro < 10; ro++)
	{
		for (i = 0; i <= 14; i++)
		{
			if (i >= 10)
			{
				_putchar((i / 10) + '0');
			}
			_putchar((i % 10) + '0');
		}
		_putchar('\n');
	}
}

TASK 6 – 6-print_line.c

#include "main.h"

/**
 * print_line - a function that draws a straight line in the terminal
 * @n: input number of times to print '_'
 * Return: a straight line
 */
void print_line(int n)
{
	int co;

	if (n <= 0)
	{
		_putchar('\n');
	}
	else
	{
		for (co = 1; co <= n; co++)
		{
			_putchar('_');
		}
		_putchar('\n');
	}
}

TASK 7 – 7-print_diagonal.c

#include "main.h"

/**
 * print_diagonal - a function that draws a diagonal line on the terminal
 * @n: length of diagonal
 *
 * Return: void
 */
void print_diagonal(int n)
{
	int a, b;

	if (n <= 0)
		_putchar('\n');
	for (a = 0; a < n; a++)
	{
		for (b = 0; b < a; b++)
		{
			_putchar(' ');
		}
		_putchar('\\');
		_putchar('\n');
	}
}

TASK 8 – 8-print_square.c

#include "main.h"

/**
 * print_square - a function that prints a square, followed by a new line
 * @size: size of both width and length
 * Return: a square made of '#'
 */
void print_square(int size)
{
	int co, ro;

	if (size <= 0)
	{
		_putchar('\n');
	}
	else
	{
		for (co = 1; co <= size; co++)
		{
			_putchar('#');
			for (ro = 2; ro <= size; ro++)
			{
				_putchar('#');
			}
			_putchar('\n');
		}
	}
}

TASK 9 – 9-fizz_buzz.c

#include <stdio.h>
#include <stdlib.h>

/**
 * main - prints the numbers from 1 to 100
 * 3 multiples print Fizz instead of the number
 * 5 multiples print Buzz instead of the number
 * 3 & 5  multiples print FizzBuzz instead of the number
 * Return: Always 0 (Success)
 */
int main(void)
{
	int i;
	char f[] = "Fizz";
	char b[] = "Buzz";
	char fb[] = "FizzBuzz";

	for (i = 1; i <= 100; i++)
	{
		if (i == 100)
			printf("%s", b);
		else if ((i % 3 == 0) && (i % 5 == 0))
			printf("%s ", fb);
		else if (i % 3 == 0)
			printf("%s ", f);
		else if (i % 5 == 0)
			printf("%s ", b);
		else
			printf("%d ", i);
	}
	printf("\n");
	return (0);
}

TASK 10 – 10-print_triangle.c

#include "main.h"

/**
 * print_triangle - a function that prints a triangle
 * @size: size of the triangle
 * Return: triangle of '#'s
 */
void print_triangle(int size)
{
	int sp, ro, tr;

	if (size <= 0)
	{
		_putchar('\n');
	}
	else
	{
		for (ro = 0; ro <= (size - 1); ro++)
		{
			for (sp = 0; sp < (size - 1) - ro; sp++)
			{
				_putchar(' ');
			}
			for (tr = 0; tr <= ro; tr++)
			{
				_putchar('#');
			}
			_putchar('\n');
		}
	}
}

ADVANCED

TASK 1 – 100-prime_factor.c

#include <stdio.h>

/**
 * main - prints prime factors of 612852475143
 *
 * Return: Always 0 (Success)
 */
int main(void)
{
	unsigned long int i, n = 612852475143;

	for (i = 3; i < 782849; i = i + 2)
	{
		while ((n % i == 0) && (n != i))
			n = n / i;
	}
	printf("%lu\n", n);
	return (0);
}

TASK 2 – 101-print_number.c

#include "main.h"

/**
 * print_number - prints an integer
 * @n: input integer parameter
 * Return: void
 */
void print_number(int n)
{
	unsigned int i = n;

	if (n < 0)
	{
		_putchar(45);
		i = -i;
	}
	if (i / 10)
	{
		print_number(i / 10);
	}
	_putchar(i % 10 + '0');
}

Hint:

Do you know you can git commit all your files and push at once?

To make that happen, complete each code and git add, then git commit. and move to the next one.

just to be sure if your work is ok, you can compile the file using make filename without the .c extension and run with ./filename.c

#Leave comments below if you have any questions.

#Share this if you find it useful.

Total
0
Shares
1 comment
  1. It’s actually a great and useful piece of information. I am glad that you shared this useful information with us. Please keep us informed like this. Thanks for sharing.

Leave a Reply

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

Previous Post
Low_level_programming

alx-low_level_programming/0x03-debugging

Next Post
earthquake-shock-tsunami-warning-issued

Powerful 6.8 magnitude earthquake hits Taiwan

Please Leave a comment on comment section, let me know what you think about this article.

Related Posts
Hey, if you have any questions and want to talk to one of our specialists chat up here:
Chat me up