Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Translate it in your own Language

Print this Job Post

Print Friendly and PDF

Thursday, May 1, 2014

C program to add two numbers

C program to add two numbers: This c language program perform the basic arithmetic operation of addition on two numbers and then prints the sum on the screen. For example if the user entered two numbers as 5, 6 then 11 (5 + 6) will be printed on the screen.

C Programming Code

#include<stdio.h>
#include<conio.h>
void main()
{
   int a, b, c;
   printf("Enter two numbers to add\n");
   scanf("%d%d",&a,&b);
   c = a + b;
   printf("Sum of entered numbers = %d\n",c);
   getch();
}

Addition without using third variable

#include<stdio.h>
#include<conio.h>
void main()
{
   int a = 1, b = 2;
   /* Storing result of addition in variable a */
    a = a + b;
  /* Not recommended because original value of a is lost 
   * and you may be using it somewhere in code considering it
   * as it was entered by the user.
   */
   printf("Sum of a and b = %d\n", a);
   getch();
}

C program to add two numbers repeatedly

#include<stdio.h>
#include<conio.h>
void main()
{
   int a, b, c;
   char ch;
   clrscr();
    while(1)
   {
      printf("Enter values of a and b\n");
      scanf("%d%d",&a,&b);
      c = a + b;
      printf("a + b = %d\n", c);
      printf("Do you wish to add more numbers(y/n)\n");
      scanf(" %c",&ch);
      if ( ch == 'y' || ch == 'Y' )
         continue;
      else
     break;
   }
  getch();
}

Adding numbers in c using function

#include<stdio.h>
#include<conio.h>
long addition(long, long);
void main()
{
   long first, second, sum;
   scanf("%ld%ld", &first, &second);
   sum = addition(first, second);
   printf("%ld\n", sum);
   getch();
}
 long addition(long a, long b)
{
   long result;
   result = a + b;
   return result;
}

We have used long data type as it can handle large numbers, if you want to add still larger numbers which doesn't fit in long range then use array, string or other data structure. 


No comments:

Post a Comment

Copyright @ CrackMNC 2014-2024
Divas Nikhra Theme by Crack MNC