C Program for beginners
keywords: c program for beginners, c program to find the sum of two numbers, basic c program, c program, c++ program, c program to add, add noumbes in c program,easy c program, c program, first lesson in c program, learn c proamming in 1 hour, x byte lab.xbyte,xbytelab,ashiq dey, author ashiq, author dey, easy way write c program to add no., easy c program, how to write a c program, to add two numbers, to find the sum of two no.
C Program to find the sum of two numbers
|
A C/C++ program to add two numbers can be achieved through many ways, here we showed only 3 of them. Before heeding ahead you should know the basic keywords, variables, codding structure etc. If you are new to C Language you can know about them at this link. Still to make the learning process easy to understand, we explained about the program right below the code. To know how to write the main structure of a C program visit this link.
But now you might be wondering that where to write the C program, Don’t worry visit This link and download Dev C++, as it is 23 years old and works perfectly and looks quite modern and is based on GUI.
To know how to save, create new project Visit this link otherwise skip this.
If you are new to C language then without wasting time Visit this link.
Method 1
|
1
|
#include<stdio.h>
main ()
{
int x,y;
printf("Enter two no.: \n");
scanf("%d%d",&x,&y);
printf("\n %d + %d = %d",x,y,x+y);
return 0;
}
|
2
| |
3
| |
4
| |
5
| |
6
| |
7
| |
8
| |
9
|
Explaination:
1. #include<stdio.h> is used to tell the compiler to include and link standard input output functions in the prograam from the library before compilation.
2. int is used to say the compiler that the data type is an integer.
3. Notice the spaces before int, these spaces are ignored by the compiler and are used for giving a good look and makes the code easy to understand when the program is long and complex.
At the end of each line we have entered ‘;’ which is similar to a full stop in a sentence
3. x,y are variable, i.e. its value will depend on user’s inputs.
4. printf("Enter two no.: \n");
printf is a keyword, and it is used to display the contents which lies between “-----”.
\n stands for new line, when the compiler encounters a \n it will start a new line, You can ignore \n in this program
5. scanf("%d%d",&x,&y);
scanf is also a keyword, it is used to read the input of user
Here the first variable is ‘x’ so when the user types a number and press the Enter, then scanf will read it and store it as a value of x.
"%d%d" is use to read the input and %d will accept only integer values
,&x,&y is use to assign the value of what the user inputs, for example 5,6
NOTE: %d is used for only integers
%c is used for character
%s is used for string (array)
To know format other Data Type Visit this link.
6. printf("\n %d + %d = %d",x,y,x+y);
This line will print the result.
Here %d =x and x= ‘what the user inputs’ %d =y and y= ‘what the user inputs’ %d =x+y
7. return 0; is used at the end of the program to return a value 0 when the program execution is finshed
In place of return 0; we can use getch (); also.
Output of the above program will be shown as below screenshot
|
tags: c program for beginners, c program to find the sum of two numbers, basic c program, c program, c++ program, c program to add, add noumbes in c program,easy c program, c program, first lesson in c program, learn c proamming in 1 hour, x byte lab.xbyte,xbytelab,ashiq dey, author ashiq, author dey, easy way write c program to add no., easy c program, how to write a c program, to add two numbers, to find the sum of two no.
Method 2
|
1
|
#include<stdio.h>
main ()
{
int x,y,sum;
printf("Enter 1st no.: ");
scanf("%d",&x);
printf(" Enter 2nd no.: ");
scanf("%d",&y);
sum = x + y;
//sum=x+y;
printf("\n sum = %d",sum);
return 0;
}
|
2
| |
3
| |
4
| |
5
| |
6
| |
7
| |
8
| |
9
| |
10
| |
11
| |
12
| |
13
|
Explaination:
1. sum is a variable, i.e. its value will depend on x and y.
Instead of sum you can take s, add, su, plus or anything you want except numbers, symbols, keywords and space
2. printf("Enter 1st no.: ");
scanf("%d",&x);
printf("Enter 2nd no.: ");
scanf("%d",&y);
Here we have used two printf and two scanf because we want to take the values of x and y separately.
Notice that here we have not use \n, because when we will enter a number a press the Enter button scanf will read the value of x and the nextprintf will appear in a new line
3. sum = x + y;
This is the formula to add numbers.
While writing formula make sure that the variable and symbols are used correctly
For e.g. product=x*y is correct and if we write product=xy then compiler is not going to do, so importance should be given to symbols also
4. //sum=x+y;
It is a comment statement, it will not be compiled by the compiler, it is used for bookmarking a location to make it easily understandable and makes easy to find something that the user is searching for.
Notice that the formula in line 9 and 10 is same except the space, you can use anyone of the above
Single line comment starts with “//This is my first program”
And multiline comment is written between “/**/” e.g.- /* This is my first program */
5. printf("\n sum of the two number= %d",sum);
This line will print the result.
Here %d = sum and sum = x+y, which we have written above the printf statement.
Output of the above program will be shown as below screenshot
|
|
|
tags: c program for beginners, c program to find the sum of two numbers, basic c program, c program, c++ program, c program to add, add noumbes in c program,easy c program, c program, first lesson in c program, learn c proamming in 1 hour, x byte lab.xbyte,xbytelab,ashiq dey, author ashiq, author dey, easy way write c program to add no., easy c program, how to write a c program, to add two numbers, to find the sum of two no.
Method 3
|
C progam to add 3 numbers
1
|
#include<stdio.h>
main ()
{
int x,y,z,sum;
printf("Enter 1st no.: ");
scanf("%d",&x);
printf(" Enter 2nd no.: ");
scanf("%d",&y);
printf(" Enter 3rd no.: ");
scanf("%d",&z);
//formula to add three numbers
sum = x + y + z;
printf("\n sum = %d",sum);
return 0;
}
|
2
| |
3
| |
4
| |
5
| |
6
| |
7
| |
8
| |
9
| |
10
| |
11
| |
12
| |
13
| |
14
| |
15
|
Explaination:
1. printf(" Enter 3rd no.: ");
scanf("%d",&z);
We can use as many printf and scanf statement as we want.
2. To add numbers upto n (as many times the user wants) we have to use loop for that purpose.
Output of the above program will be shown as below screenshot
|
|
tags: c program for beginners, c program to find the sum of two numbers, basic c program, c program, c++ program, c program to add, add noumbes in c program,easy c program, c program, first lesson in c program, learn c proamming in 1 hour, x byte lab.xbyte,xbytelab,ashiq dey, author ashiq, author dey, easy way write c program to add no., easy c program, how to write a c program, to add two numbers, to find the sum of two no.
Best way of teaching I have ever found on the web.
ReplyDeletethanks Code X lab for being so friendly
Thanks Ashiq for your word, really we want to serve this planet the best way we can.
Deletec language program codes for program writers
ReplyDeleteleap year testing c code example