SE-Foundation Help

0x01. C - Variables, if, else, while

Concepts

Resources

1. ascii # You do not need to learn about: 2. scanf 3. getc 4. getchar 5. EOF 6. EXIT_SUCCESS 7. time 8. rand 9. srand 10. RAND_MAX 11. for loops 12. do...while loops 13. functions

    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 ... else statements

    • How to use comments

    • How to declare variables of types char, int, unsigned int

    • How to assign values to variables

    • How to print the values of variables of type char, int, unsigned int, using printf

    • How to use the while loop

    • How to use variables with the while loop

    • How to print variables using printf

    • What are the purpose of the gcc flags -m32 and -m64

      Requirements

      C

      • Allowed editors: vi, vim, emacs

      • All your files will be compiled on Ubuntu 20.04 LTS using gcc, using the options -Wall -Werror -Wextra -pedantic -std=gnu89

      • All your files should end with a new line

      • A README.md file at the root of the repo, containing a description of the repository

      • A README.md file, at the root of the folder of this project, containing a description of the project

      • There should be no errors and no warnings during compilation

      • You are not allowed to use system

      • Your code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl

      Shell Scripts

      • Allowed editors: vi, vim, emacs

      • All 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

      • cd to the Betty directory

      • Install the linter with sudo ./install.sh

      • emacs or vi a new file called betty, 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 betty

      • Move the betty file into /bin/ directory or somewhere else in your $PATH with sudo 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 n will store a different value every time you will run this program

        • You don’t have to understand what rand, srand, RAND_MAX do. Please do not touch this code

        • The output of the program should be:

        • The number, followed by

          • If the number is greater than 0: is positive

          • If the number is 0: is zero

          • If the number is less than 0: is negative

          followed by a new line

        julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-positive_or_negative.c -o 0-positive_or_negative julien@ubuntu:~/0x01$ ./0-positive_or_negative -520693284 is negative julien@ubuntu:~/0x01$ ./0-positive_or_negative -973398895 is negative julien@ubuntu:~/0x01$ ./0-positive_or_negative -199220452 is negative julien@ubuntu:~/0x01$ ./0-positive_or_negative 561319348 is positive julien@ubuntu:~/0x01$ ./0-positive_or_negative 561319348 is positive julien@ubuntu:~/0x01$ ./0-positive_or_negative 266853958 is positive julien@ubuntu:~/0x01$ ./0-positive_or_negative -48147767 is negative julien@ubuntu:~/0x01$ ./0-positive_or_negative 0 is zero julien@ubuntu:~/0x01$

        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 n will store a different value every time you run this program

        • You don’t have to understand what rand, srand, RAND_MAX do. Please do not touch this code

        • The output of the program should be:

          • The string Last digit of ,followed by

          • n ,followed by

          • the string is ,followed by

            • If the last digit of n is greater than: the string and is greater than 5

            • If the last digit of n is 0: and is 0

            • If the last digit of n is less than 6 and not 0: the string and is less than 6 and not 0

            followed by a new line

        julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-last_digit.c -o 1-last_digit julien@ubuntu:~/0x01$ ./1-last_digit Last digit of 629438752 is 2 and is less than 6 and not 0 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of -748255693 is -3 and is less than 6 and not 0 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of -1052791662 is -2 and is less than 6 and not 0 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of -284805734 is -4 and is less than 6 and not 0 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of -284805734 is -4 and is less than 6 and not 0 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of 491506926 is 6 and is greater than 5 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of 954249937 is 7 and is greater than 5 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of 652334952 is 2 and is less than 6 and not 0 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of -729688197 is -7 and is less than 6 and not 0 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of -729688197 is -7 and is less than 6 and not 0 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of 45528266 is 6 and is greater than 5 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of 45528266 is 6 and is greater than 5 julien@ubuntu:~/0x01$ ./1-last_digit Last digit of 809065140 is 0 and is 0 julien@ubuntu:~/0x01$

        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 putchar function (every other function (printf, puts, etc..) is forbidden

        • All your code should be in the main function

        • You can only use putchar twice in your code

        julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-print_alphabet.c -o 2-print_alphabet julien@ubuntu:~/0x01$ ./2-print_alphabet abcdefghijklmnopqrstuvwxyz julien@ubuntu:~/0x01$

          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 putchar function (every other function (printf, puts, etc..) is forbidden

          • All your code should be in the main function

          • You can only use putchar three times in your code

          julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-print_alphabets.c -o 3-print_alphabets julien@ubuntu:~/0x01$ ./3-print_alphabets | cat -e abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$ julien@ubuntu:~/0x01$

            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 q and e

            • You can only use the putchar function (every other function (printf, puts, etc..) is forbidden

            • All your code should be in the main function

            • You can only use putchar twice in your code

            julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-print_alphabet.c -o 4-print_alphabet julien@ubuntu:~/0x01$ ./4-print_alphabt abcdfghijklmnoprstuvwxyz julien@ubuntu:~/0x01$ ./4-print_alphabet | grep [eq] julien@ubuntu:~/0x01$

              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 main function

              julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-print_numbers.c -o 5-print_numbers julien@ubuntu:~/0x01$ ./5-print_numbers 0123456789 julien@ubuntu:~/0x01$

              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 char

              • You can only use the putchar function (every other function (printf, puts, etc…) is forbidden)

              • You can only use putchar twice in your code

              • All your code should be in the main function

              julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 6-print_numberz.c -o 6-print_numberz julien@ubuntu:~/0x01$ ./6-print_numberz 0123456789 julien@ubuntu:~/0x01$

              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 putchar function (every other function (printf, puts, etc…) is forbidden)

              • All your code should be in the main function

              • You can only use putchar twice in your code

              julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 7-print_tebahpla.c -o 7-print_tebahpla julien@ubuntu:~/0x01$ ./7-print_tebahpla zyxwvutsrqponmlkjihgfedcba julien@ubuntu:~/0x01$

              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 putchar function (every other function (printf, puts, etc…) is forbidden)

              • All your code should be in the main function

              • You can only use putchar three times in your code

              julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 8-print_base16.c -o 8-print_base16 julien@ubuntu:~/0x01$ ./8-print_base16 0123456789abcdef julien@ubuntu:~/0x01$

              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 space

              • Numbers should be printed in ascending order

              • You can only use the putchar function (every other function (printf, puts, etc…) is forbidden)

              • All your code should be in the main function

              • You can only use putchar four times maximum in your code

              • You are not allowed to use any variable of type char

              julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 9-print_comb.c -o 9-print_comb julien@ubuntu:~/0x01$ ./9-print_comb | cat -e 0, 1, 2, 3, 4, 5, 6, 7, 8, 9$ julien@ubuntu:~/0x01$

                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 space

                • The two digits must be different

                • 01 and 10 are considered the same combination of the digits 0 and 1

                • Print only the smallest combination of the two digits

                • Numbers should be printed in ascending order, with two digits

                • You can only use the putchar function (every other function (printf, puts, etc…) is forbidden)

                • You can only use putchar five times maximum in your code

                • You are not allowed to use any variable of type char

                • All your code should be in the main function

                julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-print_comb3.c -o 100-print_comb3 julien@ubuntu:~/0x01$ ./100-print_comb3 01, 02, 03, 04, 05, 06, 07, 08, 09, 12, 13, 14, 15, 16, 17, 18, 19, 23, 24, 25, 26, 27, 28, 29, 34, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49, 56, 57, 58, 59, 67, 68, 69, 78, 79, 89 julien@ubuntu:~/0x01$

                  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 space

                  • The three digits must be different

                  • 012, 120, 102, 021, 201, 210 are considered the same combination of three digits 0, 1, 2

                  • Print only the smallest combination of the three digits

                  • Numbers should be printed in ascending order, with three digits

                  • You can only use the putchar function (every other function (printf, puts, etc…) is forbidden)

                  • You can only use putchar six times maximum in your code

                  • You are not allowed to use any variable of type char

                  • All your code should be in the main function

                  julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 101-print_comb4.c -o 101-print_comb4 julien@ubuntu:~/0x01$ ./101-print_comb4 012, 013, 014, 015, 016, 017, 018, 019, 023, 024, 025, 026, 027, 028, 029, 034, 035, 036, 037, 038, 039, 045, 046, 047, 048, 049, 056, 057, 058, 059, 067, 068, 069, 078, 079, 089, 123, 124, 125, 126, 127, 128, 129, 134, 135, 136, 137, 138, 139, 145, 146, 147, 148, 149, 156, 157, 158, 159, 167, 168, 169, 178, 179, 189, 234, 235, 236, 237, 238, 239, 245, 246, 247, 248, 249, 256, 257, 258, 259, 267, 268, 269, 278, 279, 289, 345, 346, 347, 348, 349, 356, 357, 358, 359, 367, 368, 369, 378, 379, 389, 456, 457, 458, 459, 467, 468, 469, 478, 479, 489, 567, 568, 569, 578, 579, 589, 678, 679, 689, 789 julien@ubuntu:~/0x01$

                    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 99

                    • The two numbers should be separated by a space

                    • All numbers should be printed with two digits. 1 should be printed as 01

                    • The combination of numbers must be separated by comma, followed by a space

                    • The combinations of numbers should be printed in ascending order

                    • 00 01 and 01 00 are considered the same combination of the numbers 0 and 1

                    • You can only use the putchar function (every other function (printf, puts, etc…) is forbidden)

                    • You can only use putchar eight times maximum in your code

                    • You are not allowed to use any variable of type char

                    • All your code should be in the main function

                    julien@ubuntu:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 102-print_comb5.c -o 102-print_comb5 julien@ubuntu:~/0x01$ ./102-print_comb5 00 01, 00 02, 00 03, 00 04, 00 05, 00 06, 00 07, 00 08, 00 09, 00 10, 00 11, [...] 40 91, 40 92, 40 93, 40 94, 40 95, 40 96, 40 97, 40 98, 40 99, 41 42, 41 43, 41 44, 41 45, 41 46, 41 47, 41 48, 41 49, 41 50, 41 51, 41 52, 41 53 [...] 93 95, 93 96, 93 97, 93 98, 93 99, 94 95, 94 96, 94 97, 94 98, 94 99, 95 96, 95 97, 95 98, 95 99, 96 97, 96 98, 96 99, 97 98, 97 99, 98 99

                      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

                        Last modified: 05 September 2024