C Program to add two numbers

C program to add two numbers

If you are a beginner in c programming and want to learn how to make a c program to add two numbers. We have come up with the easiest method to solve your problem.

There are many methods to do the task but we have come up with the easiest ones.

In the program, we need two numeric operands and need the arithmetic operator. to add the two numbers.

1. First Method – C program to add two numbers

In the first method, we will declare the two variables in the main section. If you do not know about the main section in c programming, check Main Section in c programming.

We will store the addition of two variables in the third variable and will print the output.

Sample code –

[cpp]

C Program to add two numbers

#include<stdio.h> /* Link section */
#include<conio.h>

void main() /*main section */
{
int a =10; /* First variable ‘a’ has a value of 10 */
int b =20; /* Second variable ‘b’ has a value of 20 */
int c; /* Variable to store the second variable */

c = a+b; /* Addition of two numbers */

printf(“The addition of two numbers = “+c);

}
[/cpp]

OUTPUT – The addition of two numbers = 30

In this example we have used the data of integer type, you can use float an double data type too.

Exercise NO -1  Now go to your Integrated Development Environment (IDE) and make a program using other types of data.

2. Second Method –

In this method, we will declare two variables and will take the values from the user using the Data Input Function.

For taking the value from a user through a keyboard, we will use scanf(). Scanf() function is included in the header files that are in the link section of the program.

Sample Code –

[cpp]
Program to add two numbers taking input from the user

#include<stdio.h>
#inlcude<conio.h>

void main()
{
int a, b, c; Declaration of three variables.
printf(“Enter the vakue of a and b “);
scanf(“%d”,&a);
scanf(“%d”,b);
c = a+b;

printf(“The addition of a and b =” + c);

}
[/cpp]

Output – Enter the value of a and b 20 56
The addition of a and b = 76
In this program, we have taken only integer data from the user, you can use float and double data type also.

Exercise No -2 Now, create a program using float or double but do an addition of three variables in this program. This will increase your understanding of using variables.

3. The third method – Using Function

In this program, We will create a function and If you do not know functions, read it.

Basically a function is a set of instructions which performs a specific type of task, and we can call a function as many time we need.

Sample Code –

[cpp]
<pre>#include<stdio.h>
#include<conio.h>
int add(a, b);
void main()
{
int a, b, c; Declaration of three variables.
printf(“Enter the value of a and b “);
scanf(“%d”,&a);
scanf(“%d”,&b);
c = add(a,b);

printf(“The addition of a and b =” + c);

}
int add(a, b)
{
int d;
d= a+b;
return d;
}
[/cpp]

We made a function named add which takes two variables and returns the addition of both variables. Implement the c program to add two numbers using the function.

Exercise no – 3 Implement the c program to subtract two numbers using the function on your local IDE and tell us how to like the concept.

Recommended Posts –

  1. Introduction to C language
  2. Structure of C Program
  3. Difference between C and C++

 

Leave a Reply

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

Close Bitnami banner
Bitnami