Your Ad Here

Monday, March 2, 2009

Write a C++ program to sort a list of names in ascending order.

#include(iostream.h)
#include(conio.h)
#include(stdio.h)
#include(string.h)
void main()
{
char st[10][10],temp[10];
int i, j, n;
clrscr();
cout<<"Enter the no. of names:";
cin>>n;
cout<<"Enter the different names:";
for(i=0; i< n; i++)
cin>>st[i];
for(i=0; i< n; i++)
{
for(j=i; j< n-1; j++)
{
if(strcmp(st[i], st[j+1]) >0)
{
strcpy(temp,st[i]);
strcpy(st[i],st[j+1]);
strcpy(st[j+1],temp);
}
}
}
cout<<"Given names after ascending order:";
for(i=0;i<5;i++)
cout<< st[i];
getch();
}


Output: Enter the no.of names: 5

Enter the different names: apple
cat
boy
apec
suresh

Given names after ascending order:
apec
apple
boy
cat
suresh

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Friday, February 27, 2009

Write a C++ program to count the lines, words and characters in a given text.

#include(iostream.h)
#include(conio.h)
#include(stdio.h)
void main( )
{
char ch;
int sp=0, ct=0, line=0;
clrscr( );
cout<<"Enter the required lines of text:";
while((ch=getchar())!=EOF)
{
ct++;
if((ch==' ')
sp++;
if(ch=='\n')
line++;
}
cout<<"Total Lines:"<< line;
cout<<"Total words:"<< sp+line;
cout<<"Total Characters:"<< ct;
getch( );
}

Output:- Enter the required lines of text:
Where are you
Come fast
^z

Total Lines: 2
Total words: 5

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Write a C++ program to make the frequency count of letters in a given text.

#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
#include(stdio.h)
void main( )
{
chat st[80];
int i, ct=0,sp=0;
clrscr( );
cout<<"Enter the required information:";
gets(st); for(i=0; st[i]!='\0'; i++)
{
ct++;
if(st[i]==' ')
sp++;
}
cout<<"Total Letters present in the given text :"<< ct-sp;
getch( );
}

Here the variable 'sp' is for counting space and 'ct' is for counting characters.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Thursday, February 26, 2009

Write a C++ program to sort a list of numbers in ascending order.

#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
void main( )
{
int a[10], n, i, j, temp;
clrscr( );
cout<<"Enter the no. of elements:";
cin>> n;
cout<<"Enter the array elements:";
for(i=0; i< n; i++)
cin>>a[i];
//------Operation part-------
for( i=0; i< n; i++)
{
for(j=i; j< n-1; j++)
{
if(a[i]> a[j+1] )
{
temp= a[i];
a[i]= a[j+1];
a[j+1]= temp;
}
}
}

cout<<"Elements after sorting:";
for( i=0; i< n; i++)
cin>> a[i];
getch ( );
}


Note
:- If u have any doubt regarding this program or logic, please feel free to contact me.

Write a C++ program to find both the largest and smallest number in a list of integers.

#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
void main(void)
{
int a[50], max, min, i, n;
clrscr( );
cout<<"Enter how many integers do u want? :"; cin>>n;
for( i=0; i< n ; i++)
{
cout<<"Enter the numbers: ";
cin>>a[ i ];
if(i==o) { max=a[i ]; min=a[i ]; }
if(a[ i]>max)
max= a[i ];
if(a[i ]< min)
min= a[i ];
}
cout<<"Maximum : "<< max;
cout<<"Minimum : "<< min;
getch( );
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Write a C++ program that uses functions to swap two characters.

#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
void swap(int, int);
void main( )
{
char s1, s2;
clrscr( );
cout<<"Enter the two characters to swap:";
cin>>s1>>s2;
swap(s1, s2);
getch( );
}
void swap(char c1, char c2)
{
char c3;
cout<<"Characters before swapping:"<< c1<<"and"<< c2;
c3=c1;
c1=c2;
c2=c3;
cout<<"Characters after swapping:"<< c1<<"and"<< c2;
}

Output: Enter the two characters to swap: a b

Values before swapping: a and b
Values after swapping: b and a

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Write a C++ program that uses functions to swap two integers.

#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
void swap(int, int);
void main( )
{
int x, y;
clrscr( );
cout<<"Enter the two numbers to swap:"; cin>>x >>y;
swap(x, y);
getch( );
}
void swap( int a, int b)
{
int temp;
cout<<"Values before swapping: "<< a <<"and"<< b;
temp=a;
a=b;
t=temp;
cout<<"Vaues after swapping:" << a <<"and"<< b;
}

Output: Enter the two numbers to swap: 10 20

Values before swapping: 10 and 20
Values after swapping: 20 and 10

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.