Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Thursday, June 4, 2020

Program to add two number in C



C Program to add two numbers:

By Ahmad Irshad | Programming Exercises and Solutions > Addition of two numbers

Write a C Program to add two numbers by taking input from user. Have a look at the below example and step by step logic to add two numbers.

Before writing any program you just imagine about output screen first. Like what input to be given, and output to be displayed. Let's have a look at the below example.

  1. INPUT

  2. Enter first number: 10
  3. Enter second number: 20
  OUTPUT

Sum of 10 and 20 = 30

Required knowledge for this exercise is

Logic to add two numbers:

1) Print a massage to the user like "Enter two numbers:" using printf();
2) Input number from user and store it in a variables num1 & num2 using scanf();
3) Calculate sum of two numbers using farmula sum = num1 + num2;
4) At last print the value of sum. that's it! 

Program to find sum of two numbers input givien by a user : 

  1. /** * C program to find sum of two numbers */ #include <stdio.h> int main() { int num1, num2, sum; /* * Read two numbers from user */ printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number:"); scanf("%d", &num2); /* Adding both number is simple and fundamental */ sum = num1 + num2; /* Prints the sum of two numbers */ printf("Sum of %d and %d = %d", num1, num2, sum); return 0; }

Output:

  1. Enter first number: 10
  2. Enter second number: 20
  3. sum of 10 and 20 is = 30

You can take input separately for each number by using scanf(). 

For Example:

  1. /* Read first number from user */ printf("Enter first number: "); scanf("%d", &num1);
  2. /* Read second number from user */ printf("Enter second number:"); scanf("%d", &num2);

OR

Above statements can also write like this:

  1. /* Read two numbers from user */ printf("Enter first number: "); scanf("%d%d", &num1, &num2);


Next topic >> to be updated soon.

Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us