0
#include<stdio.h>
#include<math.h>
main()
{
    int i=0,j,c,s[100];
    double t=0;
    while((c=getchar())!='\n')            
    {
        s[i]=c;
        ++i;
    }
    for(j=i;j>0;--j)
    {
        t=t+(s[i-1]*(pow(16,(4-((double)i)))));
        
        --i;
    }

    printf("%d",(int)t);
    return 0;
}

HI, i need to write a program that converts an hexadecimal number into integer form (for convenience i'm only considering hexadecimal no. without alphabets from a to f).
The program which i've written above is for the same conversion but the output is not right. I tried to find the error but failed. It would be a great help if you give suggestions to correct the above code. Pls do not ask me to write the program using some other method.:?:

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by adnan.siddique
2

check it now

#include<stdio.h>
#include <stdlib.h>
#include<math.h>
# include <iostream>
using namespace std;
int main()
{
    int i=0,j,c,s[100];
    double t=0;
    while((c=getchar())!='\n')            
    {
        s[i]=c;
		s[i] = s[i] - 48;
        ++i;
    }
	int x = 0;
    for(j=i-1;j>=0;j--)
    {
        //t=t+(s[i-1]*(pow(16,(4-((double)i)))));   
		t = t + s[j]*(pow(double(16),double(x)));
		x++;
    }
	cout<<t;
    return 0;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.