0x01. C - Variables, if, else, while
Concepts
Resources
Learning Objectives
At the end of this project, you are expected to be able to explain to anyone, without the help of Google
General
What are the arithmetic operators and how to use them
What are the logical operators (sometimes called boolean operators) and how to use them
What the the relational operators and how to use them
What values are considered TRUE and FALSE in C
What are the boolean operators and how to use them
How to use the
if, if ... elsestatementsHow to use comments
How to declare variables of types
char, int, unsigned intHow to assign values to variables
How to print the values of variables of type
char, int, unsigned int, using printfHow to use the
whileloopHow to use variables with the
whileloopHow to print variables using
printfWhat are the purpose of the
gccflags-m32and-m64
Requirements
C
Allowed editors:
vi, vim, emacsAll your files will be compiled on Ubuntu 20.04 LTS using gcc, using the options
-Wall -Werror -Wextra -pedantic -std=gnu89All your files should end with a new line
A
README.mdfile at the root of the repo, containing a description of the repositoryA
README.mdfile, at the root of the folder of this project, containing a description of the projectThere should be no errors and no warnings during compilation
You are not allowed to use
systemYour code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl
Shell Scripts
Allowed editors:
vi, vim, emacsAll your scripts will be tested on Ubuntu 20.04 LTS
All your scripts should be exactly two lines long (
$ wc -l file should print 2)All your files should end with a new line
The first line of all your files should be exactly
#!/bin/bash
Betty Linter
To run the Betty linter just with command betty [filename]
Go to Betty Repository
Clone the repo to your local machine
cdto the Betty directoryInstall the linter with
sudo ./install.shemacs or via new file calledbetty, and copy the script below:- #!/bin/bash # Simply a wrapper script to keep you from having to use betty-style # and betty-doc separately on every item. # Originally by Tim Britton (@wintermanc3r), multiargument added by # Larry Madeo (@hillmonkey) BIN_PATH="/usr/local/bin" BETTY_STYLE="betty-style" BETTY_DOC="betty-doc" if [ "$#" = "0" ]; then echo "No arguments passed." exit 1 fi for argument in "$@" ; do echo -e "\n========== $argument ==========" ${BIN_PATH}/${BETTY_STYLE} "$argument" ${BIN_PATH}/${BETTY_DOC} "$argument" done
Once saved, exit file and change permissions to apply to all users with
chmod a+x bettyMove the
bettyfile into/bin/directory or somewhere else in your$PATHwithsudo mv betty /bin/
You can now type betty [filename] to run the Betty linter!
Tasks
0. Positive anything is better than negative nothing
This program will assign a random number to the variable n each time it is executed. Complete the source code in order to print whether the number stored in the variable n is positive or negative.
Source Code
#include <stdlib.h> #include <time.h> /* more headers goes there */ /* betty style doc for function main goes there */ int main(void) { int n; srand(time(0)); n = rand() - RAND_MAX / 2; /* your code goes there */ return (0); }The variable
nwill store a different value every time you will run this programYou don’t have to understand what
rand,srand,RAND_MAXdo. Please do not touch this codeThe output of the program should be:
The number, followed by
If the number is greater than 0:
is positiveIf the number is 0:
is zeroIf the number is less than 0:
is negative
followed by a new line
1. The last digit
This program will assign a random number to the variable n each time it is executed. Complete the source code in order to print the last digit of the number stored in the variable n.
Source Code
#include <stdlib.h> #include <time.h> /* more headers goes there */ /* betty style doc for function main goes there */ int main(void) { int n; srand(time(0)); n = rand() - RAND_MAX / 2; /* your code goes there */ return (0); }The variable
nwill store a different value every time you run this programYou don’t have to understand what
rand,srand,RAND_MAXdo. Please do not touch this codeThe output of the program should be:
The string
Last digit of,followed byn,followed bythe string
is,followed byIf the last digit of
nis greater than: the stringand is greater than 5If the last digit of
nis 0:and is 0If the last digit of
nis less than 6 and not 0: the stringand is less than 6 and not 0
followed by a new line
2. I sometimes suffer from insomnia. And when I can't fall asleep, I play what I call the alphabet game
Write a program that prints the alphabet in lowercase, followed by a new line.
You can only use the
putcharfunction (every other function (printf,puts, etc..) is forbiddenAll your code should be in the
mainfunctionYou can only use
putchartwice in your code
3. alphABET
Write a program that prints the alphabet in lowercase, and then in uppercase, followed by a new line.
You can only use the
putcharfunction (every other function (printf,puts, etc..) is forbiddenAll your code should be in the
mainfunctionYou can only use
putcharthree times in your code
4. When I was having that alphabet soup, I never thought that it would pay off
Write a program that prints the alphabet in lowercase, followed by a new line.
Print all the letters except
qandeYou can only use the
putcharfunction (every other function (printf,puts, etc..) is forbiddenAll your code should be in the
mainfunctionYou can only use
putchartwice in your code
5. Numbers
Write a program that prints all single digit numbers of base 10 from 0, followed by a new line.
All your code should be in the
mainfunction
6. Numberz
Write a program that prints all single digit numbers of base 10 starting from 0, followed by a new line.
You are not allowed to use any variable of type
charYou can only use the
putcharfunction (every other function (printf, puts,etc…) is forbidden)You can only use
putchartwice in your codeAll your code should be in the
mainfunction
7. Smile in the mirror
Write a program that prints the lowercase alphabet in reverse, followed by a new line
You can only use the
putcharfunction (every other function (printf, puts,etc…) is forbidden)All your code should be in the
mainfunctionYou can only use
putchartwice in your code
8. Hexadecimal
Write a program that prints all the numbers of base 16 in lowercase, followed by a new line
You can only use the
putcharfunction (every other function (printf, puts,etc…) is forbidden)All your code should be in the
mainfunctionYou can only use
putcharthree times in your code
9. Patience, persistence and perspiration make an unbeatable combination for success
Write a program that prints all possible combinations of single-digit numbers
Numbers must be separated
,, followed by a spaceNumbers should be printed in ascending order
You can only use the
putcharfunction (every other function (printf, puts,etc…) is forbidden)All your code should be in the
mainfunctionYou can only use
putcharfour times maximum in your codeYou are not allowed to use any variable of type
char
10. Inventing is a combination of brains and materials. The more brains you use, the less material you need
Write a program that prints all possible different combinations of two digits
Numbers must be separated
,, followed by a spaceThe two digits must be different
01and10are considered the same combination of the digits0and1Print only the smallest combination of the two digits
Numbers should be printed in ascending order, with two digits
You can only use the
putcharfunction (every other function (printf, puts,etc…) is forbidden)You can only use
putcharfive times maximum in your codeYou are not allowed to use any variable of type
charAll your code should be in the
mainfunction
11. The success combination in business is: Do what you do better... and: do more of what you do...
Write a program that prints all possible different combinations of three digits
Numbers must be separated
,, followed by a spaceThe three digits must be different
012, 120, 102, 021, 201, 210are considered the same combination of three digits0, 1, 2Print only the smallest combination of the three digits
Numbers should be printed in ascending order, with three digits
You can only use the
putcharfunction (every other function (printf, puts,etc…) is forbidden)You can only use
putcharsix times maximum in your codeYou are not allowed to use any variable of type
charAll your code should be in the
mainfunction
12. Software is eating the World
Write a program that prints all possible combinations of two-two-digit numbers.
The numbers should range form
0 to 99The two numbers should be separated by a space
All numbers should be printed with two digits.
1should be printed as01The combination of numbers must be separated by comma, followed by a space
The combinations of numbers should be printed in ascending order
00 01and01 00are considered the same combination of the numbers0 and 1You can only use the
putcharfunction (every other function (printf, puts,etc…) is forbidden)You can only use
putchareight times maximum in your codeYou are not allowed to use any variable of type
charAll your code should be in the
mainfunction
Repository
GitHub repository: alx-low_level_programming
Directory: 0x01-variables_if_else_while
File: 0-positive_or_negative.c 1-last_digit.c 2-print_alphabet.c 3-print_alphabets.c 4-print_alphabet.c 5-print_numbers.c 6-print_numberz.c 7-print_tebahpla.c- 8-print_base16.c- 9-print_comb.c 100-print_comb3.c 101-print_comb4.c 102-print_comb5.c