Hi!

I am new to C programming, so this question might sound foolish. I have a problem with output of a program (sometimes the output is wrong)

Following is the program: (I am using Turbo C compiler)

/* Calculate simple interest */

#include <stdio.h>
main()
{
int p, n;
float r, si;
printf("Enter values of p, n, and r");
scanf("%d, %d, %f", &d, &d, &f);
si=p*n*r/100;
printf("Simple interest=%f", si);
getchar();
return 0;
}

There is no error in this program. Output is also right but only for small integers.

e.g. If I enter p=1000, n=5, r=10, answer is RIGHT.

BUT for p=10000, n=5, r=10, answer is wrong (in minus value and totally wrong)

I think this is because of integer value limitations.

If yes, what is the solution to it so that I can use a bigger number.

If no, what is the reason for the error?

Please help me.

Thank you very much.

Recommended Answers

All 4 Replies

Hi!

I am new to C programming, so this question might sound foolish. I have a problem with output of a program (sometimes the output is wrong)

Following is the program: (I am using Turbo C compiler)

/* Calculate simple interest */

#include <stdio.h>
main()
{
int p, n;
float r, si;
printf("Enter values of p, n, and r");
scanf("%d, %d, %f", &d, &d, &f); //should be &p, &n, &r
si=p*n*r/100;
printf("Simple interest=%f", si);
getchar();
return 0;
}

There is no error in this program. Output is also right but only for small integers.

e.g. If I enter p=1000, n=5, r=10, answer is RIGHT.

BUT for p=10000, n=5, r=10, answer is wrong (in minus value and totally wrong)

I think this is because of integer value limitations.

If yes, what is the solution to it so that I can use a bigger number.

If no, what is the reason for the error?

Please help me.

Thank you very much.

Yes, you've exceeded the limit for integers. Use unsigned longs, instead of integers.

That will solve your problem until you need *much* bigger numbers. :)

Hi!
#include <stdio.h>
main()
{
int p, n;
float r, si;
printf("Enter values of p, n, and r");
scanf("%d, %d, %f", &d, &d, &f);
si=p*n*r/100;
printf("Simple interest=%f", si);
getchar();
return 0;
}
There is no error in this program. Output is also right but only for small integers.

I think you posted wrong version of your code, namely it should not even compile due to the line scanf("%d, %d, %f", &d, &d, &f); You don't have the variables d , or f in the program. Maybe you'll post the working code you are having problems with.

There are no variables d or f. Those are just mistyped scanf "%d and %f", that were carried over to the variable name part of the scanf().

The program was compiling and working, with small numbers, so you know it was just an error in keyboarding it in, and what the error must be.

For me the code works fine, with some minor changes in it. But the code it self is working ok except the integer size. Since you are using turbo C compile which is a 16 bit compiler, the integer size is smaller than ity should be, and that the reason why it works on mine. I would really suggest you to change your compile. Turbo C compiler are no more supported expect they are used in embedded programming thats it.

You need to look in to few more things, you need to read the right variables. shouldn't that scanf function call should lile this way

scanf("%d %d %f", &p, &n, &r);

ssharish

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.