Sidebar Ad

Categories

Add two numbers in C

In this post ,  i will tell you how to add two numbers in c programming language.

Addition of two numbers in C language

Code :

#include <stdio.h>

int main()
{
int a,b,c;
a=10;
b=5;
c=a+b;
printf("Sum of two numbers : %d",c);
}
add-two-numbers-in-c
Adding two number in c
Understanding The Code:

For adding two integers, we have taken ‘int’ datatype . Then we have declared three variables . We provided values 10 and 5 to variable a and b respectively. Then we have performed addition of variable a and b and calculated its result in variable c. Then printed output using printf code.

Note :

For printing values from int datatype , we have to use %d inside quotation mark.

Adding two numbers by taking input from user in C

Program to add two numbers by taking both number as input from users in run time.

Code:

#include <stdio.h>

int main()
{

int a,b,c;
printf("Enter first number \n");
scanf("%d",&a);
printf("Enter second number \n");
scanf("%d",&b);
c=a+b;
printf("Sum of two numbers : %d",c);
}
addition-of-two-number-by-taking-input-from-user
Adding two numbers by taking input from user

Understanding the code:

Again we have taken three int datatype . Two for taking input from user and one for performing addition of these two inputs. Using scanf function , we have taken input from user and stored it in variables a and b. You have to use &variable name to store int datatype , i.e in this code we used &a and &b to store inputs from user. After that calculation is performed in variable c and then we printed the result as an output using printf function.

Note:

Use &variablename to store input that you taken from user in runtime using scanf function.

Performing other mathematical operation in C:

You can perform any other mathematical operation in c language using the same code which is posted above. Just you need to change the operation sign in third variable.

For example , in this code we used c as third int datatype and the operator is + which adding two integers. If you want to subtract two numbers , change the operator to – , i.e,

c= a-b;

For multiplication , c= a*b;

For Division , c= a/b;

Using this you can perform all the mathematical operation for two numbers that are predefined or two be taken in runtime in c language

I'm Tech Expert, Programmer , Web Developer, Owner Of this site and much more 😁

    Leave Your Comment

    Your email address will not be published.*