Select Page

# C Program to Add Two Integers

by | Dec 31, 2022 | C, C Programs

## Introduction

This program can be in two ways. One way is declaring and assigning the values to the variables and then calculating their sum. The other method is to declare the variables first and then the user is asked to enter two integers. Then, the sum operation is applied to both values, and the result will be displayed on the screen.

To understand this program, the learners should know about the following C programming topics:

1. Data Types in C
2. Variables, literals, and constants
3. C Programming operators

## Program

#include<stdio.h>
int main()
{
int a,b;
printf("Enter two values:\n");
scanf("%d",&a);
scanf("%d",&b);
int sum=a+b; // ‘+’ operator is used to add the values
printf("Sum of the values is %d",sum);
return 0;

## Explanation

Firstly, we declare two variables that are a, b that will store the values entered by the user.

int sum=a+b; the sum of ‘a’ and ‘b’ is calculated using the ‘+’ operator and the resultant is assigned to a new variable named ‘sum’ of int data type.

Since, ‘a’ and ‘b’ both are of integer type, thus their sum will also be an integer value. That is the reason why we declare the ‘sum’ variable of the ‘int’ type.