- Related Questions & Answers
- Selected Reading
C++ProgrammingServer Side Programming
Complex numbers are numbers that are expressed as a+bi where i is an imaginary number and a and b are real numbers. Some examples on complex numbers are −
A program to add complex numbers by passing structure to a function is given as follows −
In the last program C program to add two integer numbers, we discussed how to take input and find the sum of two integer numbers?In this program we are doing the same but using a user defined function, this program will take two integer numbers are calculate the sum/addition of them using a user defined function. The function addition is used to calculate addition of the numbers, in this. In the below program to add two numbers, the user is first asked to enter two numbers and the input is scanned using the scanf function and stored in the variables and.Then, the variables and are added using the arithmetic operator and the result is stored in the. C Program to Add Two Numbers In this program, user is asked to enter two integers. Then, the sum of those two integers is stored in a variable and displayed on the screen. C Program to Perform Addition Subtraction Multiplication Division Write a C program to add, subtract, multiply and divide two numbers. C program to perform arithmetic operations. Jul 10, 2019 Program to add two fractions Here we will discuss how to add two fractions using C programming language. To find the sum of two fractions we will be using the concept of LCM and GCD. For example: we have to find the sum of 7/2 and 8/3 Firstly the LCM of 2 and 3 will.
Example
The output of the above program is as follows −
In the above program, the structure complexNumber contains the real and imaginary parts of the complex number. This is given below −
The function addCN() takes two arguments of type complexNumber type and adds the real and imaginary parts of the two numbers. Then the added value is returned to the main() function. This is given below −
In the main() function, the values of the numbers are obtained from the user. This is given below −
The sum of the two numbers is obtained by the calling the addCN() function. Then the sum isprinted. This is given below −
The addition of two numbers in C language is performing the arithmetic operation of adding them and printing their sum on the screen. For example, if the input is 5, 6, the output will be 11.
Addition program in C
#include <stdio.h>int main()
{
int x, y, z;
{
int x, y, z;
printf('Enter two numbers to addn');
scanf('%d%d',&x,&y);
scanf('%d%d',&x,&y);
z = x + y;
printf('Sum of the numbers = %dn', z);
return0;
}
}
Output of the program:
Download Add numbers program executable.
Add Two Numbers In Dev C++
In the expression (z = x + y), overflow may occur if the sum is greater than the maximum value that the variable z can store.
Similarly, we can write a program that performs subtraction, multiplication, and division of two numbers.
Addition without using third variable
#include<stdio.h>int main()
{
int a =1, b =2;
/* Storing result of the addition in variable a */
a = a + b;
printf('Sum of a and b = %dn', a);
return0;
}
{
int a =1, b =2;
/* Storing result of the addition in variable a */
a = a + b;
printf('Sum of a and b = %dn', a);
return0;
}
Doing this isn't recommended because the original value of the variable 'a' is lost; if we require it further in the program, then we will not have it.
C program to add two numbers repeatedly
#include <stdio.h>int main()
{
int a, b, c;
char ch;
{
int a, b, c;
char ch;
while(1){
printf('Input two integersn');
scanf('%d%d',&a,&b);
getchar();
printf('Input two integersn');
scanf('%d%d',&a,&b);
getchar();
c = a + b;
printf('(%d) + (%d) = (%d)n', a, b, c);
printf('Do you wish to add more numbers (y/n)n');
scanf('%c',&ch);
scanf('%c',&ch);
if(ch 'y'|| ch 'Y')
continue;
else
break;
}
continue;
else
break;
}
return0;
}
}
Output of program:
Input two integers
26
(2)+(6)=(8)
Do you wish to add more numbers (y/n)
y
Input two integers
2-6
(2)+(-6)=(-4)
Do you wish to add more numbers (y/n)
y
Input two integers
-53
(-5)+(3)=(-2)
Do you wish to add more numbers (y/n)
y
Input two integers
-5-6
(-5)+(-6)=(-11)
Do you wish to add more numbers (y/n)
n
26
(2)+(6)=(8)
Do you wish to add more numbers (y/n)
y
Input two integers
2-6
(2)+(-6)=(-4)
Do you wish to add more numbers (y/n)
y
Input two integers
-53
(-5)+(3)=(-2)
Do you wish to add more numbers (y/n)
y
Input two integers
-5-6
(-5)+(-6)=(-11)
Do you wish to add more numbers (y/n)
n
C program for addition of two numbers using a function
We can calculate sum of two integers using a function.
#include<stdio.h>
long addition(long,long);
int main()
{
long first, second, sum;
scanf('%ld%ld',&first,&second);
sum = addition(first, second);
printf('%ldn', sum);
return0;
}
long addition(long a,long b)
{
long result;
result = a + b;
return result;
}
long addition(long,long);
int main()
{
long first, second, sum;
scanf('%ld%ld',&first,&second);
sum = addition(first, second);
printf('%ldn', sum);
return0;
}
long addition(long a,long b)
{
long result;
result = a + b;
return result;
}
We are using long data type as it can handle large numbers.
To add numbers that don't fit in in-built data types, use an array, a string, or other suitable data structure.