Coding With Yash

Sum of two numbers in c programming language

This is module no. 1 of our series ” Programming in c”. Today in this module, we will write a program to find the sum of two numbers in C language. In the first half, we will try to understand the syntax using the given values, and then in the second half, we will take user-defined values to find the sum of two numbers using scanf function.

Sum Of Two Numbers In C Without Using Scanf

First, we will see how we can find the sum of two numbers in c without using scanf function. Here we will use given values so that we don’t have to ask the user to enter values. In the below code, we have defined two variables n1 and n2, and then assigned some raw values to them as shown in line 7.

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

void main() {

clrscr();
int n1=10, n2=20, sum;

sum = n1 + n2;

printf("The sum of two numbers is %d", sum);
}

Sum Of Two Numbers In C Using the Scanf function

As discussed, in the first half we assigned some raw values to the variables, but now in the second half, we will not assign any values to the variables in the starting. Instead, we will ask the user to enter the values of his/her choice and then assign those entered values to the variables using printf and scanf functions.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2;
int sum;

printf("enter two numbers\n");
scanf("%d %d", &num1, &num2);

sum = num1 + num2;
printf("the sum of two numbers is %d", sum);

getch();
}

In the above code take a closer look at line number 9 and line number 10, you will get to know in line number 9 we first displayed a message to the user informing him to enter the values using printf function, and then in line number 10 we took those values using scanf function and assigned them in the variables num1 and num2 respectively.

Turbo C++ Program Window

sum-of-two-number-in-c-programming

The output of the sum of two numbers in c

After writing the complete code of the program.  You need to follow these simple steps SAVE > COMPILE > RUN. If you follow along the exact code then your program will get successfully compiled and run but if by chance you get any error message while compiling in the error window then first resolve the error and then save your program again and then run it.

On successful compilation and running of code, this is what the output will look like on the output window.

If you don’t have turbo c++ on your laptop or on your desktop then you can Download Turbo c++ from here.

Turbo C++ Output Window

sum of two number output

Conclusion:

If you have any questions about the above code, please let us know in the comment section. Also, if you want us to solve any program you have, you can share that with us in the comment section. And if you find this programming solution useful then feel free to share it with your friend using the below social media shareable links, We will be more than happy with your help.

Also, check out the entire list of our C Programming solutions for more knowledge.

Thank you for reading so far.

Happy coding 🙂

Recent Posts