Thursday, January 31, 2013

ADDITION OF TWO NUMBERS IN C++ USING FRIEND FUNCTION

/* FRIEND FUNCTION DEMO */


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

class two;
class one
{
private:int data1;
public: void setdata(int init)
{
data1=init;
}
friend int add_both(one a,two b);

};//class

class two
{
private: int data2;
public:
void setdata(int init)
{
data2=init;
}
friend int add_both(one a,two b);
};
int add_both(one a,two b)
{
return a.data1+b.data2;
}
void main()
{
one a;
two b;

int i,j;
clrscr();
cout<<"Enter the value of a & b";
cin>>i>>j;

a.setdata(i);
b.setdata(j);
cout<<"sum of one & two :"<<add_both(a,b);
getch();
}

Monday, October 1, 2012

Star Pattern in C language


#include<stdio.h>
#include<conio.h>
void main()
{
int b=2,c=1,i,p,a;
clrscr();
for(i=0;i<3;i++)
{
for(a=0;a<b;a++)
{
printf(" ");
}//for spacing
for(p=0;p<c;p++)
{
printf(" *");
}//for printing
for(a=0;a<b;a++)
{
printf(" ");
}//for spacing
for(p=0;p<c;p++)
{
printf(" *");
}//for printing
for(a=0;a<b;a++)
{
printf(" ");
}//for spacing
printf("\n");
b--;
c++;

}//outer for loop
getch();
}//eofm

Wednesday, August 29, 2012

c program to reverse a number

#include <stdio.h>
 
main()
{
   int n, reverse = 0;
 
   printf("Enter a number to reverse\n");
   scanf("%d",&n);
 
   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }
 
   printf("Reverse of entered number is = %d\n", reverse);
 
   return 0;
}

Decimal to binary conversion

#include <stdio.h>
 
int main()
{
  int n, c, k;
 
  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);
 
  printf("%d in binary number system is:\n", n);
 
  for (c = 31; c >= 0; c--)
  {
    k = n >> c;
 
    if (k & 1)
      printf("1");
    else
      printf("0");
  }
 
  printf("\n");
 
  return 0;
}

Palindrome number program c

Palindrome number algorithm

1. Get the number from user.
2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are same then print palindrome number
5. Else print not a palindrome number.
 -----------------------------------------------------------
#include<stdio.h>
 
main()
{
   int n, reverse = 0, temp;
 
   printf("Enter a number to check if it is a palindrome or not\n");
   scanf("%d",&n);
 
   temp = n;
 
   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }
 
   if ( n == reverse )
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d is not a palindrome number.\n", n);
 
   return 0;
}

c program to print pattern stars

 
#include<stdio.h>
 
main()
{
   int row, c, n, temp;
 
   printf("Enter the number of rows in pyramid of stars you wish to see ");
   scanf("%d",&n);
 
   temp = n;
 
   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");
 
      temp--;
 
      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");
 
      printf("\n");
   }
 
   return 0;