473,500 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple 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 simple interest.

#include<stdio.h>
int intrest(int rate,float amount);
int main(void)
{
int intrest_rate, /* the percentage intrest*/
return_year ; /* year of returing the amt*/
float money, /* amount which has been browed*/
intrest_amt, /* intrest to be paid */
si; /* calcuation of simple intrest*/

printf("input the intrest,amount,return year\n");
scanf("%d%f%d",&intrest_rate,&money,&return_year);
if((return_year < 1) && (return_year > 50))

printf("enter the year again.The year should not be greater than 50");

intrest_rate = intrest(intrest_rate,money);
if(intrest_rate)
si = money * intrest_amt * return_year;
printf("The simple intrest is %f\n",si);
}
int intrest(int rate,float amount)
{
int temp;

if((rate < 0) || (rate > 100))
{
printf("the intrest rate is either too high or low\n");
exit(1);
}
return temp = ((amount * rate) / 100);
}
Feb 16 '06 #1
1 5122
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 simple interest.
Nice try for a beginner. See comments below.

#include<stdio.h>
int intrest(int rate,float amount);
int main(void)
{
int intrest_rate, /* the percentage intrest*/
return_year ; /* year of returing the amt*/
I find splitting declarations between various lines like you just did
ugly. IMHO this is more readable:

int interest_rate; /*the percentage interest*/
int return_year; /*year of returning the amt*/

BTW, why do you declare the interest rate as an integer. You can have
interest rates such as 2.5% A float or preferably a double makes more
sense.
float money, /* amount which has been browed*/
intrest_amt, /* intrest to be paid */
si; /* calcuation of simple intrest*/
Again, I would repeat the type in each line.
money is somehow too general a name for a variable. money_borrowed is
more explicit. Get into the habit of using meaningful names for your
variables and you'll save yourself the pain of trying to remember what
mbzhjkil stands for.
The float type runs out of precission really quick. There's no reason
to not using double.
printf("input the intrest,amount,return year\n");
scanf("%d%f%d",&intrest_rate,&money,&return_year);
scanf is dangerous. Use fgets and sscanf instead. Wheter you stick with
scanf or change to the fgets/sscanf combination, check the return value
of (s)scanf, it tells you how many parameters were successfully parsed
and assigned. Ask yourself what will happen to your program if the user
inputs something like

13af hello world!!!

Also I would ask for each variable at a time, it makes it less
confusing for the user.
if((return_year < 1) && (return_year > 50))
I think you want an || instead of an &&. As it's written the condition
will allways be false.
printf("enter the year again.The year should not be greater than 50");
Here you ask the user to enter the year again, but you do not provide
code for reading the input again.
intrest_rate = intrest(intrest_rate,money);
if(intrest_rate)
What is this if suppossed to do? The follwing code will only execute if
intrest_rate is non_zero. But what's the problem with intrest_rate
being zero? Furthermore, if it is zero your program will not assign to
si, but it will use its value in the following printf. This is
undefined behaviour, anything may happen.
si = money * intrest_amt * return_year;
printf("The simple intrest is %f\n",si);
You declared main as returning int (well done, BTW) but you fail to
provide a return statement. In C99 is legal to fall off the end of a
function without returning a value, but it is still poor style.
}
int intrest(int rate,float amount)
{
int temp;

if((rate < 0) || (rate > 100))
{
printf("the intrest rate is either too high or low\n");
exit(1);
You did not provide a prototype for exit(). include the stdlib.h
header.

On a side note, wouldn't it be nicer asking the user to enter the
interest again, instead of exiting?
}
return temp = ((amount * rate) / 100);
}


Feb 16 '06 #2

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

Similar topics

0
881
by: Dave | last post by:
I created a C# ASP.Net project in Visual Studio 2003 called CS1. When I navigate to http://localhost/CS1/webform1.aspx, my new ASP.Net web page displays fine. But I want to rename the page to...
1
1123
by: Dave | last post by:
This is pretty basic but here goes: Is there a shortcut for typing text for a label? Every example I've seen requires you to go to the property window, select the name property value and then...
6
8374
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...
5
1145
by: pvdg42 | last post by:
VS 2003, C# if the language matters. Scenario: Two page project, text box content on the start page is loaded into session variables to be displayed in a greeting on the second page. Works fine...
2
1132
by: needin4mation | last post by:
I am looking for a good book on C# that has what an interface is and how to use it. In my local library they have a lot of Java books. Is an interface a C# thing or an OOP thing? So that if I...
1
1252
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...
2
1224
by: Jack | last post by:
Hi there, I'm a very experienced C++ developer but a relative newcomer to web development and a raw neophyte in the ASP.NET arena. I have a very simple scenario that hopefully someone can help...
3
1270
by: jmDesktop | last post by:
This program: s = 'abcde' i = -1 for i in range (-1, -len(s), -1): print s, i gives abcd -1
2
3503
by: nicholas08 | last post by:
I am new to programming and trying to work on a simple console app. I am making a simple Library Management system where the user/admin can manage members and item. Attaching the menu so it's clear...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7232
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...
1
6906
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
7397
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
5490
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,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.