Khanh Doan
asked on
Error about variable type
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i, b = 0, snp[8], t = 7, stp = 0, check, n;
clrscr();
printf("\nInput a binary number : ");
scanf("%d", &n);
for (i = 7; i >= 0 ; i--)
{
if (n - pow(10, i) >= 0)
snp[b] = 1;
else
snp[b] = 0;
//n = n - pow(10, i);
b++;
}
for (i = 0; i < 8 ; i++)
{
if ((snp[i] != 0) && (snp[i] != 1))
{
printf("This is not a binary number.");
getch();
exit();
}
}
for (i = 0; i < 8 ; i++)
{
//printf("%d ", snp[i]);
stp += snp[i] * pow(2, t);
t = t -1;
}
printf("The decimal of %d is %d.", n, stp);
getch();
}
if i change int i, b = 0, snp[8], t = 7, stp = 0, check, n;
to int i, b = 0, check, n;
long int snp[8], t = 7, stp = 0;
it doesn't work
i want input a binary number like 11111111 and then it chane to decimal. How can i do ?
#include <conio.h>
#include <math.h>
void main()
{
int i, b = 0, snp[8], t = 7, stp = 0, check, n;
clrscr();
printf("\nInput a binary number : ");
scanf("%d", &n);
for (i = 7; i >= 0 ; i--)
{
if (n - pow(10, i) >= 0)
snp[b] = 1;
else
snp[b] = 0;
//n = n - pow(10, i);
b++;
}
for (i = 0; i < 8 ; i++)
{
if ((snp[i] != 0) && (snp[i] != 1))
{
printf("This is not a binary number.");
getch();
exit();
}
}
for (i = 0; i < 8 ; i++)
{
//printf("%d ", snp[i]);
stp += snp[i] * pow(2, t);
t = t -1;
}
printf("The decimal of %d is %d.", n, stp);
getch();
}
if i change int i, b = 0, snp[8], t = 7, stp = 0, check, n;
to int i, b = 0, check, n;
long int snp[8], t = 7, stp = 0;
it doesn't work
i want input a binary number like 11111111 and then it chane to decimal. How can i do ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
to int i, b = 0, check, t = 7, stp = 0;
long int snp[8], n;
then I input 11111 and then it show : The decimal of 589738503 is 255.
why ?