PDA

View Full Version : Tutorial: Learn All The Basics Of C++ Language


Dark Star
17-03-2007, 08:57 AM
C++ LEARNING GUIDE

This is a basic guide which I have come up with for the people who are looking to go for C++. I have tried to keep it simple so that everyone can understand it.
I had been working on this for last couple of weeks. Any comments/suggestions are welcome.

NOTE: This is a beginners guide so don’t expect too much, I made this guide . Keeping Mind that learner is a newbie in C++. Hey now don’t think I am an expert. Sorry for the mistakes I have tried to cover every aspect.

INTRODUCTION

C++ is an object oriented programming language. It was developed by Dr.Bjarne Stroustrup in the year 1983 at AT&T Bell laboratories, New Jersey {U.S.A}. It was originally named as C with classes. The name is derived with the increment operation in "C" which is ‘++’. So it is the incremented version of C.
An object oriented programming language means collection of objects which are self contain collection of both data structure and functions that interact with other objects. It adds classes, inheritance, functions overloading and operator overloading.

“With the help of C++ we can develop editors, compilers, communication systems, database and other real life time applications.

Note: - C++ codes are case sensitive so every command/codes written should be in Small Letters

CHARACHTER SETS

There are 2 types of character sets:-
• Source Character :- The source text is created with the help of source character such as :

Alphabet: - A-Z, a-z and _.
• Digits: - 0-9.
• Special Characters: - +, -, *, /, ^, ~, %, =, !, &, |, ( ), [ ], ?, “ “, ; , : , \, #, .


ESCAPE SEQUENCE

The characters are interpreted at run time .These escape sequence character are represented by a back slash ‘/’ followed by a character.
For e.g.:- \n, \t, \a etc.

KEY WORDS OR RESERVED WORDS

These words are reserved to do specific task and must not be used as a normal identifier name.
For e.g.:- if, for, else, do, while etc.


IDENTIFIER

Identifier are the fundamental building blocks of a program and are used to gives names to variable, function, arrays, objects, classes etc.

CONSTANT[/COLOR]

These are the items which cannot be changed during the program execution. Every constant used in a program as a type that is determined by its form and a value .

They are mainly of 3 types:-
• Numeric Constant
• String Constant /Literals
• Character Constant
• Floating Constant

INTEGER CONSTANT

One whole number without any factorial part. The integer constant may be Decimal (base 10), Octal (base 8), and Hexadecimal (base 16).

CHRACTER CONSTANT

A character constant in C++ can be any of the valid in ASCII character and is enclosed in single quotes (‘‘)
For e.g.:- char grade=’A’

General Character can be represented by above method but for special character like tab, backspace etc. C++ provides escape sequence as already told by me. It is represented by backslash (\) followed by one or more character.
Escape sequence non graphic characters:-

\a = Audible Bell
\b = Backspace
\f = Formfeeds
\n = New line (\n is same as endl \n is used inside quotes but endl doesn’t)
\r = Carriage Return
\t = Horizontal Tab
\v = Vertical Tab
\\ = Backslash
\’ = Single Quote
\”= Double Quote
\? = Question Mark
\On = Octal number
\xHN = Hexadecimal Number

NOTE: -- These Escape sequence should always be written inside ““double quotes.
As “\n”.

FLOATING CONSTANT

These are also called real constants, they contain factorial part too. A real constant in exponent form has two parts: a mantissa and an exponent.

SEPARATORS

( ) = Parentheses
These are used for function call and function parameter.
{ } = Braces
These are used for blocking codes having simple or compound statement.
[ ] = Brackets
These are used for enclosing subscripts in case of a single and multi dimensional arrays.
, = Comma
These are used for separating arguments list in a function.
; = Semicolon
These are used as a statement terminator.
: = Colon

These are used in case of a labeled statement.

AIRTHMATICAL OPERATORS

Some of the arithmetical operator are +,-, *, /, %.
+ = Add
Gives the addition of two or more numbers. (A+B)
- = Subtraction
Gives the subtract of two or more number (A-B)
* = Multiplication
Gives the multiple of two or more numbers (A*B)
/ = Divide
Gives the divide of two or more numbers (A/B)
% = Remainder=Modulus
Gives the remainder of two or more number (A%B)
Note: Where A & B are integers.


KEY WORDS

Keywords are reserved words in C++. They convey special message to language compiler. It has some of the following Keyword but we will learn some of them here.
asm, continue, float, new, signed, try, auto, default, for, operator, sizeof, typed, break,
delete, friend, private, static, union, do, goto, protected, struct, unsigned, while, do while,

DATA TYPES

They are used to indicate char, int, float, double, long double etc .The chart showing the allocation is as follows:-

http://img167.imageshack.us/img167/3821/chartfw8.png

The following are the meaning of the words written in table:-
# Char(For Character)
# Int (For Integer)
# Double (For Integer)
# Long (For Integer)
# Long Double (For Integer)
# Float (Decimals)
# Void (For non returning function)

RELATIONAL OPERATOR


<= Less than
>= Greater than
= - Equal to (Assignment Operator)
== - Double Equal to (Conditional Operator)

LOGICAL OPERATORS

&& - To give and logic
! - Not equals to
|| - To give or logic

CIN COUT

The Identifier cout>> is a predefine object that corresponds to the standard output stream.
The Identifier cin>> is a predefine object that corresponds to the standard input stream.
“<<” – Output of data.
The output of data is known as insertion output to operator.
“>>” – Input of data.

Input of data is known as extraction or to get data from the operator.
Cout<< is the command used to print the value visually in the screen and cin>> for getting the info. from user.

HEADER FILES


Header files as the name suggest are the head of the program and are written outside the main body. These files contain commands to run codes like cin>>, cout<<, getch (); clrscr (); etc...
The files we are going to learn here are.

#include<iostream.h> : The main header file has the commands to run commands like clrscr(); and getch();
#include<conio.h> : This header file has codes to run cin>> & cout<<.
#include<stdio.h> : This header file has a function called gets which is used as same as cin>> but it is mainly used for entering character and it reads spaces bt. Character while cin>> doesn’t read spaces in character .

For E.g.:- cin>>name;

Suppose user had entered Joe joe
Then cout<<name; will give the output Joe
While gets(name); will give same output as the name entered.
Also clrscr(); means Clear the screen if this does not given then the output of previously opened will also come along the output of your current program output.
getch(); is the command to give a pause to the console output screen , if not entered then the output will be shown but if u will enter any value the screen will return to the coding window.
Ahhhhhhhh…. Now I have completed the theory part Hope every one understood it if not ask without any hesitation. Let’s start the main Programming part.

STARTING PROGRAMMING BASICS

First while writing the program we should keep in minds that we should do less mistakes. So without wasting time I am giving the way how to write a program.
The following steps should be taken:-

#include <Header File>
void main()
{
<Variable Declaration>
<Program Statement>
getch();
}

Note: - After void main() ';' semicolon should not be placed other wise error will occur. Instead of void main() only main() can be used but at the end the program should return a value. Otherwise use void main() if u are a beginner.

Giving getch() is not an essential part of the program. It is only given to pause(rather waits for a keystroke ... more precisely your program is waiting for an input) after the completion of the program, since most people run the program pressing Ctrl+F9 and without it, it may seem to you that there was no output given.



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:-

#include<iostream.h>
#include<conio.h>
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.

#include<iostream.h>
#include<conio.h>
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()

Void main()
{
clrscr();
int a;
cout<<”Enter the value of A”<<endl;
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:-

#include<iostream.h>
#include<conio.h>
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.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
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.

#include<iostream.h>
#include<conio.h>
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.

#include<iostream.h>
#include<conio.h>
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 .

#include<iostream.h>
#include<conio.h>
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


DECISION MAKING/CONDITION CODING

C++ has a huge list of decision making/condition coding statement some of them that we are going to learn are:-

1)if statement
2)if else statement
3)do while statement[will learn while looping]


IF STATEMENT

The if statement may be implemented in different forms depending on the complexity of conditions to be tested.

• Simple if statement
• If ………else statement
• If ………else ….. if ladder statement.


Note: - I have attached 2 MS word file i.e. meant to be downloaded. The 1’st one consist program related to simple programming and the other of if else programming.
You just need to download them and write the program in your compiler and enjoy.



SIMPLE IF STATEMENT

The general form of simple if else statement as already written above is:-

if(Test Condition)
{
<Program Statement>
}
statement;


For e.g.:-

if(income>4000)
{
tax = 0.1*income;
income = income – tax;
}
cout<<”Tax”<<tax;
cout<<”Income”<<income;
……

The above part of the program is testing whether the income is greater than 4000 or not. If ‘yes’ the tax @of 10% is deducted from the income and then the final v alue should be printed.

Note:-In the whole programming condition = = must be used because we are comparing/conditioning the values so always use = =. Also && should be use for and. AND || this should be used for or condition.

THE IF….ELSE STATEMENT

The if ….else statement is an extension of simple if statement. The general form is:-

if(Test Condition)
{
<Program Statement>
}
else
{
<Program Statement>
}

If the test condition is true then the program statement under if braces will be executed. In either case if the condition become false then else statement will proceed.

Note: - You need to put braces { } at the starting and at the end if to or more program condition is given otherwise it may cause error. For e.g.:-

For e.g.:-

If (code<0)
Cout<<”Code is negative number”;

else

Cout<<”Code is positive number”;


In above program, if code is less than 0 than the code number entered is negative if not then it is positive.


SOME PROGRAMING BASICS

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,b;

cout<<"Enter the 2 no";
cout<<endl<<"Enter A\n";
cin>>a;
cout<<"Enter B";
cout<<"\n";cin>>b;

if(a>b)
cout<<"A is Greatest\n";
else
cout<<"B is Greatest \n";

getch();
}

This program is absolutely correct but if the same program is written with few more condition then following steps should be taken:-

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,b;

cout<<"Enter the 2 no";
cout<<endl<<"Enter A\n";
cin>>a;
cout<<"Enter B";
cout<<"\n";cin>>b;

if(a>b)
{
cout<<”A=\t”<<a;
cout<<”B=\t”<<b;
cout<<"A is Greatest\n";
}
else if(a==b)
{
cout<<”A=\t”<<a;
cout<<”B=\t”<<b;
cout<<"Both are equal\n";
}
else
{
cout<<”A=\t”<<a;
cout<<”B=\t”<<b;
cout<<"B is Greatest \n";
cout<<”A=\t”<<a;
cout<<”B=\t”<<b;
}

getch();
}

Here = = is used because comparison is done between 2 or more variable if u want to put character then use single quotes ( ‘ ‘ ) or double quotes. But unfortunately we cannot use more than one alphabet also if u assigned the value of char to 128. The e.g. given here is directly started with if else so don’t get confused:-

For e.g:-

if(day= =’s’&& day= =’S’)

cout<<”The day is Sunday”;

But if u will give more than one alphabet then error will come. In the under written program.

if(day= =’su’ && day= =’Su’)

cout<<”The day is Sunday”;

THE IF…..ELSE….IF LADDER

This is another way of putting it together when multiple decision are involved. A multiple decision is a chain of its in which several statements are attached with each if and else conditions.
The general form Is:-

if(condition a)
{
<Program Statement>
}
else if<condition b>
{
<Program Statement>
}
else if<condition c>
{
<Program Statement>
}
………

This construct is known as if….else…..if ladder . The condition are evaluated from top to down. As soon as a true condition is found the program statement attached to it will be executed otherwise compiler will look for other condition .
This type of program can be used for making results as follows.:-
The program written below shows Simple Interest based on several condition.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int t;
double amt,si;

cout<<"Enter the value of amount\n";
cin>>amt;
cout<<"Enter the total time period\n";
cin>>t;

if(amt>50000 && t>5)
si=(amt*t*11)/100;

else if(amt>=20000 && amt<50000 && t>8)
si=(amt*t*12/100);

else if(amt<20000 && t>=1)
si=(amt*t*6)/100;

cout<<"\nThe amount entered=\t\t"<<amt;
cout<<"\nThe Time entered=\t\t"<<t;
cout<<"\nThe Int. charged=\t\t"<<si;
cout<<"\nThe total amt. to be paid=\t"<<amt+si;

getch();
}

Dark Star
17-03-2007, 08:58 AM
ADDITION AND SUBSTRACTION IN IF ELSE

As the topic state it is very clear for one to understand that how to add and sub. Using if else. Now most of u will be thinking that this can be done buy simple programming then why to use if else. Ya your thinking is absolutely correct we don’t have to use if else while adding but imagine u are subtracting using simple programming and a user entered values as follows:-

You’re coding for Subtraction using simple programming:-

C= (a-B);

Suppose user entered a=10 and b= 8 then answer will come 2. But if user entered a=8 b=10 then answer will come -2. So to come out from this -2 mistake[Actually it is not a mistake but we have to get positive value] we use if else so the coding goes as follows:-

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,b,c;

cout<<”Enter the value of a and b\n”;
cin>>a>>b;

if(a>b)
c=(a-b);
else
c=(b-a);

cout<<”The Subtract of A and B is\t”<<c;


getch();
}

So that’s the end of if else hope u understand it plz guys if u have any confusion just ask, I am here to solve your problem .
Remind u again download the attached Program files.

C++ Guide Part 2


:welcome: to the second chamber of the C++, here u will know quick and hard programming so be ready to face the rumbling C++....So the earlier thread was quite conjusted so I thought to post another part this guide will start from looping and will end to looping for now soon it will be upgraded to switch and function.But guys warning do'nt take looping light its not just like if else or any thing similar to that.
Writing the guide as my computer exam are only left so this guide recall all lost info this is a more complicated part jking I will try to make it simple:D

So I am starting with post and pre increment:-- So check it out and learn with fun;

POST & PRE OPERATOR

An operator that require only one operand or data item are called unary operators. C++ supports unary minus(-), ++ (Increment), -- (Decrement) on airthmatic operands.
In C++ the other unary operator are ++ (Increment), -- (Decrement). These operator can be used before or after the variable, to give condition type increment and decerement.


INCREMENT OPERATOR
++ Increases the value by one. If it is used before or after the variable.....

i++ means i=i+1;
++i means i+1=i;

Both the formula are very diff and can the whole output, i=i+1 means that incrementation is done after the value is called. But i+1=i; means incrementation is done before the value is called.
For e.g. :-

int i;
i=10;
i=i++;
cout<<i;

Here first the value is incremented then the value is printed and the output will come 11. But if the same program is written in a way like this:-


int i;
i=10;
cout<<i++;

Then the output will be 10 no incrementation will be done beacuse as I already told i++ means i=i+1
so the compiler will first print the value of i then it will increament.

Also the same program with diff result:--
int i;
i=10;
cout<<++i;

Here the output will be 11 incrementation will be done beacuse as I already told ++i means i+1=i
so the compiler will first increament the value then it will print the value of i.
Note:- The following topic is important if U have any problem then please ask...

For e.g: -
int i,j;
i=10;
j=i++;
cout<<i<<" "<<j;

Here the output will be 10 11 . Here the value of i is assigned to j then in the value of i there is post incrementation of 1 so the final value of i remains same and j is 11.

MULTIPlE INCREMENTS

SO after simple increment and decrement move into the second chamber of complicated increments this type of increment are rarely used in programs but they can be asked in the outputs so as a guider I have to tell everything .
These type of increments and decrements are used in same line and with same integar or diff integar..

For e.g. :-----
void main()
{
clrscr();
int x;
x=10;
cout<<x++<<" "<<++x;
getch();
}

In the above program there are 2 output in the same line and the integer is same so the output will go from left to right but the assigning of numbers will be from right to left..

As the initial value of x is 10 and for assigning heading from right the first thing is ++x means x+1=x so the value become 11. Then there is x++ means x=x+1; so the comp will 1'st print the value then it will increment it so the value remains same as 11.The final output is 11 11.

I am not going into deep of this because this is not too much important as far as I know. SO lets start the loop.

LOOPS /REPETITIONS/ITERATIONS

A loop can be defined as the repition of the statements until the condition got fulfilled.Looping is consisted of statements that are executed until some condition for termination of a loop.

A program loop normally cosists of 2 segment:-

Control Statement
Body of the loop

The control statement test the condition and then direct the repeted execution of the statement contained in the body of the loop.
The body of the loop consists of the program statement that are given by the programmer....

The looping process in general performs the following 4 steps:---

Setting and Initialization of the counter.
Execution of the statement in the loop.
Checking of the condition to run the loop for specific period of time.
Incrementing the counter.


The C++ provides three loop constructs for performing loop operations. They are:

The while statement.
The do while statement.
The for statement.


THE WHILE STATEMENT

The simplest of all the loop :D. This looping structure is known as while loop, the basic format of while loop in C++ language....
while(test condition)
{
//Body of the loop
program statement
}
The following points should be remembered while using while loop :---

(1)The loop will not get executed if the given condition becomes false.
(1)The loop will be executed till the condition is false the result will be presented as soon as the condition becomes false.
There must be some looping termination inside the loop to avoid abnormal output..

Here are some e.g. To clear ur doubts........
Note:-- Plz download the attached documents to know better programming through while loop and all :D

Program to show no. From 1-100 using while loop..

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
while(1<=100)
{
cout<<i<<",";
i++;
}
getch();
}

Here the condition is that the loop should run till the value of i becomes 100 as soon as it becomes 100 the loop ends and the output will be shown.
Firstly the value is 1 then the condition is fulfiled that 1 is less than 100 prog. Will go inside the loop the 1'st statement is cout<<i the initial value of i is 1 so 1 will be the 1'st output then acc/que[/B] the second value should be 2 then 3 and so on upto 100 so we have to write i++ so there will be an increment of 1 in the value of i and the loop goes upto 100......:)

Program to add value upto 100..

I am directly starting from void main() so dont get confused ....:D

void main()
{
clrscr();
int i,s;
i=1;
s=0;
while(i<=100)
{
s=s+i;
i++;
}
cout<<"Sum =" <<" "<<s;
getch();
}


Here I had taken 2 variables i for taking the values from 1-100 and sum for adding those values... I had asssigned the value of s=0 because 0 is an additive properties of add.. If u will add some no.. to 0 the no. Remains same ....Similarly 1 is the multiplicative prop. Of Multi..:D:D . Here first the value of i is 1 then we add the value into s. The initila value of s is 0 so after add.. the value become 1 then i got incremented again loop will run and again there will be addition. And the vaue of s was 1 after adding 2 it becomes 3 ad the loop goes on till the value of i becomes 100...


To find out the factorial of any no..

void main()
{
int n,f;
f=1;
cout<<"Enter the value of i to find the factorial\n";
cin>>n;
while(n!=0) or (n>0)or (n>=1)
{
f=f*i;
i--;
}
cout<<"Factorial ="<<" " <<f;
getch();
}


Here the user will in enter the no.. and we have to find the factorial. For ur info.. telling as most of u had forgotten what is factorial, the number resulting from multiplying a whole number by every whole number between itself and 1 inclusive.

For e.g. :- Fact. Of 5 :--- 5*4*3*2*1= 120. :D
So to find the factotorial the prdecessor no. Includin the no should be mulitiplied... To find this i had taken a variable in which the entered value will be stored. And variable f to hold fac. Value.. First I had given the value of f 1 as i had told 1 is the multiplicative prop. Of Multi..:D:D.. SO multiplying an no with 1 will give same no.. Suppose user enters th value 5 first the condition will be checked. I had given 3 cond. All 3 are correct but u had to use any one of em.. So prog. Will enter the loop then the no will be multi . With f and the value of n will get decremented again it will get multi. Till the condition becomes false . And finally the output wil be given

DO WHILE STATEMENT

The do while loop is the 2'nd type of loop, the main positive aspect of thios loop that whether the condition is true or false the loop will run one time, if the condition will be true then the loop will be continued otherwise it will stop..

The syntax of the do while statement is as follows:-

do
{
<program statement>
}
while(<condition>);

Here the condition is at the last so the loop will run 1'st time till reaching the condition....

The followind point should be remembered while using do-while loop:---


(1)It is executed at least once..
(1)It is executed till the condition remains trueand the control comes out as soon as the loop ends.
(1)There must be some looping terminating condition otherwise the loop willnot stop...

Here are some e.g. To clear ur doubts........

To check whether the number is Armstrong or not....

Note:- An armstrong no. Is a no. Whose digit cube sum is equal to the same no..

void main()
{
clrscr();
int n,k,r.s;
cout<<"Enter the number\n";
cin>>n;
k=n;
s=0;
do
{
r=n%10;
s=s+r*r*r;
n=n/10;
}while(n!=0);
if(k==s)
cout<<"It is Armstrong no.";
else
cout<<"It is not Armstrong No..";
getch();
}

In the above program I had taken 4 variables n for storing the number, k for storing the value of n so that we can check the condition,s for adding,r for taking remainder or storing every digit of the entered number...
So suppose u had entered the no 153 the no. Gets stored in k so n and k both will hold 153. Now the loop will begins first acc/cond given no will get divided be 10 and the remainder will get stored in r. First when u will divide 153 by 10 the remainder will come 3 the in second line cube of 3 i.e 27 will be added to s i.e 0 so s is now 27 then the remaining part will again got divided and the cube of 5 i.e 125 then again the last no cube i.e 1 will be added so the sum of 27+125+1=153 so the no. Is armstrong no..


To find factorial of any number.

void main()
{
clrscr();
int n,f;
cout<<"Enter the number\n";
cin>>n;
f=1;
do
{
f=f*n;
n=n-1;
}while(n!=0);
getch();
}
I have already explained it so its should be clear to all...:D


[B]FOR STATEMENT
I dont know why but this is the only loop which i finds the best. This loop is an entry controlled loop means condition should be fulfiled to enter it.The syntax of this loop is as follows:--

for(initialisation;condition;increment/decrement)
{
<program statement>;
}


Initialisation Expression:-
It is executed only when the loop 1'st start. It provides loop variable an initial value.

Test Expression:-
It involves relation operator. It is executed every time through the loop before the body of the loop is executed . If the test expression is true the loop wil go on otherwise the loop wil end.

Increment/Decrement (re-initialising) expression:-
It os always executed at the end of the loop after the body of the loop.
Here are some e.g. To help u:D

For the following output:

*
* *
* * *
* * * *
* * * * *

void main()
{
clrscr();
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<"*"<<" ";
cout<<"\n";
}
}
getch();
}

For finding out the factorial of any number.

void main()
{
clrscr();
int i,f,n;
cout<<"Enter the number\n";,
cin>>n;
f=1;
for(i=n;i>=1;i++)
{
f=f*i;
}
cout<<"The factorial is =\t"<<f;
getch();
}

For displaying the table of any number..


void main()
{
clrscr();
int i,n,t;
cout<<"Enter the number\n";
cin>>n;
for(i=1;i<=10;i++)
{
t=i*n;
cout<<t;
cout<<"\n";
}
getch();
}


:If u like it then Rep me ;)
Till then enjoyyyy..:)
Regards Shash
[/FONT]

vivekrm007
17-03-2007, 09:55 AM
Gr8 Post awesome.....+ Repute Addded.....

Dark Star
17-03-2007, 12:09 PM
^^ Thanks buddy it seems that less people are interested in Coding :(

LEARNER_LEARNER
20-03-2007, 09:23 PM
wow really gr8 4.lage raho .+ rep added.

gaurav_indian
20-03-2007, 09:26 PM
Thanks yaar.I appreciate your effort.Reps 4u.

max_demon
20-03-2007, 09:44 PM
+rep , thanx yaaar

rakeshishere
20-03-2007, 09:50 PM
Gud One//rep Added;-)

aj27july
20-03-2007, 10:49 PM
i would rather download Bruce Eckel's award winning book "Thinking in C++" because it is free and available at the author's site.

navjotjsingh
21-03-2007, 01:10 AM
I found the link for Bruce Eckel book: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

piyush gupta
21-03-2007, 09:53 AM
Hey guys but his post is not that bad

its gud

navjotjsingh
21-03-2007, 09:56 AM
I didn't say that atleast...I just quoted a free link for a free book.

Dark Star
21-03-2007, 12:55 PM
I didn't say that atleast...I just quoted a free link for a free book.
Its all right and thanks for appreciation Calm down all :)

gigyaster
21-03-2007, 01:15 PM
Hey, thanks a lot, actually I got what I was waiting for.
Thanks Again.

Dark Star
21-03-2007, 01:37 PM
Gud One//rep Added;-)

Only I did not recieive it :P

rakeshishere
21-03-2007, 06:07 PM
Only I did not recieive it :P

WARNING:Spread some reputation b4 u give :D

phreak0ut
21-03-2007, 06:48 PM
Very good post. Repped you :)

Pathik
21-03-2007, 09:05 PM
@shaswat i dont suppose u typed it all... :p
could u please give the source ... it ll be very useful to me.. thx :)

iMav
21-03-2007, 09:10 PM
use google and get this:

http://www.techtalkz.com/member-guides/3133-c-learning-guide.html

gaurav_indian
21-03-2007, 09:26 PM
@shaswat i dont suppose u typed it all... :p
could u please give the source ... it ll be very useful to me.. thx :)
Thoda copy paste toh chalta hai yaar.:D

iceeeeman
21-03-2007, 10:23 PM
dude where to type these codes
__________
dude where to copy these code????

sauravktr
22-03-2007, 03:42 AM
Thanks Yaar. Nice TUT

Dark Star
22-03-2007, 10:13 AM
dude where to type these codes
__________
dude where to copy these code????

Yu had to type these codes in the C++ compiler this guide has been made by using C++ compiler named Borland Turbo C# ver. 3.0 ;) So better you use the same :P
__________
@pathiks,mAV3,gaurav_indian

I could not stop laughing at all of you 3 ... Rolf just check that who is Renegade at Tech Talkz.... I could not beleive it you got mistaken by changed name hahahahahahahahah...... I am still laughing .. It's me Damn its me ..

Just a name change I am the only renegade of techtalkz the tech reporter if you still cant beleive it then ask the admin or any other.. ;)

Well for you information I had posted this guide at Tech Enclave and my name there is Dark Star so don't get messed up dude 's :P:P:P:P:P :D:D:D:D

Tech Geek
22-03-2007, 12:17 PM
Really cool tut
reps for you

gaurav_indian
22-03-2007, 12:25 PM
Yu had to type these codes in the C++ compiler this guide has been made by using C++ compiler named Borland Turbo C# ver. 3.0 ;) So better you use the same :P
__________
@pathiks,mAV3,gaurav_indian

I could not stop laughing at all of you 3 ... Rolf just check that who is Renegade at Tech Talkz.... I could not beleive it you got mistaken by changed name hahahahahahahahah...... I am still laughing .. It's me Damn its me ..

Just a name change I am the only renegade of techtalkz the tech reporter if you still cant beleive it then ask the admin or any other.. ;)

Well for you information I had posted this guide at Tech Enclave and my name there is Dark Star so don't get messed up dude 's :P:P:P:P:P :D:D:D:D
Sorry yaar hum toh majak kar rahe thay tum toh serious ho gaye.Hum toh tumhe test kar rahe thay.:D

iceeeeman
22-03-2007, 02:41 PM
Yu had to type these codes in the C++ compiler this guide has been made by using C++ compiler named Borland Turbo C# ver. 3.0 ;) So better you use the same :P
:D

dude can u tel me from where to download thIs software.__________

rakeshishere
22-03-2007, 04:21 PM
Download C++ compiler from the Below link
http://www.bloodshed.net/

Pathik
22-03-2007, 05:45 PM
@shaswat i didnt accuse u... i just asked if u had copied this content from some book cos i liked it...
ye sab mav3 ne kiya hai... :p
btw yea use devc++ its a lot better than turbo c++

gaurav_indian
23-03-2007, 12:30 AM
@pathiks devc++ is free?The one in the Digit this month was a beta.

Dark Star
24-03-2007, 10:13 AM
@shaswat i didnt accuse u... i just asked if u had copied this content from some book cos i liked it...

No borther I had done C++ course so many times as a result of that theory part is on my lips so don't worry I did not imitated any of the content from any book cause I did not had any book on C++ :p ya as far questions I always disturb my teacher for that :) And some times he gets annoyed as the ans of those question are solved even in seconds ;)
I had created few games using C++ will share with you soon :)

As far as Dev C++ I will surely give that a try.. Today only. And ya Borland C++ is a paid software so better do not pirate it if you are a coder .. Plz feel the anguish of a COder :(

iMav
24-03-2007, 11:11 AM
ye sab mav3 ne kiya hai... hey shashwat i owe u an apology .... i should have gone thru that website and seen ur location which is common on both forums ...

but u know how a lot of pppl seeking fame and rep are ... copy and paste they do ... sorry and keep up the good work

anantkhaitan
27-03-2007, 04:21 PM
I was unable to compile my c++ codes in Linux using gcc, therefore i use
Dosbox (http://prdownloads.sourceforge.net/dosbox/dosbox-0.70.tar.gz?download)
to run my Turboc C++ , and now i know this is a paid software..
So better if anyone can help me out

piyush gupta
27-03-2007, 05:37 PM
^^but why u r unable to compile ur c++ cod ein linux

Don't u know the way of an error if so put error msg here

anantkhaitan
28-03-2007, 02:50 PM
Content of my file 'abc.cpp'


#include<iostream.h>
void main()
{
int a,b;
cout<<"Enter numbers : ";
cin>>a>>b;
cout<<"Sum : "<<a+b;
}


The command I trigger to compile the code :


$ gcc abc.cpp


Error Message:


In file included from /usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/backward/iostream.h:31,
from abc.cpp:1:
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
abc.cpp:2: error: ‘::main’ must return ‘int’

anantkhaitan
29-03-2007, 03:02 PM
^^^
Plz give me a solution Piyush i m waiting for a reply

Note a minor change in code:

#include<iostream.h>
int main()
{
int a,b;
cout<<"Enter numbers : ";
cin>>a>>b;
cout<<"Sum : "<<a+b;
return 0;
}


But still not working

piyush gupta
29-03-2007, 05:16 PM
Sorry for late reply

http://www.linux-noob.com/forums/lofiversion/index.php/t632.html

This link solves ur problem

Use g++ for C++ files

#include <iostream>

using namespace std;

int main() {
cout << "Hello, world!" << endl;
return 0; // No error
}

g++ -o helloworld helloworld.cpp

Desi-Tek.com
29-03-2007, 08:58 PM
u can refer this page for tutorial on c and tutorial on data stucture.
http://www.desi-tek.com/forum/index.php?act=SF&s=&f=35
i wrote that tutorial long time back when i was studying c

anantkhaitan
30-03-2007, 03:02 PM
Thanks Piyush for the link now I m able to compile with 'make'
I want to share this example with u all

Code:

#include<iostream>
int main()
{
int a,b;
std::cout<<"Enter numbers : ";
std::cin>>a>>b;
std::cout<<"Sum : "<<a+b;
return 0;
}

save the file as abc.cpp
and run the following commands
$ make abc
then
$ ./abc
thats all

Ron
21-07-2007, 11:42 AM
thnks

mandeep444
21-07-2007, 10:48 PM
nice work "Shashwat Pant"

mandeep444