Monday, July 8, 2019

decimal to binary,octal and hexadecimal conversion in c language

#include<stdio.h>
#include <string.h>

void convert(int, int);
main ( )
{
    int num, base;
    int choice;
while(1)
{
    printf("l.Binary\n") ;
    printf("2.0ctal\n") ;
    printf("3.Hexadecimal\n") ;
    printf("4.Exit\n") ;
    printf ("Enter your choice\n") ;
    scanf ("%d",&choice);
    switch(choice)
    {
        case 1:
            base=2;
            break;
        case 2:
            base=8;
            break;
        case 3:
            base=16;
            break;
        case 4:
            exit(1) ;
        default:
            printf ("Wrong choice\n");
            continue;

    }
    printf ("Enter the number in decimal: ");
    scanf("%d",&num) ;
    convert(num,base) ;
    printf ("\n") ;
}
}


void convert (int num, int base)
{
    int rem;
    rem=num%base;
    num/=base;
    if (num>0)
        convert (num,base);
    if(rem<10)
        printf("%d",rem);
    else
        printf("%c",rem-10+'A');
}

out put:
l.Binary
2.0ctal
3.Hexadecimal
4.Exii
Enter your choice 1
Enter the number in decimal: 12
1100

l.Binary
2.0ctal
3.Hexadecimal
4.Exii
Enter your choice 2
Enter the number in decimal: 12
14

l.Binary
2.0ctal
3.Hexadecimal
4.Exii
Enter your choice 3
Enter the number in decimal: 12
C

No comments:

Post a Comment

python class topic video