Time for a programming tutorial continuation! Last time you've learned to write "Hello world!" in your console and now I will teach you how to write a program which can add,devide or do similar mathematical operations.
First thing you have to do is open your Dev C++ and do everything like last time until you get a code looking like this:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT SUCCESS;
}
Now like last time fill it out like this:
#include <cstdlib>
#include <iostream>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
using namespace std;
int main(int argc, char *argv[])
{
int x,y,result;
printf("Enter your first number: ");
scanf("%d",&x);
printf("\nEnter your second number: ");
scanf("%d",&y);
result=x+y;
printf("\nx+y=%d\n",result);
system("PAUSE");
return EXIT_SUCCESS;
}
Now you are probably wondering what the heck did I do here.Don't you worry,it's really easy to understand!Lets start:
int x,y,result; -> Here we initialize the variables we will use in our program. int is a variable type called integer.Its a whole number and it can have values between -2,147,438,648 and 2,147,483,647.
int x Declares that we will use symbol 'x' as an integer variable.
int x Declares that we will use symbol 'x' as an integer variable.
So by that logic,the first row initializes symbols 'x,y,result' as variables that can have values between -2,147,438,648 and 2,147,483,647.
I have already explained the printf functio but ill remind you in short.It writes out(to your program console) what you write between " ".
scanf("%d",&x); -> This function scans what you write in your program console so that you can use it later.It can scan things like characters,strings or in this case integers.The %d means that what you write will be saved as an integer(which is what we need in this program).Before each variable scanned you have to put a coma( ' , ' ) and adress ( ' & ' ).So if you are scanning multiple variables with this function you have to devide them by commas and put an adress symbol in front of each(like this scanf("%d %d",&x,&y); ).You may wonder what an adress is so I'll explain.Adress is the position of declared variable in your computer memory.Your computer has a memory(obviously) and everything stored on it has a specific location.For example if you have 20GB of memory that will be 20*2^30 Bytes of memory.Each Byte has its own location (ranging between 1 and 20*2^30).So what we are doing with this function is writing a number to a memory location occupyed by variable x or in other words x=the number scanned.
result=x+y; -> This almost doesn't need an explanation.It's a simple algerbic command which sets the result of adding x and y ( x+y ) as value of variable "result".What might confuse you is the name of the variable so I'll say right now that instead of variable "result" we could have declared variable "z" for example and write command such as "z=x+y;" which would do the same operation.
printf("x+y=%d",result); -> Now you know what printf does but this is a bit different so I'll explain in it with another function: printf("%d",result); -> What this does is write the value of variable "result" in your program console.Just like variable can be scanned it can also be written back to console."%d" as I have explained declares that the value will be in an integer form.So if you write %d anywhere between printf apostrophes the function will expect to write out a value used in program,in this case "result".So the whole function writes out "x+y= >> value of the variable "result << ".Oh you might also wonder what does "\n" mean.It simply means "next row".So whatever you were going to write out will be written in next row.
So what does the program do?It asks you to write in 2 numbers,adds them and writes out the result.Simple!
That's all for today,see you 'till next time!
So what does the program do?It asks you to write in 2 numbers,adds them and writes out the result.Simple!
That's all for today,see you 'till next time!