Thursday, 3 April 2014

How to Add Two Numbers in c Language Using Different Methods?

This c language program is used to add two numbers. It uses the basis arithmetic operation for adding two numbers and then prints the sum on output screen. It is as simple as your First C Language Program.

C Language Program to Enter 2 numbers by entering values

#include<stdio.h>
main()
{
   int a, b, c;   //Declaration of variables
   printf("Enter two numbers to add\n");
   scanf("%d%d",&a,&b);

   c = a + b; 
//Addition of variable
printf("Sum of entered numbers = %d\n",c);
}

Below is the output of the above program.





Program to ADD 2 numbers by Declaring values

#include<stdio.h>
main()
{
   int a = 1, b = 2;

   /* Storing result of addition in variable a */

   a = a + b;
   printf("Sum of a and b = %d\n", a);
}






C program to add two numbers repeatedly


#include<stdio.h>

main()
{
   int a, b, c;
   char ch;

   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;
   }

   return 0;
}


Adding numbers in c using function


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



I 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.

How to Write First Program in C Language?

If you have heard or read about the history and syntax of programming in C Language then you might be able to write a first program to print welcome message on output screen.

SYNTAX OF C LANGUAGE

Before you write any c language program, you must learn it syntax i.e. where to add comments, semi column, braces, header files etc. You must try to learn about its syntax vertically as it is describe below:

1. Header Files
2. Main Function
3. Start Curly Brace
4. PrintF Function (if printing on screen is required)
5. Scanf Function (if value entering is required)
6. Close Curly Brace
7. Comments, where is it required to tell anything important

/*This is my first program to print Welcome Message*/
/*Program is written by RapidForceAds*/
#include<stdio.h>
main()
{
       printf(“Welcome to C Language\n”);
       printf(“This is the output of your First Program”);
}


When you compile and run this program then it will show an output console with the output of this program.



Output Screen

What is C Programming ?

In computing, 'C' is a general purpose programming language and initially developed by Dennis Ritchie between the year 1969 and 1973 at AT&T Bell Laboratories. Like most other languages, C has facilities for structured programming and allows lexical variable scope and recursion, while a static type system prevents many unintended operations. Its design provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, most notably system software like the Unix computer operating system.


If you notice carefully, you will see that 'C' is one of the most widely used programming languages of all time and C compilers are available for the majority of available computer architectures and operating systems. Many other programming languages adopt the features from C Language. Current version of C Language is C11 which was approved in December 2011.

 

Have a look on this blog because we are now implementing online study of  C Programming with an easy way to learn and understand the concept behind the use of it.