Addition of Two Numbers in C

Addition of Two Numbers in C

3 mins read1.3K Views Comment
Esha
Esha Gupta
Associate Senior Executive
Updated on Apr 29, 2024 15:37 IST

Delve into C programming with a foundational task: adding two numbers. This guide walks you through each step, from declaring variables to getting the output, making it a perfect starting point for beginners. Let's understand more!

2023_08_What-is-13.jpg

Diving into the World of C Programming: It’s Easier Than You Think! If you’ve landed on this blog post about addition of two numbers in C, you’re already on the right track to diving into the world of programming. And guess what? Starting with the C programming language is much simpler than most people believe. See it yourself while you go through the examples in this blog.

Explore Online C Programming Courses

Addition of Two Numbers in C

Let’s find the sum of two values stored in variables num1 and num2:


 
#include <stdio.h>
int main()
{
// Declare variables
int num1,num2,sum;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
// calculate the sum of two numbers
sum = num1 + num2;
// Print the result
printf("%d + %d = %d", num1, num2, sum);
return 0;
}
Copy code

Output :

Enter two integers: 10 20
10 + 20 = 30
 

Analogy Based Example: Addition of Two Numbers in C

Problem Statement: Online Shopping Cart Item Counter

Background: These days, with online shopping, folks fill their carts on websites. They like to see how many items they’ve chosen. It helps them decide what to get, see how much they’re buying, and check they didn’t forget or double-add anything.

Objective: Develop a program that simulates adding items to an online shopping cart. The program should:

  1. Represent individual items as variables.
  2. Add the items together to get a total count.
  3. Display the total number of items in the cart to the user.

Constraints:

  1. For simplicity, count each item as one unit regardless of its type (e.g., shoes, shirt).
  2. Write the program in C programming language.
  3. The output should clearly indicate the total number of items in the cart.

Expected Outcome: Upon execution, the program should provide a clear and concise output indicating the total number of items in the online shopping cart.


 
#include <stdio.h>
int main() {
// Imagine you're shopping online.
// You add a pair of shoes to your cart.
int shoes = 1;
// Then, you add a shirt to your cart.
int shirt = 1;
// Now, let's see how many items you have in total in your cart.
int totalItems = shoes + shirt;
// Print the total number of items in the cart.
printf("Total items in the cart: %d\n", totalItems);
return 0;
}
Copy code

Output :

Total items in the cart: 2

When you run this code, it will output: Total items in the cart: 2

In this example, the variables shoes and shirt represent the individual items you added to the cart. The addition operation (shoes + shirt) calculates the total number of items, and the result is printed out using the printf function.

Control Statements in C | Meaning and Types
Control Statements in C | Meaning and Types
Control statements in C are used to determine the order in which the instructions within a program are executed. They are of three types: Selection, Iteration & Jump statements. Think...read more
Relational Operators in C | About and Types
Relational Operators in C | About and Types
Relational operators in C, such as “less than”, “greater than”, “less than or equal to”, “greater than or equal to”, “equal to”, and “not equal to”, play a pivotal role...read more
Learning If Statement in C
Learning If Statement in C
The if statement in C is a control structure that evaluates a condition. If the condition is true, it runs the associated code; if false, the code is ignored. It’s...read more

Crack the code of C/C++ with our comprehensive guide; find details on top colleges, programmes and online courses.

Conclusion 

Embarking on the journey of C programming is akin to unlocking a treasure chest of knowledge. The initial steps, like adding two numbers, are just the tip of the iceberg. As you delve deeper, you’ll discover a world rich with logic, creativity, and endless possibilities. And remember, every expert was once a beginner. The key is to keep the curiosity alive and continue exploring.

Learn Data Types in C Programming With Examples
Learn Data Types in C Programming With Examples
Data types are the type of data stored in a C program. Data types are used while defining a variable or functions in C. It’s important for the compiler to...read more

C programming examples 
C programming examples 
If you want to learn C language by executing programming examples. Then this article offers you 17C programming examples with outputs, so that you can learn fast.

Top 80+ C Programming Interview Questions and Answers
Top 80+ C Programming Interview Questions and Answers
Here’s a list of 80+ top frequently asked C programming interview questions and answers to help you crack your next interview. Preparing for a C programming interview? Here are the...read more

Nested If Else Statement in C | About, Syntax, Flowchart and Examples
Nested If Else Statement in C | About, Syntax, Flowchart and Examples
Have you ever wondered how programs make decisions and adapt to different situations? The answer lies in the power of Nested If else statements. These statements allow us to control...read more

What’s Next?

Having understood basic operations, it’s time to build upon that foundation. Here’s a suggested roadmap:

  1. Data Types and Variables: Before diving into complex operations, it’s essential to understand the different types of data you can work with in C and how to store them.
  2. Basic Input/Output: Learn how to interact with users by taking input and displaying results. This will make your programs more interactive and user-friendly.
  3. Operators in C: Delve into arithmetic, relational, and logical operators. This will empower you to perform a variety of operations on your data.
  4. Control Structures: Once you’re comfortable with the basics, you can start exploring decision-making in programming, leading you to more dynamic code.

Boost Your Learning:

If you’re serious about mastering C, consider enrolling in an online course. Here are some top recommendations:

  1. Coursera’s “C for Everyone” Series: A comprehensive introduction to C, suitable for both beginners and those looking to refresh their knowledge.
  2. Udemy’s “C Programming For Beginners”: This course takes a hands-on approach, ensuring you get practical experience as you learn.

By investing time in a structured course, you’ll gain a more profound understanding, benefit from expert insights, and have a clear path of progression.

So, gear up and dive deeper into the world of C programming. The journey you’ve embarked on is filled with challenges, but the rewards, in terms of knowledge and skills, are immeasurable. Happy coding!

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio