Addition of numbers in C-Language

 Addition of numbers in C-Language:


Q:- How to add two constant numbers?

Code


Answer:

  1. #include <iostream>
  2. int main() {
  3.        const int a=5;
  4.        const int b=2;
  5.        int res;
  6.        res=a+b;
  7.        printf("Answer is %d",res); 
  8.        return 0;
  9. }
Code Explanation:
 First line we include preprocessing directory.
 Second line we use main() function.
 Third and fourth line we define a constant variable which can not be change.
 Fifth line we create an 'res' variable with integer data type.
 Sixth line we use 'res' variable to store the sum of 'a' and 'b'.
 Seventh line we print the 'res' variable.
 Eight line we end the program with return value '0'.
 Ninth line we close the curly braces which we open in second line. 
output:
Result



Note: If you use constant you can not change your constant values.



Q:- How to add two numbers by taking input from user?.



addition by user inputAnswer:

  1. #include <iostream> 
  2. int main() {
  3.        int a,b,res;
  4.        printf("Enter First Number:");
  5.        scanf("%d",&a);
  6.        printf("Enter Second Number:");
  7.        scanf("%d",&b);
  8.        res=a+b;
  9.        printf("Sum of two numbers is %d",res);
  10.        return 0;
  11. }
Code Explanation:
 
First line we include preprocessing directory.
Second line we use main() function.
Third  line we define three variable in the integer data type.
Fourth line we ask from user to enter first number.
Fifth line we get  1st number from the user.
Sixth line we ask from user to enter Second number.
Seventh line we get  2nd number from the user.
Eighth line we use 'res' variable to store the sum of 'a' and 'b'.
Ninth line we print the 'res' variable.
Ten line we end the program with return value '0'.
Eleven line we close the curly braces which we open in second line. 

Output:

Output


This is the code for adding two numbers .You can also use this code for additon of more than two number only for few changes.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.