Simple program - Where have i gone wrong?

From: ali (tj_at_raha.com)
Date: 10/31/04


Date: Sun, 31 Oct 2004 16:15:16 -0500

Hi,

I've got this simple program i'm trying to run, but am not getting the
correct answer. Will appreciate if someone could help me out..

I'm compiling this using gcc under linux

==============

#include <stdio.h>

int main()
{

        int counter=0;
        double sum=1;

        double a;
        int b;

        scanf("%f", &a);
        scanf("%i", &b);

        while(counter<b)
        {
                sum = sum * a;
                counter ++;
        }

        printf("%f\n", sum);

        return 0;
}

If i run this, and enter 2 2, i get 4, but if i try to run 3 3, it
gives me 32768.00000

Thanks,
Ali



Relevant Pages

  • Re: ISO: help porting C code to 64 bit linux platform
    ... Now I'm looking at the impact of moving to a 64 bit Linux platform. ... What should I be aware of as I begin compiling code there? ... some of the dwellers on the Western Shore, `int' and `long' are not the ... Another very common mistake is to cast a pointer to an int... ...
    (comp.os.linux.development.apps)
  • help a beginner please
    ... I'm going to try to port a program I wrote in Win-LCC to GCC for console ... operational under Linux. ... program in a new #include file that will be used only while compiling ... developer on Unix using C? ...
    (comp.os.linux.development.apps)
  • Re: pow function working unexpectedly
    ... With reference to my first example given is what I was talking about. ... The first example properly ran with just compiling with: ... int main ... The problem seems to be with gcc when using the pow function in the ...
    (freebsd-questions)
  • Re: Code to connect to MSSQL Database
    ... I am compiling under Linux using GCC. ... for posting guidelines and frequently asked questions. ...
    (comp.lang.c)
  • Re: segmentation fault with getc()
    ... I think I already said running it with gdb does not produce any error. ... compiling it with gcc 3.2.3 without -O3 and running it on ... Linux 2.4 is fine. ...
    (comp.lang.c)