Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»C Programming»C Program to Add Two Numbers

    C Program to Add Two Numbers

    By RahulJune 25, 20232 Mins Read

    In this article, we will delve into the practical aspect of programming in C language, particularly focusing on a fundamental operation – addition of two numbers. Although it seems basic, it serves as a cornerstone for understanding larger, more complex programs.

    Adding Two Numbers in C

    Adding two numbers in the C language is a straightforward task and is usually the first step for beginners to understand the syntax and structure of the language.

    Below is the simple program to add two numbers:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #include<stdio.h>
     
    int main() {
        int num1, num2, sum;
        
        printf("Enter first number: ");
        scanf("%d", &num1);
        
        printf("Enter second number: ");
        scanf("%d", &num2);
        
        sum = num1 + num2;
        
        printf("Sum of the entered numbers: %d", sum);
        
        return 0;
    }

    Code Breakdown:

    Let’s break down this code to understand each component:

    1. #include: This is a preprocessor command that includes the standard input-output library functions. The printf and scanf functions used in this program are part of this library.
    2. int main(): This is the main function where the program execution begins. The int keyword before main signifies that the main function will return an integer value.
    3. int num1, num2, sum;: These are integer variable declarations. num1 and num2 are the numbers to be added and sum will hold the result of the addition.
    4. printf(“Enter first number: “);: This function is used to print the string inside the double quotation marks onto the console.
    5. scanf(“%d”, &num1);: The scanf function is used to receive the user’s input. The %d format specifier is used to scan an integer number. The & symbol is the address operator and is used here to provide the memory location for storing the input value.
    6. sum = num1 + num2;: This is the arithmetic operation where the numbers num1 and num2 are added and the result is assigned to the sum variable.
    7. printf(“Sum of the entered numbers: %d”, sum);: This function is used to print the result onto the console. The %d is a placeholder for the integer that will be replaced by the value of the sum variable.
    8. return 0;: This statement signifies the successful termination of the program. It returns the control from the main function back to the operating system.

    Wrap Up

    The ability to add two numbers may seem trivial, but understanding the syntax, variable declaration, user input, and console output in C is fundamental for developing more complex applications. As you progress in your programming journey, these principles will form the foundation of your knowledge and enable you to tackle more complex problems.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    C Program to Reverse a String

    C Program to Reverse a Number

    C Program to Check if a Number is Odd or Even

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Create and Use Custom Python Module
    • How to Install and Use Podman on Ubuntu 22.04 & 20.04
    • Setting Up Laravel with Docker and Docker-compose
    • Setting Up Development Environments with PHP and Docker
    • Using Composer with Different PHP Versions in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.