View Single Post
Old 17-03-2007, 08:58 AM   #2 (permalink)
Dark Star
Wise Old Owl
 
Dark Star's Avatar
 
Join Date: Feb 2006
Location: /dev/hd0
Posts: 1,487
Arrow Re: Tutorial: Learn All The Basics Of C++ Language

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

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

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

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

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

Code:
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:--
Code:
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: -
Code:
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. :-----
Code:
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 . This looping structure is known as while loop, the basic format of while loop in C++ language....
Code:
while(test condition)
{
//Body of the loop
program statement
}

The following points should be remembered while using while loop :---
  1. (1)The loop will not get executed if the given condition becomes false.
  2. (1)The loop will be executed till the condition is false the result will be presented as soon as the condition becomes false.
  3. 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
  • Program to show no. From 1-100 using while loop..
Code:
#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 [B]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 ....

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

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

Code:
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. (1)It is executed at least once..
  2. (1)It is executed till the condition remains trueand the control comes out as soon as the loop ends.
  3. (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..

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


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

Code:
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
  • For the following output:
*
* *
* * *
* * * *
* * * * *


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

Code:
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]
__________________
Me Myself and My Tux Blog :- http://tuxenclave.wordpress.com/
Dark Star is offline