Monday, April 28, 2025

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.

C Program - Check whether an Alphabet is Vowel or Consonant

This C program checks whether a given character is a vowel or a consonant.

#include <stdio.h>
#include <stdbool.h>

int main()
{
    char ch;
    bool isVowel = false;

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

    if(ch =='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'
            ||ch=='o'||ch=='O'||ch=='u'||ch=='U')
    {
        isVowel = true;
    }

    if (isVowel == true)
    {
        printf("%c is a Vowel", ch);
    }
    else
    {
        printf("%c is a Consonant", ch);
    }

    return 0;
}

Result: 

Enter Character:
b
b is a Consonant
Process returned 0 (0x0)   execution time : 3.969 s
Press any key to continue.

One more time:

Enter Character:
e
e is a Vowel
Process returned 0 (0x0)   execution time : 1.785 s
Press any key to continue.

Here's a line-by-line explanation: 

#include <stdio.h>
#include <stdbool.h>

These lines include the standard input/output library header file and the header file for the bool data type. 

int main()
{
    char ch;
    bool isVowel = false;

This declares the main function and two variables ch and isVowel. ch is a character variable to store the input character and isVowel is a boolean variable to store whether the input character is a vowel or not. 

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

These lines prompt the user to enter a character and read its value from the standard input using the scanf() function. 

    if(ch =='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'
            ||ch=='o'||ch=='O'||ch=='u'||ch=='U')
    {
        isVowel = true;
    }

This if statement checks if the input character is a vowel. If the character is a vowel, the boolean variable isVowel is set to true

    if (isVowel == true)
    {
        printf("%c is a Vowel", ch);
    }
    else
    {
        printf("%c is a Consonant", ch);
    }

This if-else statement checks the value of the isVowel variable. If it is true, the program prints that the input character is a vowel; otherwise, it prints that the input character is a consonant. 

    return 0;
}

This line indicates the end of the main() function and returns 0, indicating that the program has executed successfully.

C Program - Check if Numbers are Even in Range from 1 to n

This C program prints all even numbers in the range from 1 to n.  

#include <stdio.h>

int main ()
{

    int x, number;

    printf("Range from 1 to n: \n");
    scanf("%d", &number);

    for (x = 1; x <= number; x++)
    {
        if (x % 2 == 0)
        {
            printf("%d \n", x);
        }
    }

    return 0;
}

Result:

Range from 1 to n:
10
2
4
6
8
10

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

Here's a line-by-line explanation: 

#include <stdio.h>

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

int main ()
{

This line defines the main function of the program. 

    int x, number;

This line declares two integer variables x and number

    printf("Range from 1 to n: \n");
    scanf("%d", &number);

These lines prompt the user to enter the value of number and read its value from the standard input using the scanf() function. 

    for (x = 1; x <= number; x++)
    {
        if (x % 2 == 0)
        {
            printf("%d \n", x);
        }
    }

This for loop iterates over all integers from 1 to number (inclusive) and prints the even numbers using the printf() function. The if statement checks whether x is even or not by checking whether x % 2 is equal to 0. If x is even, it is printed to the standard output. 

    return 0;
}

This line indicates the end of the main() function and returns 0, indicating that the program has executed successfully.

C Program - Check if Number is Even or Odd

 This C program takes an integer as input and checks whether it is an even or odd number.

#include<stdio.h>

int main()
{

    int number;

    printf("Enter an Integer: ");
    scanf("%d",&number);

    if ( number % 2 == 0 )
    {
        printf("%d is an even number", number);
    }
    else
    {
        printf("%d is an odd number", number);
    }

    return 0;
}

Result:

Enter an Integer: 2
2 is an even number
Process returned 0 (0x0)   execution time : 2.739 s
Press any key to continue.

One more time: 

Enter an Integer: 5
5 is an odd number
Process returned 0 (0x0)   execution time : 1.359 s
Press any key to continue.

Let's go through it line by line:

 
#include<stdio.h>

 

This line includes the standard input/output header file.

 
int main()
{

 

This line defines the main function of the program.

 
    int number;

 

This line declares an integer variable number which will store the user input.

 
    printf("Enter an Integer: ");
    scanf("%d",&number);

 

These lines print a message asking the user to enter an integer and read the integer input from the user using the scanf() function. %d is the format specifier used to read an integer input from the user.

 
    if ( number % 2 == 0 )
    {
        printf("%d is an even number", number);
    }
    else
    {
        printf("%d is an odd number", number);
    }

 

This if-else statement checks whether the entered number is even or odd. The % operator calculates the remainder of dividing the entered number by 2. If the remainder is 0, the number is even, and the program prints the message "number is an even number" using the printf() function. If the remainder is not 0, the number is odd, and the program prints the message "number is an odd number" using the printf() function.

 
    return 0;
}

 

This line indicates the end of the main() function and returns 0, indicating that the program has executed successfully.

C Program - Display Characters from A to Z Using For Loop

This C code block is a program that prints the ASCII codes and corresponding characters for uppercase and lowercase alphabets.

#include <stdio.h>

int main()
{

    char char_atm;

    printf("----------- \n");
    printf("Uppercase:  \n");
    printf("----------- \n");

    for (char_atm = 'A'; char_atm <= 'Z'; char_atm++)
    {
        printf("%d <--> %c \n", char_atm, char_atm);
    }

    printf("----------- \n");
    printf("Loweracse:  \n");
    printf("----------- \n");

    for (char_atm = 'a'; char_atm <= 'z'; char_atm++)
    {
        printf("%d <--> %c \n", char_atm, char_atm);
    }

    return 0;
}

Result: 

-----------
Uppercase:
-----------
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
-----------
Loweracse:
-----------
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.109 s
Press any key to continue.

Let's go through it line by line: 

#include <stdio.h>

This line includes the standard input/output header file. 

int main()
{

This line defines the main function of the program. 

    char char_atm;

This line declares a character variable char_atm which will be used to store the alphabets. 

    printf("----------- \n");
    printf("Uppercase:  \n");
    printf("----------- \n");

These lines print a header indicating the following output will show the ASCII codes and corresponding characters for uppercase alphabets. 

    for (char_atm = 'A'; char_atm <= 'Z'; char_atm++)
    {
        printf("%d <--> %c \n", char_atm, char_atm);
    }

This for loop iterates through each uppercase alphabet from 'A' to 'Z', and for each alphabet, it prints the corresponding ASCII code and character using the printf() function. %d represents the ASCII code and %c represents the corresponding character. 

    printf("----------- \n");
    printf("Loweracse:  \n");
    printf("----------- \n");

These lines print a header indicating the following output will show the ASCII codes and corresponding characters for lowercase alphabets. 

    for (char_atm = 'a'; char_atm <= 'z'; char_atm++)
    {
        printf("%d <--> %c \n", char_atm, char_atm);
    }

This for loop iterates through each lowercase alphabet from 'a' to 'z', and for each alphabet, it prints the corresponding ASCII code and character using the printf() function. %d represents the ASCII code and %c represents the corresponding character. 

    return 0;
}

This line indicates the end of the main() function and returns 0, indicating that the program has executed successfully.

C Program - Multiplication Table of a Numbers in a Given Range

This C code block is a program that prints the multiplication table for a given starting number and stopping number.  

#include <stdio.h>

int main()
{

    int num1, num2;

    printf("Enter Starting Point: \n");
    scanf("%d", &num1);

    while (num2 <= 0)
    {
        printf("Enter Stoping Point: \n");
        scanf("%d", &num2);
    }

    printf("Multiplication rable for %d and %d \n", num1, num2);

    for (int x = 1; x <= num2; x++)
    {
        printf("%d * %d = %d \n", num1, x, num1 * x);
    }

    return 0;
}

Result: 

Enter Starting Point:
5
Enter Stoping Point:
15
Multiplication rable for 5 and 15
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
5 * 11 = 55
5 * 12 = 60
5 * 13 = 65
5 * 14 = 70
5 * 15 = 75

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

Let's go through it line by line: 

#include <stdio.h>

This line includes the standard input/output header file. 

int main()
{

This line defines the main function of the program. 

    int num1, num2;

This line declares two integer variables num1 and num2 which will be used to store the starting and stopping points of the multiplication table. 

    printf("Enter Starting Point: \n");
    scanf("%d", &num1);

These lines prompt the user to enter the starting point of the multiplication table and store it in the variable num1 using the scanf() function. 

    while (num2 <= 0)
    {
        printf("Enter Stoping Point: \n");
        scanf("%d", &num2);
    }

These lines prompt the user to enter the stopping point of the multiplication table and store it in the variable num2 using the scanf() function. If the user enters a non-positive number, the program will continue prompting the user until a positive number is entered. 

    printf("Multiplication rable for %d and %d \n", num1, num2);

This line prints the starting and stopping points of the multiplication table. 

    for (int x = 1; x <= num2; x++)
    {
        printf("%d * %d = %d \n", num1, x, num1 * x);
    }

These lines use a for loop to iterate through each number in the multiplication table, from 1 to num2. For each number, the program calculates the product of num1 and the current number and prints it in the format of num1 * x = product

    return 0;
}

This line indicates the end of the main() function and returns 0, indicating that the program has executed successfully.

C Program - Generate Multiplication Table with User Input

 This program aims to generate a multiplication table for a given number that the user enters.

#include <stdio.h>

int main()
{

    int number, x;

    printf("Enter an integer: ");
    scanf("%d", &number);

    printf("Multiplication table of %d: \n", number);

    for (x = 1; x <= 10; x++)
    {
        printf("%d * %d = %d \n", number, x, number * x);
    }

    return 0;
}

Result: 

Enter an integer: 1
Multiplication table of 1:
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10

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

At first, the program declares two variables, number and x, both of type int. number is the input integer that the user enters, and x is used as a loop variable to iterate over the multiplication table.

Next, the program prompts the user to enter an integer by printing the message "Enter an integer: " using the printf function. Then, the scanf function is used to read the integer entered by the user and store it in the variable number.

After that, the program prints a message using printf to indicate the start of the multiplication table for the entered integer. It uses the value of number to generate the message by using a format specifier %d which is replaced by the value of number in the output message.

The core of the program is a for loop, which iterates over the multiplication table from 1 to 10. Within the for loop, the printf function is used to print the multiplication table of the entered number. It uses format specifiers %d and %d * %d = %d \n to generate the output message. %d is replaced by the value of number and x, and %d * %d = %d \n is replaced by the result of the multiplication. The \n at the end of the format specifier indicates that a new line should be added after each iteration of the loop.

Finally, the program ends by returning 0 to the operating system to indicate that the program has completed successfully.

C Program - Find Greatest of Three Numbers using If Statement

This code is a program to find the largest number among three given numbers. It starts by declaring three integer variables named num1, num2, and num3.

Then the program prompts the user to input the values for these variables using printf() and scanf() statements.

After that, the program checks for three conditions using if-else statements to determine which number is the largest.

#include <stdio.h>

int main() {

  int num1, num2, num3;

  printf("Enter first number: ");
  scanf("%d", &num1);

  printf("Enter second number: ");
  scanf("%d", &num2);

  printf("Enter third number: ");
  scanf("%d", &num3);

  if (num1 == num2 && num2 == num3){
    printf("Same numbers. \n");
  }
  else if (num1 >= num2 && num1 >= num3)
    printf("%d is the largest number. \n", num1);

  else if (num2 >= num1 && num2 >= num3)
    printf("%d is the largest number. \n", num2);

  else if (num3 >= num1 && num3 >= num2)
    printf("%d is the largest number. \n", num3);

  return 0;
}

Result: 

Enter first number: 10
Enter second number: 20
Enter third number: 30
30 is the largest number.

Process returned 0 (0x0)   execution time : 3.214 s
Press any key to continue.
  • If all three numbers are equal, the program prints "Same numbers.".
  • If num1 is greater than or equal to num2 and num1 is greater than or equal to num3, the program prints "num1 is the largest number.".
  • If num2 is greater than or equal to num1 and num2 is greater than or equal to num3, the program prints "num2 is the largest number.".
  • If num3 is greater than or equal to num1 and num3 is greater than or equal to num2, the program prints "num3 is the largest number.".

Finally, the program returns 0 to indicate successful execution.

C Program - Find the Average of Integers using a Function with Return Value

This code defines a function avg_calc that calculates the average of two integer numbers and returns the result as a float value. The main function uses this function to calculate the average of two numbers inputted by the user. 

#include <stdio.h>

float avg_calc(int x, int y)
{
    float result = (x + y) / 2;
    return result;
}

int main()
{
    int number1, number2;
    float average;

    printf("Simple Average Calculator: \n");

    printf("Enter First Number: \n");
    scanf("%d", &number1);

    printf("Enter Second Number: \n");
    scanf("%d", &number2);

    average = avg_calc(number1, number2);

    printf("Average of %d and %d is: %10.2f \n", number1, number2, average);

    return 0;
}

Result:

Simple Average Calculator:
Enter First Number:
10
Enter Second Number:
6
Average of 10 and 6 is:       8.00

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

The program starts by including the stdio.h header file which provides input/output functions like printf and scanf.

The avg_calc function takes two integer parameters, x and y. It calculates their average by adding them and dividing the result by 2. The average is stored in a float variable result and returned.

The main function starts by declaring three variables: number1, number2, and average. It then prints a prompt asking the user to enter the first number using printf and reads the user's input using scanf.

Similarly, it prompts the user to enter the second number and reads their input using scanf.

Next, it calls the avg_calc function with the two numbers inputted by the user, and stores the returned value in the average variable.

Finally, the main function prints the result using printf with a formatted string that displays the inputted numbers and the calculated average with two decimal places.

The program ends by returning 0 from the main function.

C Program - Find the Average of Two Numbers with User Input

 This code is a C program that calculates the average of two numbers input by the user. It begins by including the standard input-output library, stdio.h.

#include <stdio.h>

int main()
{
    int first_number, second_number;
    float average;

    printf("Enter first number: ");
    scanf("%d", &first_number);

    printf("Enter second number: ");
    scanf("%d", &second_number);

    average = (float)(first_number + second_number)/2;

    printf("Average of %d and %d is: %.2f", first_number, second_number, average);

    return 0;
}

Result:

Enter first number: 10
Enter second number: 5
Average of 10 and 5 is: 7.50
Process returned 0 (0x0)   execution time : 5.174 s
Press any key to continue.

The main() function is then declared, which returns an integer value. Inside the function, three variables are declared - two integers first_number and second_number to hold the user input values, and a float average to hold the average value.

The program then uses the printf() function to display a prompt message asking the user to enter the first number. The scanf() function is used to read the integer input value from the user and store it in the first_number variable. Similarly, the program prompts the user to enter the second number, reads the input value and stores it in the second_number variable using scanf().

Next, the program calculates the average of the two numbers by adding them together and dividing the sum by 2. Since we want the average to be in decimal form, we use typecasting to convert one of the integers to a float value before performing the division. The result is stored in the average variable.

Finally, the program uses printf() function again to display the average value with two decimal places. The values of first_number, second_number, and average are passed as arguments to printf() using format specifiers %d and %f. The specifier .2 before f is used to specify that the number should be displayed with two decimal places.

The return 0 statement at the end of the function indicates that the program has completed successfully.

C Program - Find the Size of int, float, double and char

 This C code is a simple program that demonstrates the use of the sizeof operator in C to determine the size of different data types.

#include<stdio.h>

int main()
{

    int int_type;
    float float_type;
    double double_type;
    char char_type;

    printf("Int size: %zu bytes \n", sizeof(int_type));
    printf("Float size: %zu bytes \n", sizeof(float_type));
    printf("Double size: %zu bytes \n", sizeof(double_type));
    printf("Char size: %zu byte \n", sizeof(char_type));

    return 0;
}

Result:

Int size: 4 bytes
Float size: 4 bytes
Double size: 8 bytes
Char size: 1 byte

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

The code starts by including the standard input-output library stdio.h. The program then defines four variables of different data types: int_type, float_type, double_type, and char_type.

 
int int_type;
float float_type;
double double_type;
char char_type;

 

These variables are not assigned any initial value, so they contain undefined values.

Next, the printf function is used to print the size of each data type using the sizeof operator. The sizeof operator returns the size of a given data type in bytes. The %zu format specifier is used to print the size_t value returned by the sizeof operator.

 
printf("Int size: %zu bytes \n", sizeof(int_type));
printf("Float size: %zu bytes \n", sizeof(float_type));
printf("Double size: %zu bytes \n", sizeof(double_type));
printf("Char size: %zu byte \n", sizeof(char_type));

 

The output of the program will show the size of each data type in bytes.

Finally, the main function returns 0, which indicates successful execution of the program.

Overall, this program is a simple demonstration of the use of the sizeof operator in C to determine the size of different data types.

C Program - Print ASCII Value of a Characters inside C Array or String using a For Loop

This C code declares a character array original_chars with some initial values and prints the ASCII values of each character in the array.  

#include <stdio.h>

int main()
{

    char original_chars[] = {'A', 'B', 'C', 'a', 'b', 'c'};
    //char original_chars[] = {"test"};

    size_t number_of_elements = sizeof(original_chars)/sizeof(original_chars[0]);

    for (int x = 0; x < number_of_elements; x++)
    {
        printf("ASCII value for %c is %d: \n", original_chars[x], original_chars[x]);
    }

    return 0;
}

Result: 

ASCII value for A is 65:
ASCII value for B is 66:
ASCII value for C is 67:
ASCII value for a is 97:
ASCII value for b is 98:
ASCII value for c is 99:

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

Here's a detailed explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which provides functions for printing to the console. 

int main()
{

    char original_chars[] = {'A', 'B', 'C', 'a', 'b', 'c'};
    //char original_chars[] = {"test"};

This code declares a character array original_chars with some initial values. The first line is currently commented out and the second line is not being used. If the second line is uncommented, the original_chars array would be initialized with the string "test" instead of the character values. 

    size_t number_of_elements = sizeof(original_chars)/sizeof(original_chars[0]);

This code calculates the number of elements in the original_chars array by dividing the size of the array by the size of a single element in the array. This calculation ensures that the loop iterates through all elements in the array, even if the size of the array changes. 

    for (int x = 0; x < number_of_elements; x++)
    {
        printf("ASCII value for %c is %d: \n", original_chars[x], original_chars[x]);
    }

This code uses a loop to iterate through each element in the original_chars array. It uses the printf() function to print the ASCII value of each character in the array. The %c placeholder in the format string is replaced with the value of original_chars[x], and the %d placeholder is replaced with the integer value of original_chars[x]. This prints the ASCII value of each character in the array to the console. 

    return 0;
}

This line ends the main function and returns 0 to indicate that the program has completed successfully.

C Program - Find ASCII Value of a Character Entered by User

This C code takes a character input from the user and prints its ASCII value.  

#include <stdio.h>

int main()
{

    char original_char;

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

    printf("ASCII value for %c is: %d \n", original_char, original_char);

    return 0;
}

Result:

Enter Character:
a
ASCII value for a is: 97

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

Here's a detailed explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which provides functions for printing to the console. 

int main()
{

    char original_char;

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

This code declares a character variable original_char to store the user input and prints a message asking the user to enter a character. The scanf() function reads a character value entered by the user from the console and assigns it to the original_char variable. The & symbol is used to pass the address of the original_char variable so that its value can be modified by the scanf() function. 

    printf("ASCII value for %c is: %d \n", original_char, original_char);

This code uses the printf() function to print the ASCII value of the character entered by the user. The %c placeholder in the format string is replaced with the value of original_char, and the %d placeholder is replaced with the integer value of original_char. This prints the ASCII value of the character to the console. 

    return 0;
}

This line ends the main function and returns 0 to indicate that the program has completed successfully.

C Program - Nested If in C Programming Example

This C code takes an integer input from the user and determines whether it is positive, negative, or zero.  

#include <stdio.h>

int main()
{

    int num;

    printf("Enter a Number: \n");
    scanf("%d", &num);

    if (num <= 0)
    {
        if (num == 0)
        {
            printf("You Entered 0. \n");
        }
        else
        {
            printf("Number is Negative. \n");
        }
    }
    else
    {
        printf("Positive Number. \n");
    }

    return 0;
}

Example:

Enter a Number:
10
Positive Number.

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

Here's a detailed explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which provides functions for printing to the console. 

int main()
{

    int num;

    printf("Enter a Number: \n");
    scanf("%d", &num);

This code declares an integer variable num to store the user input and prints a message asking the user to enter a number. The scanf() function reads an integer value entered by the user from the console and assigns it to the num variable. The & symbol is used to pass the address of the num variable so that its value can be modified by the scanf() function. 

    if (num <= 0)
    {
        if (num == 0)
        {
            printf("You Entered 0. \n");
        }
        else
        {
            printf("Number is Negative. \n");
        }
    }
    else
    {
        printf("Positive Number. \n");
    }

This code checks the value of num using nested if statements. If the value of num is less than or equal to zero, the first if block is executed. If num is equal to zero, a message stating that the user entered zero is printed to the console. If num is less than zero, a message indicating that the number is negative is printed to the console. If the value of num is greater than zero, the else block is executed and a message stating that the number is positive is printed to the console. 

    return 0;
}

This line ends the main function and returns 0 to indicate that the program has completed successfully.

C Program - Check Whether a Number is Positive or Negative using If Statement

This C code takes an integer input from the user and determines whether it is positive, negative, or zero. 

#include <stdio.h>

int  main()
{
    int num;

    printf("Number to Check: \n");

    scanf("%d", &num);

    if (num > 0)
        printf("%d is a Positive Number \n", num);

    else if (num < 0)
        printf("%d is a Negative number \n", num);

    else
        printf("Number is 0. \n");

    return 0;
}

Result for 10 as input: 

Number to Check:
10
10 is a Positive Number

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

Here's a detailed explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which provides functions for printing to the console. 

int main()
{
    int num;

    printf("Number to Check: \n");

This code declares an integer variable num to store the user input and prints a message asking the user to enter a number. 

    scanf("%d", &num);

This code reads an integer value entered by the user from the console and assigns it to the num variable using the scanf() function. The & symbol is used to pass the address of the num variable so that its value can be modified by the scanf() function. 

    if (num > 0)
        printf("%d is a Positive Number \n", num);

    else if (num < 0)
        printf("%d is a Negative number \n", num);

    else
        printf("Number is 0. \n");

This code checks the value of num using an if-else statement. If the value of num is greater than zero, it is considered a positive number and a message indicating so is printed to the console using the printf() function. If the value of num is less than zero, it is considered a negative number and a message indicating so is printed to the console. If num is equal to zero, a message stating that the number is zero is printed to the console. 

    return 0;
}

This line ends the main function and returns 0 to indicate that the program has completed successfully.

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