How to add two numbers in C programming language
How to add two numbers in C programming language
In this blog post we will learn that how can we add two numbers, and then assign the result of those two numbers to any variable. Before going to the coding examples we will look at at basics of C programming language that helps us to add two numbers and show their result on the console screen. Before diving deep into the coding examples, let us walk through some key points.
When we start to learn any programming language we usually start with some basic syntax of the language and after that we go through some basic programs like Hello World program, or print a number or simply show the addition of two numbers. For this purpose we normally look at, how can we take an input from the user and after that we perform some operations on the input data using math operations.
Basic syntax and format of C programming language
In C programming language in order to make input / output operations we mainly use two functions which are printf() and scanf(). These are the very basic functions with the help of which we can take input from the users and after that print it on the console screen. So that the users can see the result there.
For printing some message on the console screen we use printf() function. Which refered to as output operation. And when we need to get input from the user we use scanf() or scanf_s() operation or method. And we can use both of these operations in C program if we have included,
#include<stdio.h>
header file that contain definition for these functions (printf() and scanf_s()).
Another key point to keep in mind is that when we write a C program we have to add a method named as main(). This method is essential for our C program to run, because when you run a C program the compiler will look for this main() method, because this main() method is the entry point for the C programming code. The complier will look for this main() method for starting the execution of C program.
Another key point for the C programming language is to declare variables and their data types at the beginning of this main() method, so that we can do some data manipulation according to our C program requirement or logic.
Before jumping into the coding example the last point to mention is that when you want to print the input data from the user in C program, we use,
printf("entered number is %d", VariableName);In this above line of code of C program, we simply print the message "entered number is 6" in case VariableName has value = 6. In the above C program code we have used format specifier (%d). Remember the main use of this specifier is that when we use this printf() method it will simply print the integer value at that specific location like this,
printf("entered number is %d", VariableName);In the above C program coding line the integer value of VariableName will be printed after the = sign. For example if the entered number is 6 in variable it will be printed like thiis,
entered number is 6
on the console screen.
And now in order to take input using scanf_s() function in C program we use it like this,
scanf_s("%d", &VariableName);We use %d to tell the compiler that the input is of type integer. And we use this & operator with VariableName is to access the memory address of this variable and put the integer value in it.
Simple coding example to add two numbers in C programming language
#include<stdio.h> void main() { int number1, number2, result, VariableName; printf("enter first number = "); scanf_s("%d", &number1); printf("enter second number = "); scanf_s("%d", &number2); result = number1 + number2; printf("sum of entered numbers = %d", result); _getch(); }
In the above coding example of C programming language we have executed the C program that will add two numbers and show their result on the console screen. At the very beginning of this C program code we have used
#include<stdio.h>
which helps us to use input and output operations, which are defined in stdio.h library.
After that we have defined the main() method like this
void main()this method is the entry point in our C program code. With in this main() method we have declared three variables of type int like this,
int number1, number2, result;After that we will simply print message on the console screen to enter first number using printf() function. And then we took input from the user using scanf_s() function like this,
printf("enter first number = "); scanf_s("%d", &number1);
As we already discussed about the printf() and scanf() in detail, so they will work in the same way in our C program. Printf() will simply print the message on the console screen and scanf_s() will take user input from the keyboard.
So after taking input from user for number1 and number2 variables we will simply add their sum and assign that value to result variable of type int. And at last we will simply print the sum on the console screen like this,
printf("sum of entered numbers = %d", result);And at last we have used,
_getch();
method to hold or pause the console screen from automatically disappearing after successful execution of our C program code.
Congratulations! like always we have successfully implemented the C program for adding two numbers and show their sum on the console screen.
Comments
Post a Comment