Sunday, January 30, 2011

A simple loop in C++ ( cpp Loop Program)

Loops are actually used to repeat the statements to a specified number of times.

Here is a simple loop program to add numbers between 2 inputted numbers.

#include"iostream.h"
#include"conio.h"

void main()
{
clrscr();
int num1,num2,sum=0;

cout<<"Enter 1st number :: ";
cin>>num1;

cout<<"\nEnter 2nd number :: ";
cin>>num2;

for(int i=num1;i<=num2;i++)
{
sum=sum+i;
cout<<"\nSum of the numbers is "<getch();
}

Saturday, January 29, 2011

Turbo c compiler download

Want to download Turbo C compiler ?

Here is a link

C Compiler download

Reversing words in a string in cpp (c plus plus)

Now , we wanna do something chilly , user will enter a string as
i am a boy
our program should return
boy a am i

Here is a program for this logic . .

******************* Start of Program ************************

// Reversing words in a string

#include"iostream.h"
#include"conio.h"
#include"stdio.h"

void main()
{
clrscr();
char str1[50],str2[50];
cout<<"Enter a String :: ";
gets(str1);

int h=0;
while(str1[h]!='\0') // to get number of chracters in sub string
{
h++;
}

int flag=h-1;
int u=0;


int d=0;
for(int i=h-1;i>=0;i--)
{
if(str1[i]==' '||i==0)
{
u=i;
while(u<=flag)
{
str2[d]=str1[u];
d++;
u++;
}
if(i!=0)
{
str2[d]=' ';
d++;
}
flag=i-1;
}
} str2[d]='\0';

cout<<"\n\nProcessed string is :: "<<(str2);
getch();
}

Reverse a string in c++

Here is a program to reverse a string , quite interesting and easy

******************* Start of Program ************************


// Reversing a string

#include"iostream.h"
#include"conio.h"
#include"stdio.h"

void main()
{
clrscr();
char str1[50],str2[50];
cout<<"Enter a String :: ";
gets(str1);

int h=0;
while(str1[h]!='\0') // to get number of chracters in sub string
{
str2[h]=str1[h];
h++;
}
str2[h]='\0';

int k=0,m=h-1;

while(m>=0)
{
str1[k]=str2[m];
m--;
k++;
}


cout<<"\n\nOutput string is :: "<
getch();
}

******************* END ************************

Finding sub-string in a string in c++

The main goal of this program is to find a sub string in an inputted string
for example you give an "input hello i am a good boy" , and gives sub-string hello, it will find hello in the string that whether its there or not



******************* Start of Program ************************

// Finding sub-string in a String

#include"iostream.h"
#include"conio.h"
#include"stdio.h"

void main()
{
clrscr();
char str1[20],str2[20];
cout<<"Enter Main String :: ";
gets(str1);
cout<<"Enter sub string :: ";
gets(str2);

int i=0,j=0;

/////////

int h=0;
while(str2[h]!='\0') // to get number of chracters in sub string
{
h++;
}
//h-=1;

/////////



//while(str2[i]!='\0')
//{
// j=i;
int g=0;
int count=0; // to store number of matches

int c=0;

int flag;


while(str1[g]!='\0')
{

if(count==h)
break;

else
{

if(str2[c]==str1[g])
{
if(count==0)
flag=g;
count++;
c++;
}

else if(count>0)
{
c=0;
count=0;
g=flag;
}

else
{
// c++;
}

}

g++;

}



if(count==h)
{
cout<<"\n\nSub String Matched";
}
else
cout<<"\n\nSub String was not found";

getch();
}

******************* END ************************

Swapping two strings in c++

Swapping two strings is an interesting program , here is the c++ code

******************* Start of Program ************************

// Swapping two strings

#include"iostream.h"
#include"conio.h"
#include"stdio.h"

void main()
{
clrscr();
char str1[20],str2[20],swap[20];
cout<<"Enter 1st string :: ";
gets(str1);
cout<<"Enter 2nd string :: ";
gets(str2);

int i=0;

while(str1[i]!='\0')
{
swap[i]=str1[i];
i++;
}

swap[i]='\0';


int j=0;

while(str2[j]!='\0')
{
str1[j]=str2[j];
j++;
}

str1[j]='\0';

j=0;
while(j{
str2[j]=swap[j];
j++;
}
str2[j]='\0';

cout<<"\n\nAfter Swapping ::";
cout<<"\n\nString 1 is :: "<cout<<"\nString 2 is :: "<
getch();
}

******************* END ************************

Swapping two variables in c++

Swapping of variables means that changing the values of variable with each other's value.

Swapping two integer values


Method 1 (using third variable )
--------

******************* Start of Program ************************

#include"iostream.h"
#include"conio.h"

void main()
{
clrscr();
int a,b,c;
cout<<"Enter value of a :: ";
cin>>a;
cout<<"\nEnter value of b :: ";
cin>>b;


c=a;
a=b;
b=c;

cout<<"\nNow value of a is "<cout<<"\nNow value of b is "<
getch();
}
******************* END ************************



Method 2 ( without using third variable )
--------
* Main advantage of this program is that it uses less memory

******************* Start of Program ************************

#include"iostream.h"
#include"conio.h"

void main()
{
clrscr();
int a,b;
cout<<"Enter value of a :: ";
cin>>a;
cout<<"\nEnter value of b :: ";
cin>>b;


a=a+b;
b=a-b;
a=a-b;

cout<<"\nNow value of a is "<cout<<"\nNow value of b is "<
getch();
}

******************* END ************************

Concatenation of two strings in c++

Here is a simple program to concatenate a string with an other to make a single string

******************* Start of Program ************************

// Concatenation of strings

#include"iostream.h"
#include"conio.h"
#include"stdio.h"

void main()
{
clrscr();
char str1[20],str2[20];
cout<<"Enter 1st string :: ";
gets(str1);
cout<<"Enter 2nd string :: ";
gets(str2);

int i=0;

while(str1[i]!='\0')
{
i++;
}

int j=0;

while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}

str1[i]='\0';

cout<<"\n\nNow string 1 is :: "<
getch();
}


******************************* END *****************************

A simple Factorial program in c++

What a factorial is ??
A factorial of a number is the multiplication of all the numbers between that number and 1.
Finding factorial of a number is too easy if you simply got the logic discussed above,

Following is a program for finding the factorial of a number

// single line comments start with // lines
/* while multi-line comments start with /* and
end with */

******************* Start of Program ************************

#include"conio.h"
#include"iostream.h"

void main()
{
clrscr(); // to clear the screen
int n,fact=1;

cout<<"Please enter a number to find factorial :: ";
cin>>n;

for(int i=n;i>=1;i--)
{
fact=fact*i;
}


cout<<"Factorial of "<
getch();
}

*********************** END ************************

Getting error in turbo c compiler ???

Following are some of the errors

1) unable to locate iostream header file
Solution:
You should set the lib paths correctly else check whether your turbo c path is as follows or not
C:\\tc\\lib

if it isn't like the above 1 , change your folder names to make the turbo c folder look like as c:\\tc\\

2) Always save the file first before compiling it ,
To compiler use short key Alt+F9
To execute use short key Ctrl+F9

A simple Program in c++ (c plus plus)

Here is a simple program in c++ to check whether your compiler is working fine or not

#include "iostream.h"
#include "conio.h"

void main()
{
clrscr();
cout<<"Welcome to my first program";

getch();
}