Code Competitions

Coding competition

Sunday, 12 May 2013

Program to convert one number system to another

#include<stdio.h>
#include<conio.h>
void main()
{
    int ch;
    clrscr();
    printf("1. decimal to binary");
    printf("\n2. decimal to octal");
    printf("\n3. decimal to hexadecimal");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:
        {
            long d;
            int a[25],i=0,j;
            printf("enter the number in decimal");
            scanf("%ld",&d);
            while(d>0)
            {
                a[i]=d%2;
                d=d/2;
                i++;
            }
            printf("\n");
            for(j=i-1;j>=0;j--)
            {
                printf("%d",a[j]);
            }
            break;
        }
        case 2:
        {
            long d;
            int a[25],i=0,j;
            printf("enter the number in decimal");
            scanf("%ld",&d);
            while(d>0)
            {
                a[i]=d%8;
                d=d/8;
                i++;
            }
            printf("\n");
            for(j=i-1;j>=0;j--)
            {
                printf("%d",a[j]);
            }
            break;
        }
        case 3:
        {
            long d;
            int a[25],i=0,j;
            printf("enter the number in decimal");
            scanf("%ld",&d);
            while(d>0)
            {
                a[i]=d%16;
                d=d/16;
                i++;
            }
            printf("\n");
            for(j=i-1;j>=0;j--)
            {
                printf("%d",a[j]);
            }
            break;
        }
    }
    getch();
}

No comments:

Post a Comment