The 0x01-variables_if_else_while module covers fundamental programming concepts: declaring variables, making decisions with conditionals, and implementing loops. You’ll learn to control program flow and work with different data types.
This program demonstrates variable declaration, random number generation, and conditional statements.
0-positive_or_negative.c
#include <stdlib.h>#include <time.h>#include <stdio.h>/** * main - Entry point *Description: This program outputs positive if greater than zero, *negative if less than zero, and zero if equal to zero *Return: 0 on success*/int main(void){int n;srand(time(0));n = rand() - RAND_MAX / 2;if (n > 0){printf("%d is positive\n", n);}else if (n < 0){printf("%d is negative\n", n);}else{printf("%d is zero\n", n);}return (0);}
srand(time(0)) seeds the random number generator with the current time, ensuring different values on each run.
The last digit program shows complex conditional logic:
1-last_digit.c
#include <stdlib.h>#include <time.h>#include<stdio.h>/** * main - Entry point *Description: This program outputs is greater tahn 5 if greater than five, *less than 6 and not if less than 6 and not 0 *0 if equal to 0 *Return: 0 on success*/int main(void){int n;int last_digit;srand(time(0));n = rand() - RAND_MAX / 2;last_digit = n % 10;if (last_digit > 5){printf("Last digit of %d is %d and is greater than 5\n", n, last_digit);}else if (last_digit < 6 && last_digit != 0){printf("Last digit of %d is %d and is less than 6 and not 0\n", n, last_digit);}else if (last_digit == 0){printf("Last digit of %d is %d and is 0\n", n, last_digit);}return (0);}
The modulo operator % gets the remainder. n % 10 extracts the last digit of any number.
Logical operators in C
&& : Logical AND (both conditions must be true)
|| : Logical OR (at least one condition must be true)
#include <stdio.h>/** * main - Entry point *Description: This program prints the alphabet in lowercase *Return: 0 on success*/int main(void){char alphabet;for (alphabet = 'a'; alphabet <= 'z' ; alphabet++)putchar(alphabet);putchar('\n');return (0);}
For loop structure:
Initialization: alphabet = 'a' - sets starting value
Condition: alphabet <= 'z' - continues while true
Increment: alphabet++ - executes after each iteration
In C, characters are actually integers (ASCII values). You can increment them: 'a' becomes 'b', etc.
#include <stdio.h>/** * main - Entry point *Description: This program prints all single digit numbers *of base 10 starting from 0 *Return: 0 on success */int main(void){int number;for (number = 0; number <= 9; number++){printf("%d", number);}printf("\n");return (0);}
#include <stdio.h>/** * main - Entry point * Description: Write a program that prints all possible combinations * of single-digit numbers. * Return: 0 on success */int main(void){int number = 0;while (number < 10){putchar('0' + number);if (number != 9){putchar(',');putchar(' ');}number++;}putchar('\n');return (0);}
'0' + number converts an integer to its character representation. '0' + 5 gives '5'.
# Compile with all warningsgcc -Wall -Werror -Wextra -pedantic -std=gnu89 0-positive_or_negative.c -o positive# Run multiple times to see different random values./positive./positive./positive