C Program to add two numbers
Objective
Write a C program to add two numbers and display the result.
Algorithm / Approach
- Include the standard I/O library
stdio.h. - Declare three integer variables:
a,b, andc. - Prompt the user to enter two numbers using
printf(). - Read the numbers using
scanf()with the%dformat specifier. - Calculate the sum:
c = a + b. - 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, whilescanf()is used to read formatted input from the keyboard.- The
&(ampersand) symbol before the variable names inscanfis 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)