Practice Program
Solution: through While loop
void main()
{
int i,j=0,n;
int a[30];
printf("Enter the decimal number:");
scanf("%d",&n);
while(n!=0)
{
a[j] = n%16;//storing the remainder at every iteration as element in an array
n = n/16;//at every iteration we are dividing 'n' with 2
//and storing its quotient as a new value in of 'n' variable.
j++;
}
j = j-1;
printf("Hexadecimal Number is: ");
while(j>=0) //printing the hex form of a number.
{
if(a[j] == 10)
printf("%c",'A');
if(a[j] == 11)
printf("%c",'B');
if(a[j] == 12)
printf("%c",'C');
if(a[j] == 13)
printf("%c",'D');
if(a[j] == 14)
printf("%c",'E');
if(a[j] == 15)
printf("%c",'F');
else
printf("%d",a[j]);
j--;
}
}
void main() { int i,j=0,n; int a[30]; printf("Enter the decimal number:"); scanf("%d",&n); while(n!=0) { a[j] = n%16;//storing the remainder at every iteration as element in an array n = n/16;//at every iteration we are dividing 'n' with 2 //and storing its quotient as a new value in of 'n' variable. j++; } j = j-1; printf("Hexadecimal Number is: "); while(j>=0) //printing the hex form of a number. { if(a[j] == 10) printf("%c",'A'); if(a[j] == 11) printf("%c",'B'); if(a[j] == 12) printf("%c",'C'); if(a[j] == 13) printf("%c",'D'); if(a[j] == 14) printf("%c",'E'); if(a[j] == 15) printf("%c",'F'); else printf("%d",a[j]); j--; } }
Output1
Enter the decimal number: 12
Hexadecimal is: C
Hexadecimal is: C
Output2
Enter the decimal number: 32
Hexadecimal Number is: 20
Hexadecimal Number is: 20
Output3
Enter the decimal number: 44
Hexadecimal Number is: 2C
Hexadecimal Number is: 2C
Solution: from do-while loops
void main()
{
int i,j=0,n;
int a[30];
printf("Enter the decimal number:");
scanf("%d",&n);
do
{
a[j] = n%16;//storing the remainder at every iteration as element in an array
n = n/16;//at every iteration we are dividing 'n' with 2
//and storing its quotient as a new value in of 'n' variable.
j++;
}while(n!=0);
j = j-1;
printf("Octal Number is: ");
do
//printing the hexa decimal form of a number.
{
if(a[j] == 10)
printf("%c",'A');
if(a[j] == 11)
printf("%c",'B');
if(a[j] == 12)
printf("%c",'C');
if(a[j] == 13)
printf("%c",'D');
if(a[j] == 14)
printf("%c",'E');
if(a[j] == 15)
printf("%c",'F');
else
printf("%d",a[j]);
j--;
}while(j>=0);
}
void main() { int i,j=0,n; int a[30]; printf("Enter the decimal number:"); scanf("%d",&n); do { a[j] = n%16;//storing the remainder at every iteration as element in an array n = n/16;//at every iteration we are dividing 'n' with 2 //and storing its quotient as a new value in of 'n' variable. j++; }while(n!=0); j = j-1; printf("Octal Number is: "); do //printing the hexa decimal form of a number. { if(a[j] == 10) printf("%c",'A'); if(a[j] == 11) printf("%c",'B'); if(a[j] == 12) printf("%c",'C'); if(a[j] == 13) printf("%c",'D'); if(a[j] == 14) printf("%c",'E'); if(a[j] == 15) printf("%c",'F'); else printf("%d",a[j]); j--; }while(j>=0); }
defining a function 'do_hex' which will print decimal number in a Hexadecimal form.
void do_hex(int n)
{
int j=0;
int a[10];
while(n!=0)
{
a[j] = n%16;//storing the remainder at every iteration as element in an array
n = n/16;//at every iteration we are dividing 'n' with 2
//and storing its quotient as a new value in of 'n' variable.
j++;
}
j = j-1; //decrementing the value of j by 1,
//because at current j value there is a garbage value.
while(j>=0)
{
printf("%d",a[j]);
j--;
}
}
void do_hex(int n) { int j=0; int a[10]; while(n!=0) { a[j] = n%16;//storing the remainder at every iteration as element in an array n = n/16;//at every iteration we are dividing 'n' with 2 //and storing its quotient as a new value in of 'n' variable. j++; } j = j-1; //decrementing the value of j by 1, //because at current j value there is a garbage value. while(j>=0) { printf("%d",a[j]); j--; } }
Using above define function 'do_hex()':
void main()
{
printf("Hexadecimal Number is:` ");
do_hex(12);
}
void main() { printf("Hexadecimal Number is:` "); do_hex(12); }
Output1
Hexadecimal Number is: C
Using above define function 'do_hex()':
void main()
{
printf("Hexadecimal Number is: ");
do_hex(32);
}
void main() { printf("Hexadecimal Number is: "); do_hex(32); }
Output2
Hexadecimal Number is: 20
Using above define function 'do_hex()':
void main()
{
printf("Hexadecimal Number is: ");
do_hex(44);
}
void main() { printf("Hexadecimal Number is: "); do_hex(44); }
Output3
Hexadecimal Number is: 2C
0 Comments
Post a Comment