Loading...

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);
}






}

No comments:

Post a Comment