Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to add two numbers

C Code Example — Basic Programs

ADVERTISEMENT

C Program to add two numbers

Objective

Write a C program to add two numbers and display the result.

Algorithm / Approach

  1. Include the standard I/O library stdio.h.
  2. Declare three integer variables: a, b, and c.
  3. Prompt the user to enter two numbers using printf().
  4. Read the numbers using scanf() with the %d format specifier.
  5. Calculate the sum: c = a + b.
  6. Print the result using printf().
main.c
#include<stdio.h>
int main( ) {
 int a, b,c;
 printf("Enter Values A and B: ");
 scanf("%d%d",&a,&b);
 c = a+b;
 printf("RESULT : %d\n",c);
 return 0;
}

Expected Output

Enter Values A and B :
12 13
RESULT : 25

Explanation of the Program

  • This is one of the most fundamental programs in C.
  • printf() is used to output text to the console, while scanf() is used to read formatted input from the keyboard.
  • The & (ampersand) symbol before the variable names in scanf is the "address-of" operator. It tells the compiler to store the inputted value directly at the memory address of the variable.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT