hi friends,

i want to write a programe that convert decimal to binary i write two codes first without using function and second is using function,my first code is working properly but second code is not given the desire result. my both codes are here below
#include<iostream.h>
main()
{
int i=0,n,j,b[100];
cout<<"Enter the decimal number";
cin>>n;
while(n>0)
{
b[i]=n%2;
n=n/2;
i++;
}
cout<<" binary is";
j=i-1;
for(i=j;j>=0;j--)
{
cout<<b[j];
}
}

and other code that is not working properly is here
#include<iostream.h>
int bin(int b[]);
main()
{
int n,bina[100];
cout<<"Enter the decimal number";
cin>>n;
bin(bina);
}
int bin(int b[])
{
int i=0;
int num,j;
while(num>0)
{
b[i]=num%2;
num=num/2;
i++;
}
j=i-1;
for(i=j;j>=0;j--)
{
cout<<b[j];
}
}
can any one tells me that what is wrong with second code. thanks in advance
regards,
Your second code doesn't work because you never pass the decimal number to function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>  // always use <iostream> instead of <iostream.h>
using namespace std;

int bin(int b[], int num);// num is the decimal number
int main()
{
    int n,bina[100];
    cout<<"Enter the decimal number ";
    cin>>n;
    bin(bina,n);
}

int bin(int b[], int num)
{
    int i=0;
    int j=0; 
    while(num>0)
    {

        b[i]=num%2;
        num=num/2;
        i++;
    }
    j=i-1;
    for(i=j; j>=0; j--)
    {
        cout<<b[j];
    }
}


Also, when posting code use [code] [/code] tags.
Last edited on
hi,
thanks friend its working
but if i use iostream instead of iostream.h my compiler not compile its ok thanks for ur help
just tell me the concept in detail these lines
int bin(int b[], int num);//
bin(bina,n);
int bin(int b[], int num)
waiting for reply
regards,
but if i use iostream instead of iostream.h my compiler not compile

That's because your compiler is very old.

just tell me the concept in detail these lines
int bin(int b[], int num);//
bin(bina,n);
int bin(int b[], int num)

I simply added an additional parameter to the bin function
1
2
3
4
int bin(
        int b[],  // the same as in your original code 
        int num // the decimal number which is entered by user
        );
Topic archived. No new replies allowed.