Thursday, March 19, 2009

Tutorial: Learn All The Basics Of C++ Language:page 6

SIMPLE PROGRAMMING


I will start with the basic programming, so to create a simple program use the method stated above or just see the following program or download the file attached to clear our confusion.

I am starting with simple addition program:-

Code:

#include
#include
void main()
{
clrscr();
int a,b,c;
a=10;
b=20;
c=a+b;
cout<<"Total=\t"<< c;
getch();
}

In the following program I have taken 3 variables and marked them as in Integers so they could hold Integral Values. Then I have assigned the value a=10 and b =20. U will notice that I had not assigned any value for C because c will hold the additive value of A and B. Then at the last I have printed the value.In the same way u could make pro. Of sub, div, multi and of remainder.
The above program has predefined value but if u want to enter the value while running the program foll. Steps should be taken.

Code:

#include
#include
void main()
{
clrscr();
int a,b,c;
cout<<"Enter the value of a \n";
cin>>a;
cout<<”Enter the value of b\n”;
cin>>b;
c=a+b;
cout<<"Total=\t"<< c;
getch();
}

Here user is entering the values so the total will come as the value is entered.
Remember there should not be any change in the value u have taken .

For eg. U have taken the following codes;
I am starting directly from void main()

Code:

Void main()
{
clrscr();
int a;
cout<<”Enter the value of A”<cin>>A;

In this case program has error as u have taken small a as int and entering the value of A in cin>>. So take same variable u have taken. Hope u understand me.

Program to input character:-

Code:

#include
#include
void main()
{
clrscr();
char name[20];
cout<<”Enter your name or any alphabet\n”;
cin>>name;
cout<<”Your name/ Alphabet”<< name;
getch();
}

Here u will notice that after char name I have taken some value inside [ ]. I have done this because in C++ char has 1 value assigned to it but if u will enter name then C++ will print only the 1’st alphabet of the name/string. To avoid this we can define value to char variable till 128.
The main defects of the program is that it will not read spaces bt. Name, but if u want that computer should read spaces then following program should be written.

Code:

#include
#include
#include
void main()
{
clrscr();
char name[20];
cout<<”Enter your name or any alphabet\n”;
gets(name);
cout<<”Your name/ Alphabet”<< name;
getch();
}

Here I had taken one extra header file and instead of cin>> I had taken gets as I had already stated earlier that cin>> does not read spaces. So gets should be entered. Hope it is clear again I am telling u any doubt ask me I will clear it.
Now some basic programs of taking out area ,simple interest etc..

To calculate the area of square.

Code:

#include
#include
void main()
{
clrscr();
int side,area;
cout<<”Enter the side of the square so as to find the area\n”;
cin>>side;
area=side*side;
cout<<”The area of the square is \t”<< area;
getch();
}

Here user is entering data and hence the area is find. Similarly I am showing u how to find the area but don’t expect much I written 10+ page and I had to complete till looping. So take a quick look.

Code:

#include
#include
void main()
{
clrscr();
int radius;
cout<<”Enter the radius of the circle so as to find the area\n”;
cin>>radius;
area=(22*r*r)/7;
cout<<”The area of the Circle is \t”<< area;
getch();
}

Here user is entering the value and hence area is find. Similarly u could find area of diff. things. If u can recognize the formula.
Now I am telling u the program of swapping of 2 variable by using 3’rd variable .

Code:

#include
#include
void main()
{
clrscr();
//Swapping of 2 variable without using third variable
int a,b,c;
cout<<"Enter the value of variable A\n";
cin>>a;
cout<<"Enter the value of variable B\n";
cin>>b;
cout<<"The Entered value are"<< endl;
cout<<"A="<< a;
cout<<"\t B ="<< b;
c=a;
a=b;
b=c;
//Values after swapping
cout<<"\n The Swapped value are\n";
cout<<"A="<< a;
cout<<"\tB="<< b;
getch();
}

Here with the help of C variable I swapped the value of A and B. Always remember the transfer of value goes to right to left. As a value being transferred to c then a is empty, again b’s value is transferring to a now b is empty. Now again c value is transferred to b. So values are interchanged means swapped.
Similarly U could enter name, integer etc and can calculate diff. things but for now I am tired will post the rest soon

No comments:

Post a Comment