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 Oct 13, 2024 21:06 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.

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
50 hours
– / –
40 hours
– / –
2 months
– / –
4 months
5.5 K
140 hours

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
", 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
Relational Operators in C | About and Types
Learning If Statement in C

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

C programming examples 

Top 80+ C Programming Interview Questions and Answers

Nested If Else Statement in C | About, Syntax, Flowchart and Examples

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