"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Sunday, February 28, 2010

WAP to check a number is palindrome or not

eg.  
10201 is a palindrome
10210 is not a palindrome


#include<iostream.h>
#include<conio.h>

void main()
{
int num, rev=0;

cout<< "\n Enter a number :";
cin>> num;

/* truncate each digit from number and store the reverse of the number in rev */
int digit;
for(int i = num; i >0 ; i = i/10)
{
digit = i % 10;  // get the last digit
rev = rev* 10 + digit;
}

if( rev == num )
cout<< "\n The " << num  << " is a Palindrome. ";
else
cout << " \n The " << num << " is not a Palindrome. ";

getch();

}

Thursday, February 25, 2010

WAP to print all prime numbers between 100 to 500 , that are palindrome.


#include<iostream.h>
#include<conio.h>
void main()
{
int flag,rev;

cout<< "\n The palindrome prime numbers between 100 to 500 are :\n";

for(int n = 100 ; n<=500 ; n ++ )
{
flag = 0;
/* Check Prime number for each n */ 
for(int j = 2; j < = n-1 ; j++)
{
if(n%j == 0)
{
flag = 1;
break;
}
}

rev = 0;

//reverse the number and check palindrome
if(flag == 0)
{
for(int k = n; k >=1 ; k = k/10)
{
rev = rev*10 + k%10;
}

if(rev == n)
cout<< n << " " ;
}

}

getch();

}

Write a C++ program to print the following series :

(i) 1  4   7  10   ......... 40
(ii) 1  -4  7  -10  ........ -40


#include<iostream.h>
#include<conio.h>
void main()
{
int a = 1, b = 1, i , incre = 3;

cout<<"\n The Series 1:";

for( i = 1 ; a<=40; i++)
{
cout<< a << " " ;
a = a+incre;
}

int p;

cout<<"\n The Series 2:";



for( i = 1 ; a<=40; i++)
{
if(i%2 == 0)
{p=-a;

cout<< p << " " ;
}
else
cout<< a << " ";

a = a+incre;
}
getch();

}

WAP to print the truth table for XY + Z

Truth Table for XY+Z

X    Y    Z     XY+Z
0     0     0     0
0     0     1     1
0     1     0     0
0     1     1     1
1     0     0     0
1     0     1     1
1     1     0     1
1     1     1     1

/* Logic is
if Z is 1 then output is 1 ;
or
if X and Y both are 1 , the output is 1 . */

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
cout<< "Truth Table\n";
cout<< "X\t Y\t Z\t\t XY+Z \n\n";

for(int i = 0 ; i<=1 ; i++)
for(int j = 0; j<=1; j++)
for(int k = 0; k<=1; k++)
{
cout<< i<< "\t"<< j << "\t" << k << "\t\t";

if( (i == 1 && j == 1) || k == 1)
cout<< 0;
else
cout<< 1;

cout<<"\n";
}

getch();

}

Write a program that reads an array of strings and count the number of strings of each length that occurs, as well as the total number of strings. The program should then print the mean (average) entirely of (lowercase and lowercase) letters and is terminated by '.'.


#include<iostream.h>
#include<conio.h>
#include <string.h>
#include<stdio.h>


void main()
{
clrscr();


char str[255] = " Hi \n Enter Strings \n Thanks ";


cout<< "\n Enter a String (~ to terminate ): ";
cin.getline(str, 255, '~');


/* Count no. of strings */
int no_f_strings = 0;
for(int i = 0; str[i] != '\0' ; i++)
   if(str[i] == '\n')
      no_f_strings ++;


/*Count total number of strings */
int tot_strings = 0;
for( i = 0 ; str[i] != '\0' ; i++)
{

/*space or new-line or fullstop means new word starts */


if( str[i] == ' ' || str [i] == '\n' || str[i] == '.')
  {tot_strings++;
  while(str[i] == ' ')
   i++;}
 if(str[i] == '\0')
   i--;
}
tot_strings++; //for the last word in the string array


cout<< "\n The  number of strings of each length : " << no_f_strings;


cout<<"\n The total number of strings (words) : " << tot_strings;


int len = strlen(str);


int ucas = 0, lcas = 0, fullst = 0;


for( i=0; str[i] != '\0' ; i++)
{
if(str[i]>='A' && str[i]<='Z')
  ucas++;


if(str[i]>='a' && str[i]<='z')
 lcas++;


if(str[i]=='.')
fullst++;


}


float avg_ucas = (float) (ucase / len);
float avg_lcas = (float) (lcas/len);


cout<< "\n The Mean  of lowercase letters in strings of array  = " <<  avg_lcas;

cout<< "\n The Mean  of uppercase letters in strings of array = " << avg_ucas;


cout<< "\n The total number of  terminating fullstop= " << fullst;



getch();
}

Write a C++ program that uses a function smallo() (that is passed two int argument by value) to receive reference of the smaller value. Then using this reference the smaller value is set to 0. Write a main() function also to exercise this function.

/* This is a example of return by reference */ 


#include<iostream.h>
#include<conio.h>

int &smallo(int a, int b)
{
if(a>b)
  return a;
else
  return b;
}


void main()
{
int a, b;
cout<< "\n Enter two numbers :";
cin >> a >> b;

cout<< "\n The original values :" << a << "  and " << b;

smallo(a, b) = 0;

cout<< "\n The changed values :" << a << " and " << b;



getch();
}

Wednesday, February 24, 2010

WAP to calculate the H.C.F and L.C.M of three numbers.

eg.  H.C.F of 11,22,33 is 11
      L.C.M of 11,22,33 is 66

-------------------------------------

     H.C.F of 2,3,4 is 1
     L.C.M of 2,3,4 is 24

#include<iostream.h>
#include<conio.h>
void main()
{
int a, b, c; // three numbers
int hcf,lcm;

//calculate the minimum of three numbers
int min = (a < b ? a : b);
min = (min < c ? min : c);

for( int i = min; i>0; i--)
{
if(a%i == 0 && b%i == 0 && c%i == 0)
{
  hcf = i;
  break;
  }
}

lcm = (a*b*c)/hcf;

cout<<" \n The H.C.F of " << a <<" , " << b << " , " << c << "is " << hcf;

cout<<" \n The L.C.M of " << a <<" , " << b << " , " << c << "is " << lcm;

getch();

}  

/*  Similarly you can calculate HCF of 2 numbers or 4 numbers and so on. */

WAP to print the highest and the lowest digit present in the number input by user.



#include<iostream.h>
#include<conio.h>

void main()
{
int n;

cout<< "\n Enter a number ";
cin>> n;

int num = n;

int high = n%10, low = n%10, rem;

n = n/10;

for(int i = n; i>=1; i=i/10)
{
rem =  i%10;

if(rem>high)
{
high = rem;
}

if(rem < low)
{
low = rem;
}

}

cout<< "\n The highest digit of "<< num <<" is " << high;
cout<< "\n The lowest digit of "<< num <<" is " << low;

getch();

}

WAP to print the product components of a number without repeating them.

eg.  No? 24
output :
24 * 1
12 * 2
8*3
4*6

#include<iostream.h>
#include<conio.h>
void main()
{
int n, s = 1;

cout<<"\n Enter the number to calculate it's product component:";
cin>>n;

clrscr();

cout<<"\n The product Components of "<< n << " is\n";

for(int i = n; i> = s ; i--)
for(int j = 1; j<=n; j++)
{
if(i*j == n)
{
cout<< i << "*" << j << endl;
s = j+1;
}
}


getch();
}

Tuesday, February 23, 2010

Write a C++ program that reads three strings and prints the longest and smallest string.


#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
char str1[30], srt2[30], str3[30];

cout<<"\n Enter 3 strings /Words one-by-one(press enter each time) :";
gets(str1);
gets(str2);
gets(str3);

/* Find Longest String */
if(strcmp(str1, str2) > 0)
{
   if(strcmp(str1,str3)>0)
       cout <<"\n The Longest string is : " << str1;
  else
       cout <<"\n The Longest string is : " << str3;
}
else
{
if(strcmp(str2,str3)>0)
cout <<"\n The Longest string is : " << str2;
else
cout <<"\n The Longest string is : " << str3;
}


/* Find smallest String */


if(strcmp(str1, str2) < 0)

{
if(strcmp(str1,str3)<0)
cout <<"\n The Smallest string is : " << str1;
else
cout <<"\n The Smallest string is : " << str3;
}
else
{
if(strcmp(str2,str3)<0)
cout <<"\n The Smallest string is : " << str2;
else
cout <<"\n The Smallest string is : " << str3;
}

getch();
}

Monday, February 22, 2010

Check Null Matrix

/* All elements are zero */


#include<iostream.h>
#include<conio.h>
void main()
{
int mat[10][10];
cout<< "\n Enter dimension of square matrix;";

 int n;
cin>>n;

cout<< "\n Enter the elements for the matrix :";
 for(int i=0; i< n ; i++)
for (int j = 0; j< n ; j++)
 { cout<<"\n Element for positon : " << i+1 << " , " << j+1 << ":";


cin>> mat[i][j];
}


int flag = 0;
for(int i=0; i< n ; i++)
for (int j = 0; j< n ; j++)
{ if(mat[i][j] != 0)
{ flag = 1;
break; }
}

if(flag == 0)
cout<< "\n Null Matrix .";
else
cout<<"\n Not a Null Matrix.";

 cout<<"\n\n Matrix :";
for(int i=0; i< n ; i++)
{for (int j = 0; j< n ; j++)
 cout<< mat[i][j] << " ";
 cout<< endl;
}
getch();
}

Sunday, February 21, 2010

Write a C++ program to use following functions:

(i) sqlarge() that is passed two int arguments by reference and then sets the larger of the two numbers to its square.



(ii) sum() that is passed an int argument by value  and that returns the sum of the individual digits of the passed number.



(iii) main() that exercises above two functions by getting two integers from the user and by printing the sum of the individual digits of the square of the larger number.



#include<iostream.h>
#include<conio.h>
void sqlarge(int &a, int &b)
{
if(a >b)
   a = a*a;
else
   b = b*b;
}


int sum(int x);
{
int r, s=0;
while(x > 0)
{
r = x % 10;
s = s+r;
x = x / 10;
}


return s;
}




void main()
{
int num1, num2;


cout<<" \ n Enter a number :";


int tot = sum(num1);


cout<< " \n THE SUM OF DIGITS = " << tot;


cout << "\n Enter another number : ";
cin>> num2;


cout<< "\n The two numbers originally are " << num1 << " and " << num2 ;


sqlarge(num1, num2);


cout<< "\n The two numbers after change are " << num1 << " and " << num2 ;


getch();


}

Do the following :


Write a complete C++ program that invokes a function satis() to find whether four integers a,b,c,d sent to satis() satisfy the equation a3+b3+c3 = d3 or not. The function satis() returns 0 if the above equation is satisfied with the given four numbers otherwise it returns -1



#include<iostream.h>
#include<conio.h>
int satis(int a, int b, int c, int d)
{
if( a*a*a + b*b*b + c*c*c == d*d*d)
return 0;
else
return -1;
}
void main()
{
cout<< "\n Enter 4 integers : ";
int x, y, z, w;
int s = satis( x,y,z,w);
if(s == 0)
cout<< "\n The equation is satisfied";
else
cout<< "\n The equation is NOT satisfied";
getch();
}

Write a program that uses a function power() to raise a number m to power n. The function takes int values for m and n and returns the result correctly. Use a default value of 2 for n to make the function calculate square of m when the value for n is missing while calling


#include<iostream.h>
#include<conio.h>
#include<math.h>


double power(int m, int n = 2)
{
double res = pow(m,n);
return res;
}


void main()
{
int x;

cout<< "\n Enter a number to calculate its square :";
double d = power(x);
cout<< "\n The square of " << x <<"is " << d;


cout<< "\n Enter a number to calculate its cube :";

 d = power(x,3);
cout<< "\n The cube of " << x <<"is " << d;



int y;
cout<< "\n Enter a number  and its power to calculate the result :";
cin>>x>>y;

d = power(x,y);
cout<< "\n The " << x << " to the power " << y <<" is " << d;
getch();
}

Saturday, February 20, 2010

Write a C++ program to use the following function :

(i) display() to display a matrix of a size m x n.
(ii) times2() to double each number of the matrix of size mxn.
(iii) main() to read a matrix of size mxn and then to display original matrix and then to display the new matrix formed by doubling its elements.


#include<iostream.h>
#include<conio.h>
void display(int a[][], int m, int n)
{
for(int i = 0; i< m ; i++)
 {
  for( int j = 0; j< n ; j++)
        cout << a[i][j] << "  ";
   cout<< endl;
 }
}

void times2(int a[][], int m, int n)
{
for(int i = 0; i< m ; i++)

  for( int j = 0; j< n ; j++)
     a[i][j] = a[i][j] * 2;


}


void main()
{
int mat[10][10], row, col, i, j;

cout<< "\n Enter total rows :";
cin>> row;

cout<<" \n Enter total columns :";
cin>> col;

cout<< "\n Enter the elements for Matrix:";

for(i = 0; i< row; i++)

  for(j = 0; j< col; j++)
     cin>>mat[i][j];


cout<<"\n The Original Matrix :";
display(mat, row, col);


times2(mat, row, col);


cout<<"\n The New Matrix :";


display(mat, row, col);


getch();
}

Write a complete C++ program that reads a float array having 15 elements. The Program uses a function reverse() to reverse this array. Make suitable assumption.

/* This example is also demonstrating function with call by reference  */
/* Whenever you pass an array through function, it will be done only by reference */


#include<iostream.h>
#include<conio.h>

void rev(float a[ ], int n)
{
float t;
for(int i =0, k = n-1; i< n/2 ; i++, k--)
{
t = a[i];
a[i] = a[k];
a[k] = t;
}
}


void main()
{
float arr[15];

cout<< "\n Enter 15 real numbers :";
for(int i = 0; i< 15 ; i++)
cin>> arr[i];

clrscr();
cout<<"\n The original array : \n";
for( i = 0; i< 15; i++)
cout<< arr[i] << "  ";

cout<<"\n";

rev(arr, 15);

cout<<"\n The reversed array : \n";

for( i = 0; i< 15; i++)
 cout<< arr[i] << " ";

getch();
}

Write a complete C++ program that uses a function called carea() to calculate area of a circle. The function carea() receives radius of float type and returns area of double type. The function main() gets a radius value from the user, calls carea(), and displays the result. The function carea() is local to main.


#include<iostream.h>
#include<conio.h>


void main()
{
double carea(float r)
{
double ar = (double) ( 3.14 * r * r );
return ar;
}


float rad;


cout<<  " \n Enter Radius :";
cin >>rad;


double area = carea(rad);


cout<<"\n The area of the circle of radius " << rad << " unit is " << area << "sq. unit ";


getch();


}

Write a function that checks whether or not two arrays (of characters ) are identical, that is, whether they have same characters and all characters in corrosponding positions are equal.


#include<iostream.h>
#include<conio.h>
#include <stdio.h>
#include <string.h>


void main()
{
char sen1[50], sen2[50];
void chk_eq(char str1[], char str2[]);
cout<< "\n Enter String1 :";
gets(sen1);


cout<<"\n Enter String 2:";
gets(sen2);


chk_eq(sen1, sen2);


}


void chk_eq(char str1[], char str2[])
{
int l1,l2;


l1 = strlen(str1);
l2 = strlen(str2);


if(l1 == l2)
{
cout<<" \ n Two String are NOT equal ";
getch();
exit(1);
}


for( int i = 0; i<= l1 - 1 ; i++)
{
  if(str1[i] ! = str2[i])
   {


     cout<<" \ n Two String are NOT equal ";
     getch();
     exit(1);
   }
}


cout<< "\n Two Strings are EQUAL lengthwise , characterwise and casewise ";
getch();
}

Thursday, February 18, 2010

Product of Matrices


#include<iostream.h>
#include<conio.h>
#include<process.h>



void main()
{
clrscr();
int A[10][10] , B[10][10] , P[10][10] , i, j, k, m, n, p, q;

cout<< "\n Enter the rows & column of Matrix A :;
cin>> m>> n;

cout<< "\n Enter the rows & column of Matrix  B :;
cin>> p>> q;

if(n ! = p)
{
cout<< "\n The Matrix Multiplication is impossible .";
 getch();
exit(1);
}

cout<< " \ n  Enter the elements for Matrix A :";
for(i = 0; i< m ; i++)

for(j = 0 ; j< n ; j++)
   cin >> A[i][j];

cout<< " \ n Enter the elements for Matrix B :";

for(i = 0; i< p ; i++)
for(j = 0 ; j< q ; j++)
  cin >> B[i][j];




/* ......Product ...... */

for(i = 0; i< m ; i++)
for(j = 0 ; j< q ; j++)
{


P[i][j] = 0;


for(k = 0; k< n ; k++)


P[i][j] = P[i][j] + A[i][k] * B[k][j];


}



cout<< "\n Matrix A : \n ";
for( i =0; i< m ; i++)
{ for(j = 0; j< n ; j++)
     cout<< A[i][j];
 cout<< endl;
}


cout<< "\n Matrix B: \n ";



for( i =0; i< p ; i++)
{ for(j = 0; j< q ; j++)
     cout<< B[i][j];
  cout<< endl;
}
cout<< "\n Product AXB : \n ";
for( i =0; i< m ; i++)
  { for(j = 0; j< q ; j++)
       cout<< P[i][j];
    cout<< endl;
  }


getch();

}

Write a program to obtain the product of the following matrices:

           5    9    -2
A =      8    0    5
           0    2    8




          2  4
B =     0   1
          1  3


A x B =      8     23
                21    47
                8     26
          


#include<iostream.h>
#include<conio.h>
#include<process.h>


void main()
{
clrscr();


int A[3][3] = { 5, 9, -2,
                     8,0,5 ,
                     0,2, 8 };


int B[3][2] = { 2,4 ,
                      0 , 1,
                      1,3 } ;


int P[3][2] , i, j, k;


/*  ......Product ...... */


for(i = 0; i< 3 ; i++)
for(j = 0 ; j< 2 ; j++)
{
  P[i][j] = 0;
for(k = 0; k< n ; k++)
 P[i][j] = P[i][j] + A[i][k] * B[k][j];
}




cout<< "\n Matrix A : \n ";


for( i =0; i< 3 ; i++)
{  for(j = 0; j< 3 ; j++)
       cout<<  A[i][j];
   cout<< endl;
}


cout<< "\n Matrix B: \n ";




for( i =0; i< 3 ; i++)
{ for(j = 0; j< 2 ; j++)
     cout<<  B[i][j];
cout<< endl;
}


cout<< "\n  Product AXB  : \n ";




for( i =0; i< 3 ; i++)
{ for(j = 0; j< 2 ; j++)
   cout<< P[i][j];
cout<< endl;
}


getch();


}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.