Monday, January 17, 2011

PROGRAM : DECIMAL TO BINARY CONVERSION

ASALAM WALAKUM,


WELL IT IS HERE IS ANOTHER PROGRAM FOR THE CONVERSION FROM DECIMAL TO BINARY.I HAVE USED A RECURSIVE FUNCTION . ENJOY


REGARDS,
NABEEL




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


void dec(int);


void main()
{
      clrscr();
      int num=0;


      printf("ENTER A DECIMAL NUMBER :\t");
      scanf("%d",&num);


      printf("THE BINARY EQUIVALENT OF %d IS ",num);


      dec(num);
      getch();


}




void dec(int num)
{
      int remainder;


      if(num==1)
      printf("%d",num);


      else
      {
      remainder=num%2;
      num=num/2;
      dec(num);
      printf("%d",remainder);
      }


}

PROGRAM : BINARY TO DECIMAL CONVERSION

ASALAM WALAKUM,

         Well this is a Program to convert a Binary Number input by the user to a Decimal Number. It is a very simple one without an array , which I personally think adds to the efficiency of my Programming skills :D.


Regards,
Nabeel
     
#include<stdio.h>
#include<conio.h>
#include<math.h>


void main()
{
      clrscr();
      int num=0,sum=0,digit=0,temp=0,a=0;


      printf("ENTER A BINARY NUMBER :\t");
      scanf("%d",&num);


      int result=num;


      while(num!=0)
      {
      digit=num%10;
      temp=digit*pow(2,a);
      sum+=temp;
      num=num/10;
      a++;
      }


      printf("THE DECIMAL EQUIVALENT OF %d IS %d ",result,sum);


      getch();


}



Thursday, January 13, 2011

PROGRAM : MATH FUNCTIONS

ASALAM WALAKUM,


   This program consists of some basics math functions for a number . I will add to it in the future .


 Regards
 Nabeel




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


char oddeven(unsigned long);   // FUNCTION PROTOTYPE OF AN ODD & EVEN FUNCTION //
char primcomp(unsigned long);  // FUNCTION PROTOTYPE OF A PRIME & COMPOSITE FUNCTION //
char sumDigits(unsigned long); // FUNCTION PROTOTYPE FOR THE SUM OF DIGITS FUNCTION //
char multiple(unsigned long);  // FUNCTION PROTOTYPE FOR THE FACTORS FUNCTION //


void main()
{


unsigned long num=0;
clrscr();


printf("\n\n\tENTER THE DESIRED NUMBER : ");
scanf("%d",&num);




printf("\n\n\tPLEASE CHOOSE ONE OF THE FOLLOWING AS PER YOUR WISH....\n\n");
printf("\t 1. TO CHECK IF THE NUMBER IS ODD OR EVEN\n");
printf("\t 2. TO CHECK IF THE NUMBER IS PRIME OR COMPOSITE\n");
printf("\t 3. TO CHECK IF THE SUM OF DIGITS OF THE SELECTED NUMBER \n\t\t\tIS ODD OR EVEN\n");
printf("\t 4. TO CHECK THE FACTORS OF THE SELECTED NUMBER \n\t");


switch(getch())
{


  case '1':
  oddeven(num);   // FUNCTION CALL FOR AN ODD & EVEN RECOGNIZATION //
  break;


  case '2':
  primcomp(num);  // FUNCTION CALL FOR A PRIME & COMPOSITE RECOGNIZATION //
  break;


  case '3':
  sumDigits(num);  // FUNCTION CALL FOR THE SUM OF DIGITS FUNCTION //
  break;


  case '4':
  multiple(num);  // FUNCTION CALL FOR THE FACTORS FUNCTION //
  break;
}


getch();


}


char oddeven(unsigned long num) // FUNCTION HEADER FOR AN ODD & EVEN RECOGNIZATION //
{
if (num%2==0)
    return printf("\n\tThe Number %ld is Even.",num);




else
    return printf("\n\tThe Number %ld is Odd.",num);


}


char primcomp(unsigned long num) // FUNCTION HEADER FOR A PRIME & COMPOSITE RECOGNIZATION //
{
unsigned long n=num,result;


for (unsigned long i=2; i<(n-1);i++)
{
   result=num%i;


if (result==0)
     return printf("\n\tThe Number %ld is composite.",num);
}
if (result!=0)
     return printf("\n\tThe Number %ld is prime.",num);


}


char sumDigits(unsigned long num)  // FUNCTION HEADER FOR THE SUM OF DIGITS FUNCTION //
{


unsigned long number=num,digits=0,sum=0;


while(number!=0)
{
      digits=number%10;   // FOR GETTING THE NUMBERS INDIVIDUALLY //
      sum+=digits;
      number=number/10;   /* FOR DELETING THE LAST NUMBER
OR
     FOR GETTING THE NEXT NUMBER */
}


if (sum%2==0)
return printf("\n\tThe Sum of Digits of %ld is Even.",num);


else
return printf("\n\tThe Sum of Digits of %ld is Odd.",num);


      /*
 I HAVE WRITTEN THESE STATEMENTS AGAIN DUE TO THE DIFFERENCE OF
 STATEMENTS IN THE oddeven FUNCTION .
 I COULD HAVE USED THE oddeven FUNCTION INSTEAD OF RE-WRITTING
 THESE STATEMENTS
*/
}


char multiple(unsigned long num)  // FUNCTION HEADER FOR THE SUM OF DIGITS FUNCTION //
{
int table;


printf("\n\n\tDO YOU WANT TO CHECK FOR ONLY ONE TABLE OR MANY....\n\n");
printf("\t1. SINGLE TABLE \n\t2. MANY TABLES");




switch (getch())
{
 case '1':


 printf("\n\nENTER THE TABLE :\t");
 scanf("%d",&table);


  if(num%table==0)
  return printf("\n\tThe Number %ld is a Factor of %d",num,table);


  else
  return printf("\n\tThe Number %ld is NOT a Factor of %d",num,table);




  case '2':


  printf("\n\n\tENTER THE LAST TABLES :\t");
  scanf("%d",&table);


  for(int a=1;a<=table;a++)
  {
if(num%a==0)
printf("%d,",a);
  }


printf("\b are the factors of %ld. ",num);
}






}