100% found this document useful (1 vote)
2K views

0x02. C - Functions, Nested Loops

The document contains 11 assignments for a C programming course. The assignments cover basic C concepts like printing characters, strings, numbers, using functions, conditional statements, loops, and arithmetic operations. The assignments increase in complexity from printing simple strings to adding integers to printing sequences of numbers.

Uploaded by

Iddrisu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

0x02. C - Functions, Nested Loops

The document contains 11 assignments for a C programming course. The assignments cover basic C concepts like printing characters, strings, numbers, using functions, conditional statements, loops, and arithmetic operations. The assignments increase in complexity from printing simple strings to adding integers to printing sequences of numbers.

Uploaded by

Iddrisu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment 0 (putchar)

#include "main.h"

/**
* main - Prints _putchar followed by a newline
*
* Return: Always 0.
*/
int main(void)
{
int str[] = {95, 112, 117, 116, 99, 104, 97, 114};
int count, sz;

sz = sizeof(str) / sizeof(int);
for (count = 0; count < sz; count++)
{
_putchar(str[count]);
}
_putchar('\n');
return (0);
}

ASSIGNMENT (1-alphabet)

#include "main.h"

/**
* print_alphabet - Prints the alphabet in lowercase
*
* Return : Always 0
*/
void print_alphabet(void)
{
char c;

for (c = 'a'; c <= 'z'; c++)


{
_putchar(c);
}
_putchar('\n');
}

Assignment (2-print_alphabet_x10.c)

#include "main.h"

/**
* print_alphabet_x10 - prints 10x the alphabet in lowercase
*
* Return: Always 0 (Success)
*/
void print_alphabet_x10(void)
{
int count = 0;
char letter;

while (count++ <= 9)


{
for (letter = 'a'; letter <= 'z'; letter++)
_putchar(letter);
_putchar('\n');
}
}

Assignment(3-islower)

#include "main.h"

/**
* _islower - checks if a character is lowercase.
* @c: the character to be checked
*
* Return: 1 if character is lowercase, 0 otherwise
*/
int _islower(int c)
{
if (c >= 'a' && c <= 'z')
return (1);
else
return (0);
}

assignment(4-isalpha.c)

#include "main.h"

/**
* _isalpha - shows 1 if input isna letter. Otherwise 0
*
* @c: character in ASCII code
*
* Return: 1 for letters. 0 for rest
*/
int _isalpha(int c)
{
if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90))
{
return (1);
}
else
{
return (0);
}
_putchar('\n');
}

assignment(5-sign.c)

#include "main.h"

/**
* print_sign - checks for integer sign
* @n: the integer to be checked
*
* Return: 0 or 1
*/

int print_sign(int n)
{
if (n > 0)
{
_putchar(43);
return (1);
}
else if (n < 0)
{
_putchar(45);
return (-1);
}
else
{
_putchar(48);
return (0);
}
_putchar('\n');
}

assignment(6-abs.c)

#include "main.h"

/**
* _abs - computes the absolute value of an integer
* @i: input number as an integer.
*
* Return: absolute value
*/
int _abs(int i)
{
if (i >= 0)
{
return (i);
}
else
{
return (i * -1);
}
}

assignment(7-print_last_digit.c)

#include "main.h"

/**
* print_last_digit - prints the last digit of a number
*
* @n: input number as an integer
*
* Return: last digit
*/
int print_last_digit(int n)
{
int l;

l = n % 10;
if (l < 0)
{
_putchar(-l + 48);
return (-l);
}
else
{
_putchar(l + 48);
return (l);
}
}

Assignment(8-24_hour.c)

#include "main.h"

/**
* jack_bauer - prints the minutes of a day
*
* Return: no return
*/
void jack_bauer(void)
{
int a, b, c, d;

for (a = 48; a <= 50; a++)


{
for (b = 48; b <= 57; b++)
{
for (c = 48; c <= 53; c++)
{
for (d = 48; d <= 57; d++)
{
if (a >= 50 && b >= 52)
break;
_putchar(a);
_putchar(b);
_putchar(58);
_putchar(c);
_putchar(d);
_putchar('\n');
}
}
}
}
}

assignment(9-times_table.c)

#include "main.h"

/**
* times_table - prints the 9 times table
*
* Return: no return
*/
void times_table(void)
{
int a, b, mu;

for (a = 0; a <= 9; a++)


{
_putchar(48);
for (b = 1; b <= 9; b++)
{
mu = a * b;
_putchar(44);
_putchar(32);
if (mu <= 9)
{
_putchar(32);
_putchar(mu ÷ 48);
}
else
{
_putchar((mu / 10) ÷ 48);
_putchar((mu % 10) ÷ 48);
}
}
_putchar('\n')
}

assignment(10-add.c)

#include "main.h"

/**
* add - Adds two integers
* @num1: First integer to be added
* @num2: second integer to be added.
*
* Return: The result of the addition
*/
int add(int num1, int num2)
{
return (num1 + num2);
}

assignment(11-print_to_98.c)

#include <stdio.h>

/**
* print_to_98 - Prints all natural numbers
* from n to 98.
*
* @n: input number.
*
* Return: no return.
*/
void print_to_98(int n)
{
if (n > 98)
{
for (; n > 98; n--)
{
printf("%d, ", n);
}
}
else if (n < 98)
{
for (; n < 98; n++)
{
printf("%d, ", n);
}
}
printf("%d\n", n);
}

You might also like