C Program to Add Two Integers

Spread the love

In this article, you are going to learn how to add two integers in C program with example. Before going to learn how to write a c program to add two integers you have to know what are integers, operators, variables, and data types.

C Program to Add two integers:

c program to add two integers
c program to add two integers

Here both the variables a,b are initialized with the values 5,6 and the c value is the sum of two variables a and b. The sum value is stored in the variable C i.e. c=a+b. Here plus(+) operator is used for adding two variables.

The c value is displayed on the screen by using the printf function.

Example:

#include<stdio.h>
int main() {
int a, b, c;
a=5,b=6;
c=a+b;
printf(" The value of C is %d",c);
return 0;
}

Output:

The value of C is 11

Values entered by the user:

In this example, the values for a,b are asked to enter by the user and stored in their respective variables.

Example of C program to add two integers:

#include<stdio.h>
int main() {
int a,b,c;
printf("Enter a value");
scanf("%d",&a);
printf("Enter b value");
scanf("%d",&b);
c=a+b;
printf("Sum of a + b= %d",c);
return 0;
}

Output:

Enter a value 15
Enter b value 25
Sum of a + b= 40

C program to add two integers using Function:

Example:

#include<stdio.h>
int sum(int a, int b)
 {
int c;
c=a+b;
return(c);
}
int main()
{
int a,b,result;
printf("Enter two values: ");
scanf("%d %d", &a,&b);
result= sum(a,b);
printf("Addition of two values are : %d",result);
return 0;
}

Output:

Enter two values: 30
50
Addition of two values are : 80

C

Leave a Comment

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

Scroll to Top