Hi, i'm really new to programming in C, and iam kind of stuck on this problem, the question is
Write a C function to print out an integer value in base 4 (using only the digits 0, 1, 2, 3). Use this function to write
a program that reads an integer from the keyboard and displays it in base 4 on the screen.

Now i dont understand wat it means by using digits(0, 1, 2, 3), does it mean 00, 01, 02, 03, 10, n so on???

And with regards to the base 4 thing, i knw how to solve it mathematically, heres an example

Using Division
Let's take a number in base 10, like 231 and change it to base 4. First divide 231 by 4

231/4 = 57 with a remainder of 3

Now divide 57 by 4

57/4 = 14 with a remainder of 1

Divide 14 by 4

14/4 = 3 with a remainder of 2

Divide 3 by 4

3/4 = 0 with a remainder of 3

Am i on the rite track with that?

and now finally with regards to the coding side of it, all i really came up with is this...

#include <stdio.h>

int main(){
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);
    return 0;     

Can some1 plz help me.....

Recommended Answers

All 6 Replies

The answer to your question is so simple... it's equal to your homework complete solution.
But it's your homework, not mine.
Look at your original post carefully. That's the answer.
Or search daniweb C (and C++) forums: there are lots of thread on this extreme popular topic...


i worked on the mathematical part and converted it to code

char *conv_base(long number, int base){
     long calculateRemainder;
     int  n = 0, k = 0;
     static char temp[32], result[32];
     static char *digit = "0123456789ABCDEF"; // list of digits"0123456789ABCDEF"
        // modulus to get the  remainder, practically the same process above, but written in code    
        calculateRemainder = number % base;    
        number = number / base;    
        temp[k++] = digit[calculateRemainder];
     }  while (number > 0);
     while(k >= 0){
         // spell forward  
         result[n++] = temp[--k];         
     result[n-1] = 0;             
     return (result);   

is this a rite approach, iam lookin for an answer just in base 4( digits 0, 1, 2, 3)..??

It looks good - does it work?

yes it does work now, one more problem, I havent been thought how to redirect from a text file into a program, my last question asks me to Note: User can use
redirection to enter a number from a text file to the program.
, i honestly havent been thought this yet, so i was wondering if someone could help me out...

But your program can't detect stdin redirection so everybody can invoke it right now:

yourprogname <theonlynumberbase4.txt

What's a problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.