compound intrest program(beginner)

C

c_beginner

yes, this is my how work question. Since I am lack in getting
an assistance with my lab work I put this in this advance group.
Sorry for the trouble I am making.

Write a program to calculate the compound interest.

#include<stdio.h>
#include<stdlib.h>
double intrest(double tot_amount,float percent)
{
double temp;
if(percent < 0.0 || percent > 100.0)
{
printf("un-reasonable intrest rate, enter again\n");
fscanf(stdin,"%f",percent);
}
temp = (tot_amount * percent) / 100;
return temp;
}
int main(void)
{
double intrest_rate,cpnd_intrest,prnc_amount,percent;
unsigned short year,i;
printf("enter the percent of intrest,amount,year\n");
fscanf(stdin,"%f%f%d",&percent,&prnc_amount,&year);
if(year > 50)
{
printf("kindly input the reasonable year of return\n");
fscanf(stdin,"%d",&year);
}
else
{
intrest_rate = intrest(prnc_amount,percent);
cpnd_intrest = prnc_amount*(1+intrest_rate);
for(i = year ; i< year ; i++)
cpnd_intrest = cpnd_intrest * i;
}
printf("the compound intrest is %f\n",cpnd_intrest);
return 0;
}

I am getting a strange output when I run the program.Kindly help.
 
E

Eric Sosman

c_beginner wrote On 02/16/06 10:44,:
[...]
int main(void)
{
double intrest_rate,cpnd_intrest,prnc_amount,percent;
unsigned short year,i;
printf("enter the percent of intrest,amount,year\n");
fscanf(stdin,"%f%f%d",&percent,&prnc_amount,&year);

Here's at least one problem (I haven't looked carefully
for others): the conversion specifiers in the format string
don't match the corresponding pointer arguments. With "%f"
you're telling fscanf() to produce a `float', but you've
supplied pointers to `double' variables instead. Similarly,
"%d" tells fscanf() to produce an `int', but you've provided
a pointer to an `unsigned short'. fscanf() trusts you (not
the best policy, it appears ;-), and when it tries to store
`float' values in `double' variables or an `int' value in
an `unsigned short' variable, there's no telling what will
happen.

Hint #1: Go back to your C textbook and read the scanf()
section again. Although the format strings look similar to
those used with printf(), they are *not* the same at all.

Hint #2: Some compilers are able to detect and warn
about mismatches of this kind, if you ask them to do so.
For example, if you're using gcc try the "-W -Wall" flags
when you compile your code. Other compilers that can do
this have their own incantations; consult the documentation.
 
M

Mara Guida

c_beginner said:
yes, this is my how work question. Since I am lack in getting
an assistance with my lab work I put this in this advance group.
Sorry for the trouble I am making.

Write a program to calculate the compound interest.

#include<stdio.h>
#include<stdlib.h>
double intrest(double tot_amount,float percent)
{
double temp;
if(percent < 0.0 || percent > 100.0)
{
printf("un-reasonable intrest rate, enter again\n");
fscanf(stdin,"%f",percent);
}
temp = (tot_amount * percent) / 100;
return temp;
}
int main(void)
{
double intrest_rate,cpnd_intrest,prnc_amount,percent;
unsigned short year,i;
printf("enter the percent of intrest,amount,year\n");
fscanf(stdin,"%f%f%d",&percent,&prnc_amount,&year);
You have a type mismatch: %f <= float; percent <= double
You have a type mismatch: %f <= float; prnc_amount <= double
You have a type mismatch: %d <= int; year <= unsigned short
if(year > 50)
{
printf("kindly input the reasonable year of return\n");
fscanf(stdin,"%d",&year);
You have a type mismatch: %d <= int; year <= unsigned short
}
else
{
intrest_rate = intrest(prnc_amount,percent);
cpnd_intrest = prnc_amount*(1+intrest_rate);
for(i = year ; i< year ; i++)
The body of the for loop is never executed
cpnd_intrest = cpnd_intrest * i;
}
printf("the compound intrest is %f\n",cpnd_intrest);
return 0;
}

I am getting a strange output when I run the program.Kindly help.


Set your compiler to output warning messages and treat them as errors.
Correct the errors/warnings and rerun the program.
 
A

August Karlstrom

c_beginner said:
yes, this is my how work question. Since I am lack in getting
an assistance with my lab work I put this in this advance group.
Sorry for the trouble I am making.

Write a program to calculate the compound interest.

#include<stdio.h>
#include<stdlib.h>
double intrest(double tot_amount,float percent)
{
double temp;
if(percent < 0.0 || percent > 100.0)
{
printf("un-reasonable intrest rate, enter again\n");
fscanf(stdin,"%f",percent);
}
temp = (tot_amount * percent) / 100;
return temp;
}
int main(void)
{
double intrest_rate,cpnd_intrest,prnc_amount,percent;
unsigned short year,i;
printf("enter the percent of intrest,amount,year\n");
fscanf(stdin,"%f%f%d",&percent,&prnc_amount,&year);
if(year > 50)
{
printf("kindly input the reasonable year of return\n");
fscanf(stdin,"%d",&year);
}
else
{
intrest_rate = intrest(prnc_amount,percent);
cpnd_intrest = prnc_amount*(1+intrest_rate);
for(i = year ; i< year ; i++)
cpnd_intrest = cpnd_intrest * i;
}
printf("the compound intrest is %f\n",cpnd_intrest);
return 0;
}

I am getting a strange output when I run the program.Kindly help.

Why do you post the same question twice? I think you should instead
thank Antonio for the help you got with your previous posting.


August
 
A

Antonio Contreras

August said:
Why do you post the same question twice? I think you should instead
thank Antonio for the help you got with your previous posting.

Actually the program is not exactly the same and he took into account
some things I said (he changed some variables from float to double and
added some code after my hints). This program also claims to compute a
compound interest, the other one claimed to compute a simple interest.
(And I say claimed because I did not check the program logic.)

Anyway you're right in saying that some kind of acknowledgement
would've been nice.
 
H

hammrGRS

c_beginner said:
yes, this is my how work question. Since I am lack in getting
an assistance with my lab work I put this in this advance group.
Sorry for the trouble I am making.

Write a program to calculate the compound interest.

#include<stdio.h>
#include<stdlib.h>
double intrest(double tot_amount,float percent)
{
double temp;
if(percent < 0.0 || percent > 100.0)
{
printf("un-reasonable intrest rate, enter again\n");
printf("Invalid rate you dolt. Try again
nimrod\n");
fscanf(stdin,"%f",percent);
}
temp = (tot_amount * percent) / 100;
return temp;
}
int main(void)
{
double intrest_rate,cpnd_intrest,prnc_amount,percent;
unsigned short year,i;
printf("enter the percent of intrest,amount,year\n");
fscanf(stdin,"%f%f%d",&percent,&prnc_amount,&year);
if(year > 50)
{
printf("kindly input the reasonable year of return\n");
printf("please please please enter the year of
return\n");
printf("please?n");
fscanf(stdin,"%d",&year);
}
else
{
intrest_rate = intrest(prnc_amount,percent);
cpnd_intrest = prnc_amount*(1+intrest_rate);
for(i = year ; i< year ; i++)
cpnd_intrest = cpnd_intrest * i;
}
printf("the compound intrest is %f\n",cpnd_intrest);
return 0;
}

I am getting a strange output when I run the program.Kindly help.

That should correct the output problems.
 
M

Martin Ambuhl

c_beginner said:
yes, this is my how work question. Since I am lack in getting
an assistance with my lab work I put this in this advance group.
Sorry for the trouble I am making.

See if this much shorter program helps you figure out your problems.
Note specifically that many of your scanf specifiers are wrong.

#include <stdio.h>
#include <math.h>

int main(void)
{
double principal, rate, periods;
printf("enter interest rate per period, principal amount,\n"
" & number of periods, separated by spaces: ");
fflush(stdout);
scanf("%lf%lf%lf", &rate, &principal, &periods);
printf("the compound interest is %f\n",
principal * (pow(1 + rate / 100, periods) - 1));
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top