English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

//this program converts a string to binary number and displays it
#include
#include
#include
#include
void main()
{
clrscr();
char ch[20],a;
int len=0,total[100],i=0,pp=0;
cout<<"enter the string: ";
gets(ch);
len=strlen(ch);
for(i=0;i { i=0;
a=ch[i];
pp=a;
while(pp!=0)
{ if((pp%2)==0)
{ total[i]=0;
i++;
}
else
{ total[i]=1;
i++;
}
pp=pp/2;
}
}
i--;
for(int k=i;k>=0;k--)
{ cout< getch();
}
//eg-- input a
// output-- 100001
but i m not getting the proper output...tried hard but nothings helping....so i need ur help!

2007-02-25 21:12:39 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

these is the program command

2007-02-25 21:17:30 · answer #1 · answered by keanhong2 2 · 0 0

1. You reused your for-loop variable *inside* your for-loop itself: the variable 'i'.

2. You put the binary output of each character *outside* the for loop, resulting in it only printing the last character it retrieved

I have modified it to suit my compiler anyway (g++).

#include
#include
#include
#include
int main()
{
char ch[20];
int len=0,total[100],a,i=0,k,pp=0;
cout<<"enter the string: ";
gets(ch);
len=strlen(ch);
for(a=0;a {
pp=ch[a];
i=0;
while( pp!=0 )
{
if( pp%2==0 )
{
total[i]=0;
i++;
}
else
{
total[i]=1;
i++;
}
pp=pp/2;
}
for(k=i-1;k>=0;k--){ cout< cout<<" ";
}
getch();
return 0;
}

2007-02-26 05:37:29 · answer #2 · answered by lwz 2 · 0 0

//this program converts a string to binary number and displays it

No it doesn't!

2007-02-26 05:27:59 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers