473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

compound intrest program(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.
Feb 16 '06 #1
6 8370


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.

--
Er*********@sun.com

Feb 16 '06 #2
c_beginner wrote:
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.

Feb 16 '06 #3
c_beginner wrote:
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

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Feb 16 '06 #4
August Karlstrom wrote:
c_beginner wrote:
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.

<snip actual code>
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.


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.

Feb 16 '06 #5

c_beginner wrote:
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.

Feb 16 '06 #6
c_beginner wrote:
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;
}
Feb 16 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Art | last post by:
NEWBIE ALERT! Esteemed List Participants and Lurkers: (System: P-II 350, 192 meg, Win98 SE, Python 2.2.3, wxPythonWIN32-2.4.1.2-Py22.exe) I'm having a lot of fun getting started with Python...
7
by: Rensjuh | last post by:
Hello, does someone have / know a good C++ tutorial for beginnners? I would prefer Dutch, but English is also fine. Hoi, heeft / kent iemand nog een goede C++ tutorial voor beginners? Het liefste...
4
by: Mark | last post by:
BEGINNER QUESTION I have a table which has a compound primary key consisting of two columns. One of these columns is a foreign key which is generated in another table by an identity. I want...
27
by: MHoffman | last post by:
I am just learning to program, and hoping someone can help me with the following: for a simple calculator, a string is entered into a text box ... how do I prevent the user from entering a text...
1
by: c_beginner | last post by:
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...
8
by: AG | last post by:
Hello, This is my first post to this group, and on top of that I am a beginner. So please direct me to another group if this post seems out of place.... I have recently written a program which...
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
1
by: ash84 | last post by:
This program writes a webpage which displays a voting survey. I'm in the first semester of programming in C++. Here is the program: Problem: I can access the page from web BUT... when I click...
7
by: moogyd | last post by:
Hi group, I have a basic question on the zip built in function. I am writing a simple text file comparison script, that compares line by line and character by character. The output is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.