#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.
Friday, February 27, 2009
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.
#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.
#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];
}
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.
#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.
#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.
#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.
Write a C++ program that uses a recursive function for solving Towers of Hanoi problem.
#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
#include(math.h)
void hanoi(int x, char from, char to, char aux)
{
if(x==1)
cout<<"Move Disk From"<< from <<"to"<< to;
else
{
hanoi(x-1,from,aux,to);
cout<<"Move Disk From"<< from <<"to"<< to;
}
}
void main( )
{
int disk;
int moves;
clrscr();
cout<<"Enter the number of disks you want to play with:";
cin>>disk;
moves=pow(2,disk)-1;
cout<<"The No of moves required is:" << moves;
hanoi(disk,'A','C','B');
getch( );
}
Output:-
Enter the number of disks you want to play with: 3
The No of moves required is=7
Move Disk from A to C
Move Disk from A to B
Move Disk from C to B
Move Disk from A to C
Move Disk from B to A
Move Disk from B to C
Move Disk from A to C
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
#include(conio.h)
#include(math.h)
void hanoi(int x, char from, char to, char aux)
{
if(x==1)
cout<<"Move Disk From"<< from <<"to"<< to;
else
{
hanoi(x-1,from,aux,to);
cout<<"Move Disk From"<< from <<"to"<< to;
}
}
void main( )
{
int disk;
int moves;
clrscr();
cout<<"Enter the number of disks you want to play with:";
cin>>disk;
moves=pow(2,disk)-1;
cout<<"The No of moves required is:" << moves;
hanoi(disk,'A','C','B');
getch( );
}
Output:-
Enter the number of disks you want to play with: 3
The No of moves required is=7
Move Disk from A to C
Move Disk from A to B
Move Disk from C to B
Move Disk from A to C
Move Disk from B to A
Move Disk from B to C
Move Disk from A to C
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Subscribe to:
Posts (Atom)
