make a program using C,C++

M

mavic

hi to everybody,i need help for my project....how to make a program
that accept any integer and convert it binary,octal and hexadecimal.pls
help me...thanks
 
A

Arne Demmers

mavic said:
hi to everybody,i need help for my project....how to make a program
that accept any integer and convert it binary,octal and hexadecimal.pls
help me...thanks

And what should happen to the binary/octal/hexadecimal representation?

Arne
 
A

Anup Bishnoi

here is a program my friend navjot made to convert decimal system
integer to binary.

u can easily change this program to show octal or hexadecimal or any
other system.

#include<stdio.h>
#include<math.h>
main()
{
int i,x,t,answer=0;
int num[15];

clrscr();

printf("Enter the digits of the binary number \n(pressing Enter
between 2 digits and enter 9 when finished entering) : \n");

for(i=0;i<15;i++)
{
scanf("%d",&num);
if( num!=0 && num!=1 )
{break;
}
}

i--;

x=i;

for(t=0;t<=x;t++)
{
/* while(i>=0) */
answer+=(pow(2,t)*num);
i--;
}

printf("%d",answer);

getch();

}


groups.google.com/group/all-at-c
 
A

Anup Bishnoi

this might b of some help :

#include<stdio.h>
main()
{
int num,i=0,q;
int rem[100];
clrscr();

printf("Enter the number : ");
scanf("%d",&num);
q=num;
while(q>0)
{
rem=q%2;
q=q/2;
i++;
}

while( i>=0 )
{
printf("%d",rem[i-1]);
i--;
}
getch();

}


It only converts integer to binary, integer to other systems can b made
similarly.

Anup Bishnoi
 
A

Arne Demmers

mavic said:
there should be an output of these...

this should work

#include <stdio.h>

int main()
{
int number = 0, temp = 0, counter = 0;

clrscr();

printf("Give me a number: ");
scanf("%d", &number);
printf("HEXADECIMAL: %04X\n", number);

printf("Octal: ");
while(counter < (sizeof(int)*8 /3))
{
temp = number << (counter*3);
temp >>= (sizeof(int)*8 -3);
printf("%d", temp);
counter++;
}
printf("\nBinary: );
counter = 0;
while(counter < (sizeof(int)*8 ))
{
temp = number << (counter);
temp >>= (sizeof(int)*8 -1);
printf("%d", temp);
counter++;
}

return 0;
}

Arne
 
I

Ico

[snip homework answer]

Hi Arne,

That's very friendly of you, to do this man's homework; I guess he
really deserves a good grade for that, don't you think so ? His teacher
will be really proud of him, that's for sure. You know, I don't feel
like working today, and I'm just too lazy to learn about the stuff I'm
supposed to be doing, it's all sooooo complicated! If I just tell you
what my job is, can you do it for me ?

Ico
 
A

Anup Bishnoi

here is a program my friend navjot made to convert decimal system
integer to binary form.

u can easily change this program to show octal or hexadecimal or any
other system.

#include<stdio.h>
main()
{
int num,i=0,q;
int rem[100];
clrscr();

printf("Enter the number : ");
scanf("%d",&num);
q=num;
while(q>0)
{
rem=q%2;
q=q/2;
i++;
}

while( i>=0 )
{
printf("%d",rem[i-1]);
i--;
}
getch();

}

Anup Bishnoi
groups.google.com/group/all-at-c
 
I

Ico

Anup Bishnoi said:
this might b of some help :
[ snip code ]
It only converts integer to binary, integer to other systems can b made
similarly.

Anup,

Could you please :

- properly quote the messages you are replying to.

- use proper English when posting to this newsgroup. As far as I know,
there are no such words as 'u' and 'b' in English.

- stop making other people's homework. Homework is supposed to give
students a chance to practice the stuff they have learned (or were
supposed to learn); By just giving a way solutions, the man will
graduate his computer course without being able to program properly,
but still he will be assigned to write the fly-by-wire software for
the new Boing plane which you will fly with to your summer holiday
next year. You wouldn't want that plane to crash, would you ?
 
A

Arne Demmers

Yes I know, normally I don't do this too, I don't like lazy persons, but it
was somewhat too much basic C for not doing it at all, probaly should have
been better to give some kind of link to a C manual where you can find all
this kind basics..

Anyway next time less luck for guys/women like him...

Arne

Ico said:
[snip homework answer]

Hi Arne,

That's very friendly of you, to do this man's homework; I guess he
really deserves a good grade for that, don't you think so ? His teacher
will be really proud of him, that's for sure. You know, I don't feel
like working today, and I'm just too lazy to learn about the stuff I'm
supposed to be doing, it's all sooooo complicated! If I just tell you
what my job is, can you do it for me ?

Ico
 
B

Barry

Arne Demmers said:
this should work

#include <stdio.h>

int main()
{
int number = 0, temp = 0, counter = 0;

clrscr();

printf("Give me a number: ");
scanf("%d", &number);
printf("HEXADECIMAL: %04X\n", number);

printf("Octal: ");
while(counter < (sizeof(int)*8 /3))
{
temp = number << (counter*3);
temp >>= (sizeof(int)*8 -3);
printf("%d", temp);
counter++;
}
printf("\nBinary: );
counter = 0;
while(counter < (sizeof(int)*8 ))
{
temp = number << (counter);
temp >>= (sizeof(int)*8 -1);
printf("%d", temp);
counter++;
}

return 0;
}

Arne

You should not be doing homework, but since this won't compile anyway
perhaps the point is moot.
 
A

Anup Bishnoi

Could you please :
- properly quote the messages you are replying to.

hey i clicked the reply link to his msg! u can see who replied whom by
clicking on "view as tree"
- use proper English when posting to this newsgroup. As far as I know,
there are no such words as 'u' and 'b' in English.

i'll take care of that. there was a time when i used to hate people who
wrote like this but eventually it just seemed simpler to write that
way!
- stop making other people's homework. Homework is supposed to give
students a chance to practice the stuff they have learned (or were
supposed to learn); By just giving a way solutions, the man will
graduate his computer course without being able to program properly,
but still he will be assigned to write the fly-by-wire software for
the new Boing plane which you will fly with to your summer holiday
next year. You wouldn't want that plane to crash, would you ?

ok i got the point, so as Arne says "no luck for others!"


ps-was teh quoting ok this time?
 
A

Arne Demmers

You should not be doing homework, but since this won't compile anyway
perhaps the point is moot.

Now you mention it, it indeed contains some mistakes.... What was I
thinking....


Arne
 
I

Ico

Hi Anup,

Anup Bishnoi said:
hey i clicked the reply link to his msg! u can see who replied whom by
clicking on "view as tree"

No, it's not that simple. What you see as 'google groups' is actually a
world-wide network of servers (which are *not* a part of the google
network) that are distributing the messages all over the planet. This
network already exists for more then 25 years, and is called usenet. You
can find a proper explanation about usenet and google at the following site :
http://cfaj.freeshell.org/google/
i'll take care of that. there was a time when i used to hate people who
wrote like this but eventually it just seemed simpler to write that
way!

It might be simpler to write, but it is definitely not easier to read.
English is not the native language of a lot of visitors and writers on
this newsgroup, and delibirately using obfuscations like 'u' is only
likely to cause confusion.
ok i got the point, so as Arne says "no luck for others!"


ps-was teh quoting ok this time?

Fantastic, thanks a lot :)
 
D

David Bolt

printf("\nBinary: );
counter = 0;
while(counter < (sizeof(int)*8 ))
{
temp = number << (counter);
temp >>= (sizeof(int)*8 -1);
printf("%d", temp);
counter++;
}

Even though it's looks like this is the solution to a homework question,
is there any reason for not doing it this way:

printf("\nBinary: ");
for(counter=(sizeof(int)*8);counter>=0;--counter)
{
temp = 1 << (counter);
printf("%d", (number & temp) ? 1 : 0);
}


Regards,
David Bolt
 
F

Flash Gordon

Anup said:
hey i clicked the reply link to his msg! u can see who replied whom by
clicking on "view as tree"

So what makes you think anyone else is using the same interface? I know
a blind person who reads Usenet and does *not* use Google. I know
people who use text based news readers where there are *no* buttons to
click on. In my case, I have the news reader (not Google) set to hide
all read messages since it makes it easier for me which means that even
with a tree view (I use a threaded view, effectively the same thing) I
might not be able to see the previous post. Finally, there is absolutely
no guarantee that the message you are replying to has, or ever will,
propagate to all the people who see your message.

Google is just one of a vast number of interfaces to Google, and it is a
comparatively recent one at that.

So please quote properly, and that includes leaving in the lines that
say who said what which you just deleted. Your deleting them means it
would require effort for me to see which of the many sensible people
here you were replying to.

Note, this is not intended to be getting at you, rather it is explaining
the situation. I accept that Google don't make it obvious and, in fact,
make it needlessly hard for you to do the right thing. There is a link
off this page http://cfaj.freeshell.org/google/ to a questionair so that
you can vote for Google to "Default quoting of previous message in
replies" which would make it easier for everyone using Google to do the
right thing. Please go and vote to make life better for all Usenet users.
i'll take care of that.

Then do so please. In this post you have continued to use "u" for you.
> there was a time when i used to hate people who
wrote like this but eventually it just seemed simpler to write that
way!

Whether it is easier to write like that is largely irrelevant. Please
understand that it is harder to read especially for people for whom
reading English is not easy or natural, such as Dyslexics and people for
whom English is not their first language. I know we have people in both
groups here. Also please remember, you write the post once but it gets
read many times, so if you spend an extra 30 seconds but save 20 people
5 seconds each it is *still* a net saving.

We generally don't mind real errors cause by English not being someone's
first language. Those are natural and understandable. Although sometimes
such errors are pointed out to help people improve their English.

ps-was teh quoting ok this time?

No, you snipped the bit saying who you were replying to. However, it was
a vast improvement. Thank you for making the effort though.
 
P

pete

David said:
Even though it's looks like this is the solution
to a homework question,
is there any reason for not doing it this way:

printf("\nBinary: ");
for(counter=(sizeof(int)*8);counter>=0;--counter)
{
temp = 1 << (counter);
printf("%d", (number & temp) ? 1 : 0);
}

What is (1 << (sizeof(int)*8)) supposed to be?
 
P

pete

mavic said:
hi to everybody,i need help for my project....how to make a program
that accept any integer and convert it binary,octal and hexadecimal.pls
help me...thanks

/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>

void itoa_b(int n, char *s);

int main(void)
{
int integer;
char string[CHAR_BIT * sizeof integer];

integer = 12345;
itoa_b(integer, string);
printf("The binary representation of %d, is B %s\n",
integer, string);
sprintf(string, "%o", integer);
printf("The octal representation of %d, is 0%s\n",
integer, string);
sprintf(string, "%x", integer);
printf("The hex representation of %d, is 0x%s\n",
integer, string);
return 0;
}

void itoa_b(int n, char *s)
{
int half, min_offset;
char *p, swap;

min_offset = 0;
if (0 > n) {
if (-INT_MAX > n) {
++n;
++min_offset;
}
n = -n;
*s++ = '-';
}
p = s;
half = n;
do {
half /= 2;
*p++ = (char)(n - 2 * half + '0');
n = half;
} while (half != 0);
*s = (char)(*s + min_offset);
*p-- = '\0';
while (p > s) {
swap = *s;
*s++ = *p;
*p-- = swap;
}
}

/* END new.c */
 
D

David Bolt

On Tue said:
David Bolt wrote:

What is (1 << (sizeof(int)*8)) supposed to be?
^^^^^^^^^^^^^
gives the number of bits in an int and left-shifts 1 that number of
times.

I suppose that I should have used:

for(counter=(sizeof(int)*CHAR_BIT);counter>=0;--counter)
^^^^^^^^^
However, I'm guessing that you missed the above, which would mean that
mean you missed that for the first run through the loop:

temp = 1 << (counter);

would be the equivalent of:

temp = 1 << ((sizeof(int)*8)-1)


Regards,
David Bolt
 
F

Flash Gordon

David said:
^^^^^^^^^^^^^
gives the number of bits in an int and left-shifts 1 that number of times.

<snip>

I might be going mad (and people will correct me if I am wrong) but
isn't the following all true?
A numeric constant 1 in the source code as above will be treated as
being of type int

If you left shift 1 by the number of bits in an int you get a number
too big to be represented in an int thus causing an overflow

If you cause an overflow you are invoking undefined behaviour and
*anything* can happen. One likely result in the above being
initialising counter to 0, but stranger things could happen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top