From Frustration to Functionality: Using C to Solve Simple Problems

I'm thrilled to share that this is going to be my first blog post, and I'm really excited about it! My hope is that we can all learn and grow together through this journey. Currently, I'm working on a new project, and I can't wait to see how far we'll come in the upcoming months. I look forward to looking back on our progress together!

Before we dive into the solutions I'll be sharing in this post, I'm assuming that you already have a basic understanding of C programming. However, if you're new to C or need a quick refresher, I highly recommend the book 'Sams Teach Yourself C in 24 Hours.' I found it to be an excellent resource that helped me learn everything I needed to know in just a weekend.

Another thing to note is that C is only a tool and its performance and efficiency depend mainly on the person using it. You have to be very good at problem-solving to get the best out of any programming language. I will recommend this post by CMOE '10 ways to improve problem-solving skills' and 'An ultimate guide that helps you to improve problem-solving in programming' by Simplilearn, they will serve as a starting point on your journey to problem-solving.

Problem 1

A program that assigns a random number to a variable n each time it is executed. It then prints if the variable is positive, negative or zero.

step 1: Understand the problem and develop a pseudocode.

  • define variable n

  • assign to 'n' a random value

  • check if the value is greater than zero

  • if yes .. print - n is positive

  • else if n is less than zero

    • print - n is negative

    • else print - n is zero

step 2: Write your code.

/* Header Files*/
#include <stdlib.h> 
#include <time.h>
/* main function declared*/
int main(void)
{
    int n;

    srand(time(0));
    n = rand() - RAND_MAX / 2;

    if (n > 0)
    {
        printf("%d is positive \n", n);
        return (0);
    }
    else if (n < 0)
    {
        printf("%d is negative \n", n);
        return (0);
    }
    else
    {
        printf("%d is zero \n", n);
        return (0);
    }
    return (0);
}

Problem 2

A program that will assign a random number to the variable n each time it is executed. print "last digit of " n "is" last digit " greater than 5 / and is 0 / and is less than 6 and not 0"

Step 1: Understand the problem and develop the pseudocode

  • Define variable n and last_num

  • assign a random value to n

  • assign to last_num (n % 10)

  • test last_num based on the following

    • last_num > 5 ... print (the last digit of n is last_num and is greater than 5)

    • last_num < 6 && last_num != 0 ... print (last digit of n is last_num and is less than 6 and not 0)

    • last_num == 0 .. print (the last digit of n is last_num and is 0

Step 2: write your code

click this link to access the code directly on GitHub

Problem 3: Write a program that prints the alphabet in lower case followed by a new line using putchar()

step 1: understand the problem and write the pseudo-code

For this particular problem, different approaches will arrive at the same solution it depends on the person solving the problem.

I found it easier to use an ASCII chart for carrying out this exercise, however, some others prefer to use direct substitution of the alphabets 'a' to 'z'.

  • for each lowercase alphabet in the English alphabet:

use putchar() to print the alphabet

for (char c = 'a'; c <= 'z'; c++){
    putchar(c);
}

OR 

for (int i = 97; i <= 122; i++){
    putchar(i);
}

Problem 4: Write a program that prints out the numbers of the Fibonacci sequence up to n.

Step 1: Understand the problem and write the pseudocode

  • Start

  • Read in the value of n, the number of Fibonacci numbers to generate

  • Initialize variables a and b to 0 and 1, respectively

  • For i from 1 to n, do all the following:

    • Print the value of a

    • set a to the value of b, and b to c which is the sum of a and b

#include <stdio.h>

int main(){
    int n, i, a = 0, b = 1, c;
    printf("Enter the no. of Fibinacci numbers to generate: ");
    scanf("%d", &n);
    printf("Fibonacci Series:\n");
    for (i = 0; i < n; i++){
        printf("%d ", a);
        c = a + b;
        a = b;
        b = c;
    }
    return (0);
}

Problem 5: Write a program that prints 1 to 100 and replaces all factors of 3 by Fizz , factors of 5 by Buzz and factors of 3 & 5 by FizzBuzz

  • Start the loop from 1 to 100

  • For each integer i in the lop, do the following:

    • If i is divisible by 3 and 5, print "FizzBuzz"

    • Else if i is divisible by 3 and not 5, print "Fizz"

    • Else if i is divisible by 5 and not 3, print "Buzz"

    • Else print i

Here is a link to the code that I developed on GitHub. You can edit to suite new constraints.

Thank you for reading "From Frustration to Functionality: Using C to solve simple problems". We hope this blog has given you a helpful introduction to programming in C and shown you how it can be used to solve practical problems. While C can seem intimidating at first, with practice and persistence, you can become proficient in this powerful language.

Remember, programming is all about problem-solving and using logic to create solutions. As you continue on your programming journey, don't be affraid to experiment, make mistakes, and learn from them. Keep building your skills, and who knows we may meet one day to create amazing programs together.

Happy coding!

I would like to express my gratitude to the team at ALx Africa for providing a valuable opportunity to numerous individuals seeking to enhance their IT skills. I am privileged to be one of the beneficiaries of this remarkable initiative.