C Program on Number Theory


Numbers can be represented in many systems like base 2, base 10 etc,. In this article, there is a C program which can be used to perform arithmetic on same base numbers and conversion of a number from one base to another and also logical operations on two binary numbers.

Introduction to Base System


Base Systems from 2 to 36 are defined. The digits available in different base systems are as follows.
Base 2 : 0, 1
Base 3 : 0, 1, 2
Base 4 : 0, 1, 2, 3
Base 5 : 0, 1, 2, 3, 4
Base 6 : 0, 1, 2, 3, 4, 5
Base 7 : 0, 1, 2, 3, 4, 5, 6
Base 8 : 0, 1, 2, 3, 4, 5, 6, 7
Base 9 : 0, 1, 2, 3, 4, 5, 6, 7, 8
Base 10 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Base 11 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A
Base 12 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B
Base 13 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C
Base 14 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D
Base 15 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E
Base 16 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
… … …
… … …
Base 35 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, … X, Y
Base 36 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, … X, Y, Z
In the above mentioned base systems, the commonly used ones are base 2 also called as binary system, base 8 also called as octal system, base 10 also called as decimal system and base 16 also called as hexadecimal system. The C Program below can be used to convert a number from one base to another. Also, you can perform arithmetic on numbers of same base (can vary from 2 to 36) or you can apply logical operators like AND, OR etc., on two bits using this program.

Program


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
typedef double val;
void basevarto10_num(val a,val *b)
{
char C[10],F[10];
val c,d,e,f,g,h,i,j;
val A[10],B[10],D[10],E[10];
printf("\n INTEGER PART \n");
printf("\n Enter {number} of digits present:");
scanf("%lf",&c);
for(d=0,A[d]=10,B[d]=0,C[d]='\0';d<c;d++)
{
printf("\n Is digit (%.0lf) from right (1) Number or (2) Letter:",d+1);
scanf("%lf",&e);
if(e==1)
{
printf("\n Enter it:");
scanf("%lf",&A[d]);
B[d]=A[d];
}
else
{
printf("\n Enter it:");
C[d]=getche();
printf("\n ");
B[d]=toascii(C[d])-55;
}
}
for(d=0,f=0;d<c;d++)
f+=B[d]*pow(a,d);
printf("\n FRACTIONAL PART \n");
printf("\n Enter {precision} value present:");
scanf("%lf",&g);
for(d=0,D[d]=10,E[d]=0,F[d]='\0';d<g;d++)
{
printf("\n Is digit (%.0lf) from left (1) Number or (2) Letter:",d+1);
scanf("%lf",&e);
if(e==1)
{
printf("\n Enter it:");
scanf("%lf",&D[d]);
E[d]=D[d];
}
else
{
printf("\n Enter it:");
F[d]=getche();
printf("\n ");
E[d]=toascii(F[d])-55;
}
}
for(d=0,h=0;d<g;d++)
h+=E[d]/pow(a,d+1);
i=f+h;
printf("\n SIGN \n");
printf("\n Is the number (1) Non negative or (2) Negative:");
scanf("%lf",&j);
i*=(j==1)?(+1):(-1);
*b=i;
}
void base10tovar_num(val a,val b,val c)
{
char C[10],F[10];
val d,e,f,g,h,i,j;
val A[10],B[10],D[10],E[10];
d=floor(fabs(a));
for(e=0,A[e]=10,B[e]=0,C[e]='\0';d!=0;e++)
{
f=(long int)d%(long int)b;
if(f<10)
{
A[e]=f;
B[e]=A[e];
}
else
{
if(f==10)
C[e]='A';
else if(f==11)
C[e]='B';
else if(f==12)
C[e]='C';
else if(f==13)
C[e]='D';
else if(f==14)
C[e]='E';
else if(f==15)
C[e]='F';
else if(f==16)
C[e]='G';
else if(f==17)
C[e]='H';
else if(f==18)
C[e]='I';
else if(f==19)
C[e]='J';
else if(f==20)
C[e]='K';
else if(f==21)
C[e]='L';
else if(f==22)
C[e]='M';
else if(f==23)
C[e]='N';
else if(f==24)
C[e]='O';
else if(f==25)
C[e]='P';
else if(f==26)
C[e]='Q';
else if(f==27)
C[e]='R';
else if(f==28)
C[e]='S';
else if(f==29)
C[e]='T';
else if(f==30)
C[e]='U';
else if(f==31)
C[e]='V';
else if(f==32)
C[e]='W';
else if(f==33)
C[e]='X';
else if(f==34)
C[e]='Y';
else
C[e]='Z';
B[e]=toascii(C[e])-55;
}
d=(long int)d/(long int)b;
}
g=e;
h=fabs(a)-floor(fabs(a));
for(e=0,i=floor(b*h),j=(b*h)-floor(b*h),D[e]=10,E[e]=0,F[e]='\0';e<c;e++)
{
if(i<10)
{
D[e]=i;
E[e]=D[e];
}
else
{
if(i==10)
F[e]='A';
else if(i==11)
F[e]='B';
else if(i==12)
F[e]='C';
else if(i==13)
F[e]='D';
else if(i==14)
F[e]='E';
else if(i==15)
F[e]='F';
else if(i==16)
F[e]='G';
else if(i==17)
F[e]='H';
else if(i==18)
F[e]='I';
else if(i==19)
F[e]='J';
else if(i==20)
F[e]='K';
else if(i==21)
F[e]='L';
else if(i==22)
F[e]='M';
else if(i==23)
F[e]='N';
else if(i==24)
F[e]='O';
else if(i==25)
F[e]='P';
else if(i==26)
F[e]='Q';
else if(i==27)
F[e]='R';
else if(i==28)
F[e]='S';
else if(i==29)
F[e]='T';
else if(i==30)
F[e]='U';
else if(i==31)
F[e]='V';
else if(i==32)
F[e]='W';
else if(i==33)
F[e]='X';
else if(i==34)
F[e]='Y';
else
F[e]='Z';
E[e]=toascii(F[e])-55;
}
i=floor(j*b);
j=(j*b)-floor(j*b);
}
if(a>=0)
printf("{+");
else
printf("{-");
for(e=g-1;e>=0;e--)
{
if(B[e]>9)
putchar(C[e]);
else
printf("%.0lf",A[e]);
}
if(c!=0)
{
printf(".");
for(e=0;e<c;e++)
{
if(E[e]>9)
putchar(F[e]);
else
printf("%.0lf",D[e]);
}
}
printf("} in base(%.0lf) ",b);
}
void logicaloperator(val a,val b,val c,val *d)
{
if(c!=1&&c!=2&&c!=3&&c!=4&&c!=5&&c!=6&&c!=7)
printf("\n SYNTAX ERROR:TRY AGAIN \n");
else
{
if(c==1)
*d=(a==0)?((b==0)?(0):(0)):((b==0)?(0):(1));
else if(c==2)
*d=(a==0)?((b==0)?(0):(1)):((b==0)?(1):(1));
else if(c==3)
*d=(a==0)?(1):(0);
else if(c==4)
*d=(a==0)?((b==0)?(1):(1)):((b==0)?(1):(0));
else if(c==5)
*d=(a==0)?((b==0)?(1):(0)):((b==0)?(0):(0));
else if(c==6)
*d=(a==0)?((b==0)?(0):(1)):((b==0)?(1):(0));
else
*d=(a==0)?((b==0)?(1):(0)):((b==0)?(0):(1));
}
}
void main()
{
val choice1,choice2,a,b,c,d,e,f;
for(choice1=0;choice1<=7;choice1++)
{
printf("\n 1.(A) in base(m) -> (B) in base(n) ");
printf(" 2.(A)+(B) ");
printf(" 3.(A)-(B) ");
printf("\n\n 4.(A)*(B) ");
printf(" 5.(A)/(B) ");
printf(" 6.Logical Operators(BINARY) ");
printf(" 7.Off ");
printf("\n\n Enter your choice:");
scanf("%lf",&choice1);
if(choice1==1)
{
printf("\n Enter base of number (A):");
scanf("%lf",&a);
printf("\n Enter base of number (B):");
scanf("%lf",&b);
if(a==floor(a)&&b==floor(b)&&a>=2&&a<=36&&b>=2&&b<=36)
{
printf("\n Enter number (A): \n");
basevarto10_num(a,&c);
printf("\n ");
base10tovar_num(c,a,6);
printf("is ");
base10tovar_num(c,b,6);
printf("\n\n ");
}
else
printf("\n TRANSFORMATION IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice1==2)
{
printf("\n Enter base of number (A):");
scanf("%lf",&a);
printf("\n Enter base of number (B):");
scanf("%lf",&b);
if(a==floor(a)&&b==floor(b)&&a>=2&&a<=36&&b>=2&&b<=36&&a==b)
{
printf("\n Enter number (A): \n");
basevarto10_num(a,&c);
printf("\n Enter number (B): \n");
basevarto10_num(b,&d);
e=c+d;
printf("\n Number (A) is ");
base10tovar_num(c,a,6);
printf("\n\n Number (B) is ");
base10tovar_num(d,b,6);
printf("\n\n Number (A)+(B) is ");
base10tovar_num(e,a,6);
printf("\n\n ");
}
else
printf("\n Number (A)+(B) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice1==3)
{
printf("\n Enter base of number (A):");
scanf("%lf",&a);
printf("\n Enter base of number (B):");
scanf("%lf",&b);
if(a==floor(a)&&b==floor(b)&&a>=2&&a<=36&&b>=2&&b<=36&&a==b)
{
printf("\n Enter number (A): \n");
basevarto10_num(a,&c);
printf("\n Enter number (B): \n");
basevarto10_num(b,&d);
e=c-d;
printf("\n Number (A) is ");
base10tovar_num(c,a,6);
printf("\n\n Number (B) is ");
base10tovar_num(d,b,6);
printf("\n\n Number (A)-(B) is ");
base10tovar_num(e,a,6);
printf("\n\n ");
}
else
printf("\n Number (A)-(B) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice1==4)
{
printf("\n Enter base of number (A):");
scanf("%lf",&a);
printf("\n Enter base of number (B):");
scanf("%lf",&b);
if(a==floor(a)&&b==floor(b)&&a>=2&&a<=36&&b>=2&&b<=36&&a==b)
{
printf("\n Enter number (A): \n");
basevarto10_num(a,&c);
printf("\n Enter number (B): \n");
basevarto10_num(b,&d);
e=c*d;
printf("\n Number (A) is ");
base10tovar_num(c,a,6);
printf("\n\n Number (B) is ");
base10tovar_num(d,b,6);
printf("\n\n Number (A)*(B) is ");
base10tovar_num(e,a,6);
printf("\n\n ");
}
else
printf("\n Number (A)*(B) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice1==5)
{
printf("\n Enter base of number (A):");
scanf("%lf",&a);
printf("\n Enter base of number (B):");
scanf("%lf",&b);
if(a==floor(a)&&b==floor(b)&&a>=2&&a<=36&&b>=2&&b<=36&&a==b)
{
printf("\n Enter number (A): \n");
basevarto10_num(a,&c);
printf("\n Enter number (B): \n");
basevarto10_num(b,&d);
if(d!=0)
{
e=c/d;
printf("\n Number (A) is ");
base10tovar_num(c,a,6);
printf("\n\n Number (B) is ");
base10tovar_num(d,b,6);
printf("\n\n Number (A)/(B) is ");
base10tovar_num(e,a,6);
printf("\n\n ");
}
else
{
printf("\n Number (A) is ");
base10tovar_num(c,a,6);
printf("\n\n Number (B) is ");
base10tovar_num(d,b,6);
printf("\n\n Number (A)/(B) IS UNDEFINED:TRY AGAIN \n\n ");
}
}
else
printf("\n Number (A)/(B) IS UNDEFINED:TRY AGAIN \n\n ");
}
else if(choice1==6)
{
printf("\n LOGICAL OPERATORS(BINARY) \n\n");
printf(" 1.AND ");
printf(" 2.OR ");
printf(" 3.NOT ");
printf(" 4.NAND ");
printf(" 5.NOR ");
printf(" 6.XOR ");
printf(" 7.XNOR ");
printf("\n\n Enter your choice:");
scanf("%lf",&choice2);
if(choice2==1)
{
printf("\n Enter {first,second} bits:");
scanf("%lf,%lf",&a,&b);
if(a!=0&&a!=1)
printf("\n (%lf) AND (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
if(b!=0&&b!=1)
printf("\n (%lf) AND (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
logicaloperator(a,b,1,&c);
printf("\n (%.0lf) AND (%.0lf) = {%.0lf} \n\n ",a,b,c);
}
}
}
else if(choice2==2)
{
printf("\n Enter {first,second} bits:");
scanf("%lf,%lf",&a,&b);
if(a!=0&&a!=1)
printf("\n (%lf) OR (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
if(b!=0&&b!=1)
printf("\n (%lf) OR (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
logicaloperator(a,b,2,&c);
printf("\n (%.0lf) OR (%.0lf) = {%.0lf} \n\n ",a,b,c);
}
}
}
else if(choice2==3)
{
printf("\n Enter {first} bit:");
scanf("%lf",&a);
if(a!=0&&a!=1)
printf("\n NOT (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a);
else
{
logicaloperator(a,2,3,&b);
printf("\n NOT (%.0lf) = {%.0lf} \n\n ",a,b);
}
}
else if(choice2==4)
{
printf("\n Enter {first,second} bits:");
scanf("%lf,%lf",&a,&b);
if(a!=0&&a!=1)
printf("\n (%lf) NAND (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
if(b!=0&&b!=1)
printf("\n (%lf) NAND (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
logicaloperator(a,b,4,&c);
printf("\n (%.0lf) NAND (%.0lf) = {%.0lf} \n\n ",a,b,c);
}
}
}
else if(choice2==5)
{
printf("\n Enter {first,second} bits:");
scanf("%lf,%lf",&a,&b);
if(a!=0&&a!=1)
printf("\n (%lf) NOR (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
if(b!=0&&b!=1)
printf("\n (%lf) NOR (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
logicaloperator(a,b,5,&c);
printf("\n (%.0lf) NOR (%.0lf) = {%.0lf} \n\n ",a,b,c);
}
}
}
else if(choice2==6)
{
printf("\n Enter {first,second} bits:");
scanf("%lf,%lf",&a,&b);
if(a!=0&&a!=1)
printf("\n (%lf) XOR (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
if(b!=0&&b!=1)
printf("\n (%lf) XOR (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
logicaloperator(a,b,6,&c);
printf("\n (%.0lf) XOR (%.0lf) = {%.0lf} \n\n ",a,b,c);
}
}
}
else if(choice2==7)
{
printf("\n Enter {first,second} bits:");
scanf("%lf,%lf",&a,&b);
if(a!=0&&a!=1)
printf("\n (%lf) XNOR (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
if(b!=0&&b!=1)
printf("\n (%lf) XNOR (%lf) IS UNDEFINED:TRY AGAIN \n\n ",a,b);
else
{
logicaloperator(a,b,7,&c);
printf("\n (%.0lf) XNOR (%.0lf) = {%.0lf} \n\n ",a,b,c);
}
}
}
else
printf("\n {%lf} IS NOT A VALID CHOICE:TRY AGAIN \n\n ",choice2);
}
else if(choice1==7)
{
printf("\n ");
exit(0);
}
else
{
printf("\n {%lf} IS AN INVALID CHOICE:TRY AGAIN \n\n ",choice1);
choice1=0;
}
}
getch();
}

Explanation


Operation you want to perform can be selected by choosing the corresponding 'choice1' value. There are 3 functions in which the first and second are used in conversion of a number between base 10 and any other base (in 2 to 36 obviously) and the third is used to perform logical operations. Numbers are scanned in input base value, converted to base 10 and then converted to output base value by the program. The same principle works for arithmetic operations also. But for logical operations on two bits, only the third function is used. This program never gives error output and in addition, it displays an error message if error inputs are given.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: