C Program To Add Three Numbers

TABLE OF CONTENTS

Explanation of the Code

  1. #include <stdio.h>

    • This line includes the Standard Input Output library which is necessary for using input and output functions like printf and scanf.

  2. int main()

    • This is the main function where the execution of the program begins. It returns an integer value, which is 0 in this case to indicate successful completion.

  3. Variable Declarations:

    • int num1, num2, num3, sum;

      • Here, we declare four integer variables: num1, num2, num3 to store the numbers entered by the user, and sum to store the result of the addition.

  4. Prompt and Input for First Number:

    • printf("Enter the first number: ");
      • This line prints a message asking the user to enter the first number.
    • scanf("%d", &num1);
      • scanf reads an integer from the user and stores it in the variable num1.
  5. Prompt and Input for Second Number:

    • printf("Enter the second number: ");
      • This line prints a message asking the user to enter the second number.
    • scanf("%d", &num2);
      • scanf reads an integer from the user and stores it in the variable num2.
  6. Prompt and Input for Third Number:

    • printf("Enter the third number: ");
      • This line prints a message asking the user to enter the third number.
    • scanf("%d", &num3);
      • scanf reads an integer from the user and stores it in the variable num3.
  7. Calculate the Sum:

    • sum = num1 + num2 + num3;
      • This line calculates the sum of num1, num2, and num3 and stores the result in the variable sum.
  8. Display the Result:

    • printf("The sum of %d, %d, and %d is %d\n", num1, num2, num3, sum);
      • This line prints the result to the screen, displaying the numbers and their sum.
  9. Return Statement:

    • return 0;
      • This returns 0 from the main function, which indicates that the program has executed successfully.

How to Implement and Run This Code

  1. Using an IDE or Compiler:

    • Open your preferred C IDE (like Code::Blocks, Dev-C++, or Visual Studio) or a C compiler (like GCC).

    • Create a new project or file and paste the code into the editor.

    • Save the file with a .c extension, for example, add_numbers.c.

    • Compile and run the program using the build/run commands of your IDE or compiler.

  2. Using Command Line:

    • Save the code in a file named add_numbers.c.

    • Open a terminal or command prompt.

    • Navigate to the directory where the file is saved.

    • Compile the code using a command like gcc add_numbers.c -o add_numbers (if using GCC).

    • Run the compiled program using ./add_numbers (on Unix-like systems) or add_numbers.exe (on Windows).

  3. C code to Add Three Numbers 

  4. Program Code 

  5. You Can You This Code On Any Projects and Works 

 

#include <stdio.h>

int main() {
    // Declare three variables to store the numbers and one variable to store the sum
    int num1, num2, num3, sum;

    // Prompt the user to enter the first number
    printf("Enter the first number: ");
    // Read the first number from the user
    scanf("%d", &num1);

    // Prompt the user to enter the second number
    printf("Enter the second number: ");
    // Read the second number from the user
    scanf("%d", &num2);

    // Prompt the user to enter the third number
    printf("Enter the third number: ");
    // Read the third number from the user
    scanf("%d", &num3);

    // Calculate the sum of the three numbers
    sum = num1 + num2 + num3;

    // Display the result
    printf("The sum of %d, %d, and %d is %d\n", num1, num2, num3, sum);

    // Return 0 to indicate successful completion of the program
    return 0;
}
C

Output

You Can See The Output 

 

Enter the first number: 5
Enter the second number: 10
Enter the third number: 15
The sum of 5, 10, and 15 is 30
C

Thanks For Visiting Our Website

COMPILE CODE: CODE COMPILER

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Resize text
Scroll to Top