Find Largest and Smallest Numbers Using Global Variables

CCBeginner
Practice Now

Introduction

In this lab, we will learn how to find the largest and smallest numbers among two input numbers using global declaration in C programming. Unlike local variables, global variables can be accessed and modified by any function within the program. We will use global variables to store the input numbers and solve the problem.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123273{{"`Find Largest and Smallest Numbers Using Global Variables`"}} c/data_types -.-> lab-123273{{"`Find Largest and Smallest Numbers Using Global Variables`"}} c/operators -.-> lab-123273{{"`Find Largest and Smallest Numbers Using Global Variables`"}} c/if_else -.-> lab-123273{{"`Find Largest and Smallest Numbers Using Global Variables`"}} c/user_input -.-> lab-123273{{"`Find Largest and Smallest Numbers Using Global Variables`"}} c/memory_address -.-> lab-123273{{"`Find Largest and Smallest Numbers Using Global Variables`"}} c/function_parameters -.-> lab-123273{{"`Find Largest and Smallest Numbers Using Global Variables`"}} c/function_declaration -.-> lab-123273{{"`Find Largest and Smallest Numbers Using Global Variables`"}} end

Declare global variables

First, we need to declare two global variables a and b outside of the main function. We will store the input numbers in these variables.

#include <stdio.h>
int a, b;

Take input values from the user

In this step, we will take two integer values from the user and store them in the a and b variables using the scanf() function.

int main()
{
    printf("Enter two numbers to find the largest and smallest numbers: ");
    scanf("%d %d", &a, &b);
}

Find the largest and smallest numbers

To find the largest and smallest numbers, we will use an if...else statement to compare the values of a and b. If a is greater than b, then a is the largest and b is the smallest, and vice versa. If a and b are equal, then both are the same.

    if(a > b)
    {
        printf("The largest number is %d\n", a);
        printf("The smallest number is %d\n", b);
    }
    else if(a < b)
    {
        printf("The largest number is %d\n", b);
        printf("The smallest number is %d\n", a);
    }
    else
    {
        printf("Both numbers are equal\n");
    }

Complete the program

Finally, we will add some print statements to display the output messages and return 0 to indicate that the program executed successfully.

#include <stdio.h>
int a, b;

int main()
{
    printf("Enter two numbers to find the largest and smallest numbers: ");
    scanf("%d %d", &a, &b);

    if(a > b)
    {
        printf("The largest number is %d\n", a);
        printf("The smallest number is %d\n", b);
    }
    else if(a < b)
    {
        printf("The largest number is %d\n", b);
        printf("The smallest number is %d\n", a);
    }
    else
    {
        printf("Both numbers are equal\n");
    }
    return 0;
}

Summary

In this lab, we learned how to find the largest and smallest numbers among two input numbers using global declaration in C programming. We declared two global variables a and b to store the input numbers and compared their values to determine the largest and smallest numbers. By practicing with this program, students can learn how to declare global variables and use basic conditional statements in C programming.

Other C Tutorials you may like