#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.
Monday, March 2, 2009
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.
#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.
#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.
Wednesday, February 25, 2009
Write C++ programs that use both recursive and non-recursive functions to find the GCD of two given integers.
a). using Recursive function
int gcd (int, int); //func. declaration.
void main( )
{
int a, b, res;
clrscr( );
cout<<"Enter the two integer values:";
cin>> a >>b;
res= gcd(a, b); // calling function.
cout<<" GCD is:" << res;
getch( );
}
int gcd( int x, int y) //called function.
{
int z;
z=x%y;
if(z==0)
return y;
gcd(y,z); //recursive function
}
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
int gcd (int, int); //func. declaration.
void main( )
{
int a, b, res;
clrscr( );
cout<<"Enter the two integer values:";
cin>> a >>b;
res= gcd(a, b); // calling function.
cout<<" GCD is:" << res;
getch( );
}
int gcd( int x, int y) //called function.
{
int z;
z=x%y;
if(z==0)
return y;
gcd(y,z); //recursive function
}
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Write C++ programs that use both recursive and non-recursive functions to find the GCD of two given integers.
a). Without using Recursive function.
void gcd (int, int); //func. declaration.
void main( )
{
int a, b;
clrscr( );
cout<<"Enter the two integer values:";
cin>>a >>b;
cout<<"Given numbers are:" << a <<"and" << b;
gcd(a, b); // calling function.
getch( );
}
void gcd( int x, int y) //called function.
{
int z;
for( ; ; ) //This is empty for loop.
{
z= x%y;
if( z==0) break;
x=y;
y=z;
}
cout<<"The GCD is:" << y;
}
Note:- Take the values as 23 and 3 , then GCd is 3.
This logic works, when a>b. (ie., 23>3 )
I'm not mentioned the logic when b>a. ( for that, we need if-else statement)
Explanation:-
Here i'm taking empty for loop becoz i dont have initial value , condition and increment or decrement.
This for loop will ends, when 'z' becomes zero. ie.,(z==0)
void gcd (int, int); //func. declaration.
void main( )
{
int a, b;
clrscr( );
cout<<"Enter the two integer values:";
cin>>a >>b;
cout<<"Given numbers are:" << a <<"and" << b;
gcd(a, b); // calling function.
getch( );
}
void gcd( int x, int y) //called function.
{
int z;
for( ; ; ) //This is empty for loop.
{
z= x%y;
if( z==0) break;
x=y;
y=z;
}
cout<<"The GCD is:" << y;
}
Note:- Take the values as 23 and 3 , then GCd is 3.
This logic works, when a>b. (ie., 23>3 )
I'm not mentioned the logic when b>a. ( for that, we need if-else statement)
Explanation:-
Here i'm taking empty for loop becoz i dont have initial value , condition and increment or decrement.
This for loop will ends, when 'z' becomes zero. ie.,(z==0)
Write C++ programs that use both recursive and non-recursive functions to find the factorial of a given integer.
int fact( int); // function declaration
void main( )
{
int no,result;
clrscr( );
cout<<"Enter the required number:"; cin>>no;
result = fact( no);
cout<<"Factorial is :" << result;
getch( );
}
int fact(int n)
{
int ft;
for( ft=1; n>=1; n--)
ft=ft*n;
return ft;
}
Output:- 4 Factorial is : 24
(a).using recursive function.
int fact( int); // function declaration
void main( )
{
int no,result;
clrscr( );
cout<<"Enter the required number:"; cin>> n0;
result = fact( no);
cout<<"Factorial :"<< result;
getch( );
}
int fact(int n)
{
int ft;
if( n==1)
return 1;
else
ft= n*fact (n-1);
return ft;
}
Output:- 4 Factorial is : 24
void main( )
{
int no,result;
clrscr( );
cout<<"Enter the required number:"; cin>>no;
result = fact( no);
cout<<"Factorial is :" << result;
getch( );
}
int fact(int n)
{
int ft;
for( ft=1; n>=1; n--)
ft=ft*n;
return ft;
}
Output:- 4 Factorial is : 24
(a).using recursive function.
int fact( int); // function declaration
void main( )
{
int no,result;
clrscr( );
cout<<"Enter the required number:"; cin>> n0;
result = fact( no);
cout<<"Factorial :"<< result;
getch( );
}
int fact(int n)
{
int ft;
if( n==1)
return 1;
else
ft= n*fact (n-1);
return ft;
}
Output:- 4 Factorial is : 24
Write a C++ program to generate all the prime numbers between 1 to n, where n is a value supplied by the user.
#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
void main( )
{
int n, x, flag,ct;
clrscr( );
cout<<"Enter the n value:"; cin>> n;
cout<<"Prime Numbers:";
for( ct=1; ct<=n; ct++)
{
x=2; flag=0;
while(x<=ct/2)
{
if(ct%x==0) { flag=1; break; }
x++;
}
if(flag==0)
cout<< ct;
}
getch( );
}
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
#include(conio.h)
void main( )
{
int n, x, flag,ct;
clrscr( );
cout<<"Enter the n value:"; cin>> n;
cout<<"Prime Numbers:";
for( ct=1; ct<=n; ct++)
{
x=2; flag=0;
while(x<=ct/2)
{
if(ct%x==0) { flag=1; break; }
x++;
}
if(flag==0)
cout<< ct;
}
getch( );
}
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Tuesday, February 24, 2009
Write a C++ program to generate the first n terms of the sequence.
--A fibonacci sequence is difined as follows: the first and second term in the sequence are 0 and 1. Subsequent terms are found by adding the proceeding two terms in the sequence.
#include(iostream.h)
#include(conio.h)
void main( )
{
int f=0, s=1, t, n, ct;
clrscr( );
cout<<"Enter the no. of terms:";
cin>>n;
cout<<"Fibonacci series:";
cout<< f << s;
for( ct=3; ct<=10; ct++)
{
t=f+s;
cout<< t;
f=s;
s=t;
}
getch( );
}
Here 'f' means 'first' 's' means 'second'
't' means 'third'
'ct' means 'count' the displayed numbers.
Output:- Enter the no. of terms: 10
The Fibonocci series:
0 1 1 2 3 5 8 13 21 34
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
#include(iostream.h)
#include(conio.h)
void main( )
{
int f=0, s=1, t, n, ct;
clrscr( );
cout<<"Enter the no. of terms:";
cin>>n;
cout<<"Fibonacci series:";
cout<< f << s;
for( ct=3; ct<=10; ct++)
{
t=f+s;
cout<< t;
f=s;
s=t;
}
getch( );
}
Here 'f' means 'first' 's' means 'second'
't' means 'third'
'ct' means 'count' the displayed numbers.
Output:- Enter the no. of terms: 10
The Fibonocci series:
0 1 1 2 3 5 8 13 21 34
Note:- If u have any doubt regarding this program or logic, please feel free to contact me.
Write a C++ program to find the sum of individual digits of a positive integer.
#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
void main(void)
{
int no, rem, sum=0;
clrscr();
cout<<"Enter the required number:";
cin>>no;
while(no>0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
cout<<"Sum of individual digits of a positive integer is:"<< sum;
getch();
}
Output:-
here 'rem' means remainder
Enter the required number: 123
Sum of individual digits of a positive integer is: 6
Note:-If u have any doubt regarding this program or logic, please feel free to contact me.
#include(conio.h)
void main(void)
{
int no, rem, sum=0;
clrscr();
cout<<"Enter the required number:";
cin>>no;
while(no>0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
cout<<"Sum of individual digits of a positive integer is:"<< sum;
getch();
}
Output:-
here 'rem' means remainder
Enter the required number: 123
Sum of individual digits of a positive integer is: 6
Note:-If u have any doubt regarding this program or logic, please feel free to contact me.
Subscribe to:
Posts (Atom)