c program to add two numbers || easy example addition of 2 number in c

c program to add two numbers

In this example, We will learn how to add two numbers in c language. Here we are going to explain c program to add two numbers in 2 easy to understand examples.

addition of two numbers in c without using scanf

#include<stdio.h>
void main()
{
    int a,b,c;
    a=10;
    b=20;
    c=a+b;
    printf("Addition of a and b = %d",c);
}

Output

Addition of a and b = 30

Explanation

In this program we are going to add two numbers. first of all we have taken 3 variable a, b, and c. Where a and b are used to store two numbers which we are going to add. And after the passing the values to two variables we have added two variable and stored the result in variable c. In last line we have display the result on output window using printf() function.

C program to add two numbers using scanf

#include<stdio.h>
void main()
{
    int a,b,c;
    printf("Enter any number");
    scanf("%d",&a);
    printf("Enter any other number");
    scanf("%d",&b);
    c=a+b;
    printf("Addition of a and b = %d",c);
}

Output

Enter any number
5
Enter any other number
15
Addition of a and b = 20

Explanation

In this program, we have asked the user to enter the values for addition using scanf() function. After that we have added the values and display the result to output window along with values of two variables.

Share in a Single Click

Leave a Reply

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