0

Dear All,

I have written a program which converts an input decimal number into any number in a base between 2 and 36. Below is my program:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int newbase,n,i,ascii,ndigit=0,q,r,zero;
    char newrep[100];
    printf("Enter the number to be converted");
    scanf("%d",&n);
    printf("Enter the base between 2 and 36 to which the number is to be converted");
    scanf("%d",&newbase);
    zero=48;
    q=n;
    ndigit=0;
    while(q!=0)
    {
    r=q%newbase;
    ndigit+=1;
    ascii=zero+r;
    if(ascii>57)
    {
            ascii=ascii+7;
    }
    newrep[ndigit]=(char)ascii;
    q=q/newbase;
    }
    printf("Base %d representation of %d is\n",newbase,n);
    for(i=ndigit;i<1;i--)
    {
    printf("%s",newrep[ndigit]);
    }                              
    system("pause");
    return 0;       
}

I am getting correct values of newbase and ndigit but am unable to print the value of the converted output stored in newrep array. For example if I enter input number as 15 and base as 8 then I should get 17 as output. Kindly help me out to print the value of the output array. Thank you.

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Ancient Dragon
0

line 29: should be %d, not %s because newrep[ndigit] is a single numeric digit, not a string

Edited by Ancient Dragon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.