Wednesday, April 30, 2025

C Program - Multiple Circles inside C Array - Circumference and Area Calculator

This program calculates the area and circumference of a circle, given its radius. It prompts the user to enter the radius of the circle, reads in the value using scanf, and then calculates the area and circumference using the formulas "PI * radius * radius" and "2 * PI * radius", respectively, where PI is a constant value defined using the #define preprocessor directive.

After calculating the area and circumference, the program prints the values to the console using printf. Finally, the program returns 0 to indicate successful program execution.

In short, this program is a simple calculator for the area and circumference of a circle, given its radius. 

//calculate multiple circle area and circumference using c array

#include <stdio.h>

#define PI 3.14159
#define SEPARATOR "----------------------------------------- \n"

int main()
{

    float area, circumference;
    int x; // for for loop

    int radiuses[] = {20, 34, 50, 14}; //circles

    int number_of_circles = sizeof(radiuses)/sizeof(radiuses[0]);

    for (x = 0; x < number_of_circles; x++)
    {
        printf("\nCalculation for circle %d with radius %d: \n", x, radiuses[x]);

        area = PI * radiuses[x] * radiuses[x];
        printf("Area: %f \n", area);

        circumference = 2 * PI * radiuses[x];
        printf("Circumference: %f \n", circumference);
        printf(SEPARATOR);
    }

    return 0;

}

Result: 


Calculation for circle 0 with radius 20:
Area: 1256.635986
Circumference: 125.663597
-----------------------------------------

Calculation for circle 1 with radius 34:
Area: 3631.677979
Circumference: 213.628113
-----------------------------------------

Calculation for circle 2 with radius 50:
Area: 7853.975098
Circumference: 314.158997
-----------------------------------------

Calculation for circle 3 with radius 14:
Area: 615.751648
Circumference: 87.964523
-----------------------------------------

Process returned 0 (0x0)   execution time : 0.062 s
Press any key to continue.

Here's a line-by-line explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which is needed for functions like printf and scanf. 

#define PI 3.14159
#define SEPARATOR "----------------------------------------- \n"

These lines define two constant values: "PI" and "SEPARATOR". PI is set equal to 3.14159, and SEPARATOR is set equal to a string of dashes that will be used to separate the calculations for each circle. 

int main()
{
    float area, circumference;
    int x; // for for loop

    int radiuses[] = {20, 34, 50, 14}; //circles

    int number_of_circles = sizeof(radiuses)/sizeof(radiuses[0]);

This is the "main" function of the program. It declares two float variables: "area" and "circumference". It also declares an integer variable "x" to use in a for loop. It then declares an integer array "radiuses" containing the radii of four circles.

The variable "number_of_circles" is set to the number of elements in the "radiuses" array using the sizeof operator. 

    for (x = 0; x < number_of_circles; x++)
    {
        printf("\nCalculation for circle %d with radius %d: \n", x, radiuses[x]);

        area = PI * radiuses[x] * radiuses[x];
        printf("Area: %f \n", area);

        circumference = 2 * PI * radiuses[x];
        printf("Circumference: %f \n", circumference);
        printf(SEPARATOR);
    }

This is a for loop that iterates through the "radiuses" array for each circle. It first prints a message using printf indicating which circle is being calculated, along with its radius. It then calculates the area and circumference of the circle using the formulas "PI * radius * radius" and "2 * PI * radius", respectively, and stores them in the "area" and "circumference" variables.

It then prints the calculated values of the area and circumference to the console using printf, along with the SEPARATOR string to separate the calculations for each circle. 

    return 0;
}

Finally, the main function returns 0 to indicate successful program execution.

In summary, this code calculates the area and circumference of multiple circles based on an array of radii using a for loop. It first defines constant values for PI and a separator string, then reads in the radii from an array and calculates the area and circumference of each circle using formulas. It then prints the calculated values of the area and circumference for each circle to the console, along with a separator string to make it clear which circle the calculations correspond to.

C Program - Calculate Area and Circumference of a Circle

This program calculates the area and circumference of a circle based on a user inputted radius.

It first defines the constant value of PI as 3.14159.

Then, it prompts the user to input the radius of the circle using printf and reads in the user's input using scanf.

It calculates the area of the circle using the formula "PI * radius * radius" and stores it in the "area" variable. It also calculates the circumference of the circle using the formula "2 * PI * radius" and stores it in the "circumference" variable.

Finally, it prints the calculated values of the area and circumference to the console using printf, and returns 0 to indicate successful program execution. 

//are and circumference of a circle
#include <stdio.h>

#define PI 3.14159

int main()
{

    float area, circumference, radius;

    printf("Circle Radius: \n");
    scanf("%f", &radius);

    area = PI * radius * radius;
    printf("Area: %f \n", area);

    circumference = 2 * PI * radius;
    printf("Circumference: %f \n", circumference);

    return 0;

}

Result: 

Circle Radius:
20
Area: 1256.635986
Circumference: 125.663597

Process returned 0 (0x0)   execution time : 1.969 s
Press any key to continue.

Here's a line-by-line explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which is needed for functions like printf and scanf. 

#define PI 3.14159

This line defines a constant value called "PI" and sets it equal to 3.14159. This constant will be used to calculate the area and circumference of the circle. 

int main()
{
    float area, circumference, radius;

    printf("Circle Radius: \n");
    scanf("%f", &radius);

This is the "main" function of the program. It declares three float variables: "area", "circumference", and "radius". It then uses printf to ask the user to input the radius of the circle, and uses scanf to read in the user's input and store it in the "radius" variable. 

    area = PI * radius * radius;
    printf("Area: %f \n", area);

This calculates the area of the circle using the formula "PI * radius * radius" and stores it in the "area" variable. It then uses printf to print the calculated area to the console. 

    circumference = 2 * PI * radius;
    printf("Circumference: %f \n", circumference);

This calculates the circumference of the circle using the formula "2 * PI * radius" and stores it in the "circumference" variable. It then uses printf to print the calculated circumference to the console. 

    return 0;
}

Finally, the main function returns 0 to indicate successful program execution.

C Program - Find Largest Element of an Array

This code finds the largest element in an array of integers by iterating through the array and comparing each element to a variable called "largest_atm".

It starts by assuming that the first element in the array is the largest so far, and then it checks each subsequent element to see if it is larger. If it is, then it updates the value of "largest_atm" to be that element instead. After iterating through the entire array, it returns the value of "largest_atm".

In the "main" function, an array of integers is declared and initialized with some values.

The number of elements in the array is determined using the sizeof operator, which returns the total size of the array in bytes. Since each element in the array is an integer, and an integer takes up 4 bytes of memory, the total size of the array in bytes is equal to the number of elements multiplied by 4.

Therefore, the size of the array in bytes is divided by the size of one element in bytes to get the number of elements in the array. This value is then passed along with the array to the "find_largest" function, which returns the largest element in the array.

This value is then printed to the console using printf. Finally, the main function returns 0 to indicate successful program execution.

//largest elements

#include <stdio.h>

int find_largest(int some_array[], int number_of_elements)
{
    int x, largest_atm;

    largest_atm = some_array[0];

    for (x = 1; x < number_of_elements; x++)
        if (some_array[x] > largest_atm)
            largest_atm = some_array[x];

    return largest_atm;
}

int main()
{

    int some_array[] = {5, 25, 243, 1, -10, -5, 500};

    int number_of_elements = sizeof(some_array)/sizeof(some_array[0]);

    printf("Largest Element %d \n", find_largest(some_array, number_of_elements));

    return 0;
}

Result: 

Largest Element 500

Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.

Here's a line-by-line explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which is needed for functions like printf. 

int find_largest(int some_array[], int number_of_elements)
{
    int x, largest_atm;

    largest_atm = some_array[0];

    for (x = 1; x < number_of_elements; x++)
        if (some_array[x] > largest_atm)
            largest_atm = some_array[x];

    return largest_atm;
}

This is the implementation of the "find_largest" function. It takes two arguments: an array of integers "some_array" and the number of elements in the array "number_of_elements". It declares two integer variables, "x" and "largest_atm", and initializes "largest_atm" to the first element in the array.

It then loops through the rest of the elements in the array, comparing each element to "largest_atm" and updating "largest_atm" if the element is larger.

After iterating through the entire array, it returns the value of "largest_atm". 

int main()
{

    int some_array[] = {5, 25, 243, 1, -10, -5, 500};

    int number_of_elements = sizeof(some_array)/sizeof(some_array[0]);

    printf("Largest Element %d \n", find_largest(some_array, number_of_elements));

    return 0;
}

This is the "main" function of the program. It first declares an array of integers called "some_array" and initializes it with some values.

It then determines the number of elements in the array by dividing the total size of the array in bytes (which is given by "sizeof(some_array)") by the size of one element in bytes (which is given by "sizeof(some_array[0])"). It then calls the "find_largest" function, passing in "some_array" and "number_of_elements" as arguments, and prints the result to the console using printf.

Finally, it returns 0 to indicate successful program execution.

C Program - How do you Add Elements to a 2D Array in C

This C program reads input from the user and stores it in a 2-dimensional integer array called number_arr. It then prints the contents of the array to the console. 

//get stuff into 2d array
#include <stdio.h>

int main()
{

    int a, b; //insertion of elements
    int x, y; //extraction - printing

    int number_arr[2][4];

    //Insertion of elements into 2d array

    for (a = 0; a < 2; a++)
    {
        for (b = 0; b < 4; b++)
        {
            printf("Enter value for number_arr[%d][%d] ", a, b);
            scanf("%d", &number_arr[a][b]);
        }
    }

    //Printing

    for (x = 0; x < 2; x++)
    {
        if (x == 1)
        {
            printf("\n");
        }
        for (y = 0; y < 4; y++)
        {
            printf("%d ", number_arr[x][y]);
        }
    }

    return 0;
}

Result: 

Enter value for number_arr[0][0] 1
Enter value for number_arr[0][1] 2
Enter value for number_arr[0][2] 3
Enter value for number_arr[0][3] 4
Enter value for number_arr[1][0] 5
Enter value for number_arr[1][1] 6
Enter value for number_arr[1][2] 7
Enter value for number_arr[1][3] 8
1 2 3 4
5 6 7 8
Process returned 0 (0x0)   execution time : 20.221 s
Press any key to continue.

Code explanation: 

#include <stdio.h>

This line includes the standard input/output library, which provides the necessary functions for input and output operations in C programs. 

int main()
{

This line declares the main function, which is the entry point for the program. The int before main indicates that the function returns an integer value to the operating system when it terminates. 

    int a, b; //insertion of elements
    int x, y; //extraction - printing

    int number_arr[2][4];

These lines declare the integer variables a, b, x, and y, which will be used to iterate over the rows and columns of the 2-dimensional array number_arr. The array is initialized with 2 rows and 4 columns. 

    //Insertion of elements into 2d array

    for (a = 0; a < 2; a++)
    {
        for (b = 0; b < 4; b++)
        {
            printf("Enter value for number_arr[%d][%d] ", a, b);
            scanf("%d", &number_arr[a][b]);
        }
    }

These lines begin a nested for loop that iterates over the rows and columns of the number_arr array. Inside the loop, the printf function is called to prompt the user to enter a value for the current element of the array, and the scanf function is called to read the user's input and store it in the array. 

    //Printing

    for (x = 0; x < 2; x++)
    {
        if (x == 1)
        {
            printf("\n");
        }
        for (y = 0; y < 4; y++)
        {
            printf("%d ", number_arr[x][y]);
        }
    }

These lines begin a nested for loop that iterates over the rows and columns of the number_arr array. Inside the loop, the printf function is called to print the current element of the array to the console.

If the current row x is equal to 1, a newline character (\n) is printed to the console to start a new line before printing the elements of the second row. 

    return 0;
}

This line ends the main function and returns the integer value 0 to the operating system, indicating that the program has terminated successfully.

Monday, April 28, 2025

C Program - How to Print 2D Array in C with For Loops

This program creates a 2-dimensional integer array number_arr with 2 rows and 4 columns. It then uses a nested for loop to iterate through each element in the array and print its value to the console.

The outer loop iterates through the rows of the array (x), while the inner loop iterates through the columns of the array (y). The if statement inside the outer loop checks if x is equal to 1, and if so, prints a newline character to the console to separate the two rows of the array.

The program then prints each element in the array using the printf() function with the %d format specifier to print integers.

When run, this program will output the following to the console: 

1 2 3 4 
5 6 7 8 

Note that the two commented-out printf() statements at the bottom of the program demonstrate how to access individual elements of the array by their row and column indices. For example, number_arr[0][0] would access the element in the first row and first column of the array, which has a value of 1

#include <stdio.h>

int main()
{

    int x, y;

    int number_arr[2][4] =
    {
        {1, 2, 3, 4},
        {5, 6, 7, 8}
    };

    for (x = 0; x < 2; x++)
    {
        if (x == 1)
        {
            printf("\n");
        }
        for (y = 0; y < 4; y++)
        {
            printf("%d ", number_arr[x][y]);
        }
    }

    return 0;
}

//printf("%d \n", number_arr[0][0]);
//printf("%d \n", number_arr[1][0]);

Result: 

1 2 3 4
5 6 7 8
Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.

Let's break down the C program line by line: 

#include <stdio.h>

This line includes the standard input/output library, which provides the necessary functions for input and output operations in C programs. 

int main()
{

This line declares the main function, which is the entry point for the program. The int before main indicates that the function returns an integer value to the operating system when it terminates. 

    int x, y;

This line declares two integer variables x and y, which will be used to iterate over the rows and columns of the 2-dimensional array number_arr

    int number_arr[2][4] =
    {
        {1, 2, 3, 4},
        {5, 6, 7, 8}
    };

This line initializes a 2-dimensional integer array number_arr with 2 rows and 4 columns. The first row contains the values 1, 2, 3, and 4, while the second row contains the values 5, 6, 7, and 8

    for (x = 0; x < 2; x++)
    {

This line begins a for loop that iterates over the rows of the number_arr array. The loop starts with x equal to 0 and continues until x is less than 2

        if (x == 1)
        {
            printf("\n");
        }

This line checks if the current row x is equal to 1. If it is, a newline character (\n) is printed to the console to start a new line before printing the elements of the second row. 

        for (y = 0; y < 4; y++)
        {
            printf("%d ", number_arr[x][y]);
        }

This line begins a nested for loop that iterates over the columns of the number_arr array. The loop starts with y equal to 0 and continues until y is less than 4.

Inside the nested loop, the printf function is called to print the current element of the array, which is accessed using the indexes x and y. The format specifier %d is used to print an integer value, followed by a space character to separate the values. 

    }

This line ends the nested for loop. 

    return 0;
}

This line ends the main function and returns the integer value 0 to the operating system, indicating that the program has terminated successfully.

C Program - Find Quotient and Remainder - Modulo Operator

This program prompts the user to enter a dividend and a divisor, then performs integer division on them and displays the quotient and remainder of the division operation.

The program uses the % (modulus) operator to calculate the remainder and the / (division) operator to calculate the quotient. It then displays the result using printf().

Note that this program assumes that the user enters valid integer values for the dividend and divisor, and does not perform any input validation or error checking.

In arithmetic, the modulo operation finds the remainder after division of one number by another. Given two positive integers, a (the dividend) and n (the divisor), the modulo operation (denoted by the symbol %) returns the integer remainder of the division a/n.

For example, the expression 13 % 5 would evaluate to 3, because when 13 is divided by 5, the remainder is 3. Similarly, 24 % 7 would evaluate to 3, because 24 divided by 7 is 3 with a remainder of 3.

#include <stdio.h>

int main()
{

    int dividend_num, divisor_num, quotient, remainder;

    printf("Enter Dividend: ");
    scanf("%d", &dividend_num);

    printf("Enter Divisor: ");
    scanf("%d", &divisor_num);

    quotient = dividend_num / divisor_num;

    remainder = dividend_num % divisor_num;

    printf("----------------------- \n");
    printf("Quotient: %d  \n", quotient);
    printf("Remainder: %d \n", remainder);
    printf("----------------------- \n");

    return 0;
}

Result: 

Enter Dividend: 10
Enter Divisor: 3
-----------------------
Quotient: 3
Remainder: 1
-----------------------

Process returned 0 (0x0)   execution time : 7.556 s
Press any key to continue.

Here's an explanation of each block of code in the program: 

#include <stdio.h>

This line includes the standard input-output library header file.

int main()
{

This line declares the main function that runs when the program is executed. The int keyword specifies that the function returns an integer value (0 in this case). 

int dividend_num, divisor_num, quotient, remainder;

This block of code declares four integer variables:

  • dividend_num: the number that will be divided by the divisor_num
  • divisor_num: the number that dividend_num will be divided by
  • quotient: the result of the division operation, which is the whole number part of the answer
  • remainder: the result of the division operation, which is the remainder part of the answer 
printf("Enter Dividend: ");
scanf("%d", &dividend_num);

printf("Enter Divisor: ");
scanf("%d", &divisor_num);

These lines prompt the user to enter the dividend_num and divisor_num values and read them into the corresponding integer variables using scanf()

quotient = dividend_num / divisor_num;

remainder = dividend_num % divisor_num;

These lines perform the division operation on dividend_num and divisor_num and store the results in quotient and remainder respectively. The / operator is used for the division operation, which returns the whole number part of the answer, while the % operator is used for the modulus operation, which returns the remainder part of the answer. 

printf("----------------------- \n");
printf("Quotient: %d  \n", quotient);
printf("Remainder: %d \n", remainder);
printf("----------------------- \n");

These lines use printf() to display the quotient and remainder values, along with some additional formatting. The ----------------------- string is used to separate the output for visual clarity. 

return 0;

This line returns an integer value of 0 to the operating system to indicate that the program executed successfully.

C Program - Find Frequency of a Character in a String

This C program prompts the user to enter a string and a character, then calculates and displays the frequency of the character in the string. 

#include <stdio.h>

int main()
{

    char some_string[1000], ch;
    int counter = 0;

    printf("Enter Some String: \n");
    gets(some_string);

    printf("Enter a Character: \n");
    scanf("%c", &ch);

    for (int x = 0; some_string[x] != '\0'; x++)
    {
        if (ch == some_string[x])
        {
            counter++;
            //printf("Counter atm: %d ", counter);
            //printf("Detected :%c \n", ch);
        }
    }

    printf("Frequency of %c character: %d \n", ch, counter);

    return 0;
}

Result: 

Enter Some String:
this is some test
Enter a Character:
t
Frequency of t character: 3

Process returned 0 (0x0)   execution time : 5.396 s
Press any key to continue.

Here's a detailed explanation of each block of code in the program: 

#include <stdio.h>

This line includes the standard input-output library header file. 

int main()
{

This line declares the main function that runs when the program is executed. The int keyword specifies that the function returns an integer value (0 in this case). 

char some_string[1000], ch;
int counter = 0;

This block of code declares three variables:

  • some_string: a character array that can hold up to 1000 characters
  • ch: a single character variable to store the character to be searched for
  • counter: an integer variable that will be used to count the frequency of the character in the string 
printf("Enter Some String: \n");
gets(some_string);

These lines prompt the user to enter a string and then reads the input string into the some_string array using the gets() function. Note that gets() is generally considered unsafe to use because it can cause a buffer overflow if the user inputs more than the allocated space for some_string. It is recommended to use fgets() instead, which limits the amount of input read. 

printf("Enter a Character: \n");
scanf("%c", &ch);

These lines prompt the user to enter a character and then reads the input character into the ch variable using the scanf() function. 

for (int x = 0; some_string[x] != '\0'; x++)
{
    if (ch == some_string[x])
    {
        counter++;
    }
}

This block of code is a for loop that iterates over the characters in the some_string array using an index variable x. The loop continues until it reaches the null character '\0', which marks the end of the string. Inside the loop, the program checks if the current character at index x is equal to the character ch. If it is, the counter variable is incremented. 

printf("Frequency of %c character: %d \n", ch, counter);

This line uses printf() to display the frequency of the character in the string. The %c format specifier is used to print the character ch, and the %d format specifier is used to print the integer counter

return 0;

This line returns an integer value of 0 to the operating system to indicate that the program executed successfully.

C Program - Get the String Length with While Loop

This C program prompts the user to enter a string and then calculates and displays the length of the string. 

#include <stdio.h>

int main()
{

    char some_string[100];
    int x;

    printf("Enter Some String: \n");
    scanf("%s", some_string);

    while (some_string[x] != '\0')
    {
        x++;
    }
    printf("Total Length: %d \n", x);

    return 0;
}

Result: 

Enter Some String:
something
Total Length: 9

Process returned 0 (0x0)   execution time : 2.677 s
Press any key to continue.
Line by line explanation: 
#include <stdio.h>

This line includes the standard input-output library header file, which is needed for using standard functions like printf() and scanf()

int main()
{

This is the starting point of the program where the main() function is defined. This function is required in every C program and serves as the entry point of the program. The function signature int main() indicates that the function returns an integer value upon completion. 

    char some_string[100];
    int x;

This code block declares two variables: some_string, which is a character array of size 100 and x, which is an integer variable. 

    printf("Enter Some String: \n");
    scanf("%s", some_string);

These lines of code print a prompt to the user to enter a string using printf() and then reads in the input string using scanf(). %s format specifier in the scanf() function indicates that it will read in a string of characters from the user. 

    while (some_string[x] != '\0')
    {
        x++;
    }

This code block uses a while loop to iterate through the character array some_string until it reaches the null character '\0'. The null character marks the end of a C-style string. In each iteration, the loop increments the value of the x variable, which keeps track of the number of characters in the string. 

    printf("Total Length: %d \n", x);
    return 0;
}

Finally, this code block uses printf() to display the length of the string by printing the value of the x variable. The program then returns 0 to indicate successful execution of the program.

C Program – Find Length of a String without strlen() Function

This program takes a string input from the user using the scanf() function and stores it in the some_string character array. It then uses a for loop to iterate over the characters of the string until it encounters the null terminator character '\0'. During each iteration of the loop, it prints the length of the string up to that point using the printf() function.

Therefore, the program calculates and outputs the length of the input string entered by the user.

#include <stdio.h>

int main()
{

    char some_string[100];
    int x;

    printf("Enter Some String: \n");
    scanf("%s", some_string);

    for(x = 0; some_string[x]!='\0'; x++)
    {
        printf("Length atm: %d \n", x+1);
    }

    return 0;
}

Result: 

Enter Some String:
test
Length atm: 1
Length atm: 2
Length atm: 3
Length atm: 4

Process returned 0 (0x0)   execution time : 1.031 s
Press any key to continue.

Here's a line-by-line explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which contains functions for input/output operations. 

int main()
{

This is the beginning of the main function, which is the entry point for the program. 

    char some_string[100];
    int x;

Here, we declare a character array variable named some_string that can hold up to 100 characters and an integer variable named x that will be used to keep track of the loop iterations. 

    printf("Enter Some String: \n");
    scanf("%s", some_string);

This code prompts the user to enter a string and stores it in the some_string variable using the scanf() function. 

    for(x = 0; some_string[x]!='\0'; x++)
    {
        printf("Length atm: %d \n", x+1);
    }

This is a for loop that iterates through the string some_string until it encounters the null character \0, which marks the end of the string. The loop body simply prints the current length of the string on each iteration, starting from 1 because the x variable starts at 0. 

    return 0;
}

This line simply ends the program and returns 0 to indicate successful execution.

C Program - Power of a Number for Elements inside C Array

This C program calculates the power of each element in an array to a fixed exponent and displays the results. 

#include <stdio.h>
#include <math.h>

int main()
{

    int numbers[] = {2, 3, 4, 5, 6};
    int exponent = 3;

    int number_of_elements = sizeof(numbers)/sizeof(numbers[0]);

    printf("Number of elements: %d \n", number_of_elements);

    for (int x = 0; x < number_of_elements; x++)
    {
        printf("Number: %d, Exponent: %d, Result: %d \n", numbers[x], exponent, (int)pow(numbers[x], exponent));
    }

    return 0;

}

Result: 

Number of elements: 5
Number: 2, Exponent: 3, Result: 8
Number: 3, Exponent: 3, Result: 27
Number: 4, Exponent: 3, Result: 64
Number: 5, Exponent: 3, Result: 125
Number: 6, Exponent: 3, Result: 216

Process returned 0 (0x0)   execution time : 0.078 s
Press any key to continue.

Here's a line-by-line explanation of the code: 

#include <stdio.h>
#include <math.h>

These are header files which provide the necessary libraries to perform input/output operations and mathematical calculations using the pow() function. 

int main()
{
    int numbers[] = {2, 3, 4, 5, 6};
    int exponent = 3;

    int number_of_elements = sizeof(numbers)/sizeof(numbers[0]);

This declares an integer array numbers and initializes it with some values. It also declares an integer variable exponent and initializes it to 3. The number_of_elements variable is initialized to the size of the numbers array divided by the size of one element in the array. 

    printf("Number of elements: %d \n", number_of_elements);

    for (int x = 0; x < number_of_elements; x++)
    {
        printf("Number: %d, Exponent: %d, Result: %d \n", numbers[x], exponent, (int)pow(numbers[x], exponent));
    }

    return 0;
}

This displays the number of elements in the numbers array using printf(). Then, it loops through each element in the numbers array using a for loop, calculates the power of the element raised to the exponent using the pow() function from the math.h library, and displays the result using printf(). Finally, the program ends with a return value of 0.

C Program - Calculate Power of a Number using pow() function

This C program calculates the power of a base number raised to an exponent entered by the user, using the pow() function from the math.h library.

It prompts the user to enter the base number and exponent using printf() and scanf(), respectively. It then uses the pow() function to calculate the power of the base number raised to the exponent and stores the result in the result variable.

After that, it displays the final result using printf() statement.

#include <stdio.h>
#include <math.h>

int main()
{

    int base_number, exponent, result;

    printf("Enter the Base Number: \n");
    scanf("%d", &base_number);

    printf("Enter Exponent: \n");
    scanf("%d", &exponent);

    result = pow(base_number, exponent);

    printf("%d to the Power of %d is: %d \n", base_number, exponent, result);

    return 0;

}

Result: 

Enter the Base Number:
10
Enter Exponent:
3
10 to the Power of 3 is: 1000

Process returned 0 (0x0)   execution time : 2.725 s
Press any key to continue.

Here's an explanation of the code line by line: 

#include <stdio.h>
#include <math.h>

These two lines include the standard input-output library and the math library in C programming. 

int main()
{
    int base_number, exponent, result;

This is the main function of the program. It declares three integer variables: base_number, exponent, and result

    printf("Enter the Base Number: \n");
    scanf("%d", &base_number);

    printf("Enter Exponent: \n");
    scanf("%d", &exponent);

These printf() statements display prompts for the user to enter the base_number and exponent values respectively. The scanf() function reads in integer values for these variables from the user input using the %d format specifier. 

    result = pow(base_number, exponent);

This line uses the pow() function from the math.h library to calculate the power of the base_number raised to the exponent. The pow() function returns a double value, which is then stored in the integer variable result

    printf("%d to the Power of %d is: %d \n", base_number, exponent, result);

    return 0;
}

This printf() statement displays the final result of the calculation, which is the base_number raised to the exponent, using the %d format specifier to print integer values. The program ends with a return value of 0.

C Program - Calculate Power of a Number using While Loop

This C program calculates the power of a base number raised to an exponent entered by the user.

It prompts the user to enter the base number and exponent using printf() and scanf(), respectively. It then uses a while loop to calculate the power of the base number raised to the exponent. Inside the loop, it multiplies the result variable by base_number for each iteration until the temp_exponent reaches 0.

After the loop, it prints the final value of result using printf().

#include <stdio.h>

int main()
{

    int base_number, exponent;
    int result = 1;

    printf("Enter the Base Number: \n");
    scanf("%d", &base_number);

    printf("Enter Exponent: \n");
    scanf("%d", &exponent);

    int temp_exponent = exponent;

    while (temp_exponent != 0)
    {
        result = result * base_number;
        printf("Result atm: %d \n", result);
        printf("Exponent atm: %d \n", temp_exponent);
        --temp_exponent;
    }

    printf("Final Result: %d \n", result);

    return 0;
}

Result: 

Enter the Base Number:
10
Enter Exponent:
3
Result atm: 10
Exponent atm: 3
Result atm: 100
Exponent atm: 2
Result atm: 1000
Exponent atm: 1
Final Result: 1000

Process returned 0 (0x0)   execution time : 4.000 s
Press any key to continue.

Here's an explanation of the code line by line: 

#include <stdio.h>

This line includes the standard input-output library in C programming. 

int main()
{
    int base_number, exponent;
    int result = 1;

This is the main function of the program. It declares three integer variables: base_number, exponent, and result. result is initialized to 1. 

    printf("Enter the Base Number: \n");
    scanf("%d", &base_number);

    printf("Enter Exponent: \n");
    scanf("%d", &exponent);

These printf() statements display prompts for the user to enter the base_number and exponent values respectively. The scanf() function reads in integer values for these variables from the user input using the %d format specifier. 

    int temp_exponent = exponent;

    while (temp_exponent != 0)
    {
        result = result * base_number;
        printf("Result atm: %d \n", result);
        printf("Exponent atm: %d \n", temp_exponent);
        --temp_exponent;
    }

This while loop executes result = result * base_number for each value of temp_exponent from exponent down to 0. The --temp_exponent expression decrements temp_exponent by 1 at the end of each iteration of the loop. Inside the loop, printf() statements display the current value of result and temp_exponent

    printf("Final Result: %d \n", result);

    return 0;
}

After the loop is completed, the final value of result is displayed using printf(). The program ends with a return value of 0.

C Program - Display All Alphabets And SKIP Special Characters

This is a C program that displays the ASCII codes and corresponding characters for all the alphabets in the English language.  

#include <stdio.h>

int main()
{

    int x;

    for (x = 65; x <= 122; x++)
    {
        if (x != 91 &&
                x != 92 &&
                x != 93 &&
                x != 94 &&
                x != 95 &&
                x != 96)
        {
            printf("%d <--> %c \n", x, x);
        }
        else
        {
            printf("%d <--> %c character is not an alphabet. \n ", x, x);
        }
    }

    return 0;
}

Result: 

65 <--> A
66 <--> B
67 <--> C
68 <--> D
69 <--> E
70 <--> F
71 <--> G
72 <--> H
73 <--> I
74 <--> J
75 <--> K
76 <--> L
77 <--> M
78 <--> N
79 <--> O
80 <--> P
81 <--> Q
82 <--> R
83 <--> S
84 <--> T
85 <--> U
86 <--> V
87 <--> W
88 <--> X
89 <--> Y
90 <--> Z
91 <--> [ character is not an alphabet.
 92 <--> \ character is not an alphabet.
 93 <--> ] character is not an alphabet.
 94 <--> ^ character is not an alphabet.
 95 <--> _ character is not an alphabet.
 96 <--> ` character is not an alphabet.
 97 <--> a
98 <--> b
99 <--> c
100 <--> d
101 <--> e
102 <--> f
103 <--> g
104 <--> h
105 <--> i
106 <--> j
107 <--> k
108 <--> l
109 <--> m
110 <--> n
111 <--> o
112 <--> p
113 <--> q
114 <--> r
115 <--> s
116 <--> t
117 <--> u
118 <--> v
119 <--> w
120 <--> x
121 <--> y
122 <--> z

Process returned 0 (0x0)   execution time : 0.073 s
Press any key to continue.

Here's a detailed explanation of the code, line by line: 

#include <stdio.h>

This line includes the standard input-output library in C programming.

int main()
{
    int x;

This is the main function of the program. It declares an integer variable x

    for (x = 65; x <= 122; x++)
    {

This is a for loop that starts with x being assigned a value of 65, which is the ASCII code for the uppercase letter "A". The loop will continue as long as x is less than or equal to the ASCII code for the lowercase letter "z", which is 122. 

        if (x != 91 && x != 92 && x != 93 && x != 94 && x != 95 && x != 96)
        {
            printf("%d <--> %c \n", x, x);
        }

This block of code uses if statements to check if x is not one of the ASCII codes for the non-alphabetic characters '[', '', ']', '^', '_', and ''. If xis not one of these characters, theprintf()function will print the ASCII code and the corresponding character using the%dand%c` format specifiers. 

        else
        {
            printf("%d <--> %c character is not an alphabet. \n ", x, x);
        }

If x is one of the non-alphabetic characters, the printf() function will print the ASCII code and the corresponding character along with the message "character is not an alphabet". 

    }
    return 0;
}

This is the end of the for loop. After the loop is completed, the program ends with a return value of 0.

C Program - Convert Uppercase String to Lowercase String

This is a C program that converts uppercase letters in a string to lowercase letters.

#include <stdio.h>
#include <string.h>

int main()
{

    char some_string[100];
    int x;

    printf("Enter the String: \n");
    scanf("%s", some_string);

    for (x = 0; x <= strlen(some_string); x++)
    {
        if (some_string[x] >= 65 && some_string[x] <= 90)
        {
            some_string[x] = some_string[x] + 32;
            printf("Individual character: %d <--> %c \n", some_string[x], some_string[x]);
        }
    }

    printf("Lowercase: %s\n", some_string);

    return 0;
}

Result :

Enter the String:
SOMETHING
Individual character: 115 <--> s
Individual character: 111 <--> o
Individual character: 109 <--> m
Individual character: 101 <--> e
Individual character: 116 <--> t
Individual character: 104 <--> h
Individual character: 105 <--> i
Individual character: 110 <--> n
Individual character: 103 <--> g
Lowercase: something

Process returned 0 (0x0)   execution time : 4.172 s
Press any key to continue.

Let's go through the code line by line to understand how it works: 

#include <stdio.h>
#include <string.h>

These are header files that are needed to use standard input/output functions and string manipulation functions. 

int main()
{
    char some_string[100];
    int x;
    
    printf("Enter the String: \n");
    scanf("%s", some_string);

This is the main function of the program. It declares a character array called some_string of size 100 and an integer variable x. The printf function displays the message "Enter the String: " on the screen. The scanf function reads a string input from the user and stores it in the some_string array. 

    for (x = 0; x <= strlen(some_string); x++)
    {
        if (some_string[x] >= 65 && some_string[x] <= 90)
        {
            some_string[x] = some_string[x] + 32;
            printf("Individual character: %d <--> %c \n", some_string[x], some_string[x]);
        }
    }

This is a for loop that iterates through each character in the some_string array using an index variable x. The strlen function returns the length of the string in some_string.

The loop checks if the current character in some_string is an uppercase letter by comparing its ASCII value to the ASCII values of 'A' (65) and 'Z' (90). If the character is an uppercase letter, it is converted to lowercase by adding 32 to its ASCII value, which corresponds to the difference between the uppercase and lowercase letters in the ASCII table.

The printf function displays the individual character that has been converted to lowercase along with its ASCII code. 

    printf("Lowercase: %s\n", some_string);
    return 0;
}

After the for loop has finished iterating through the characters in the string, the final lowercase string is displayed using the printf function, and the program ends with a return value of 0.

C Program - Convert Lowercase String to Uppercase String

 This program takes a string input from the user and converts all the lowercase characters in the string to uppercase characters.

#include<stdio.h>
#include<string.h>

int main()
{

    char some_string[100];
    int x;

    printf("Enter the String:");
    scanf("%s", some_string);

    for(x = 0; x <= strlen(some_string); x++)
    {
        if (some_string[x] >= 97 && some_string[x] <= 122)
            some_string[x] = some_string[x] - 32;
    }

    printf("\nUppercase is: %s", some_string);

    return 0;
}

Result: 

Enter the String:something

Uppercase is: SOMETHING
Process returned 0 (0x0)   execution time : 4.734 s
Press any key to continue.

Here is a block by block explanation of the code: 

#include <stdio.h>
#include <string.h>

int main()
{

This code block includes the necessary header files and declares the main function. 

    char some_string[100];
    int x;

This declares a character array to store the input string and an integer variable to iterate over the string. 

    printf("Enter the String:");
    scanf("%s", some_string);

This prompts the user to enter a string and reads the input string into the some_string character array. 

    for(x = 0; x <= strlen(some_string); x++)
    {
        if (some_string[x] >= 97 && some_string[x] <= 122)
            some_string[x] = some_string[x] - 32;
    }

This loop iterates through the input string and converts any lowercase characters to uppercase. The strlen() function is used to determine the length of the string. The if statement checks whether the character is a lowercase letter by comparing its ASCII code to the values for lowercase letters. If it is, it is converted to uppercase by subtracting 32 from its ASCII code. 

    printf("\nUppercase is: %s", some_string);

    return 0;
}

Finally, this code block prints the converted string to the console and returns 0 to indicate successful program execution.

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...