473,804 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add 4 20 digit numbers in C

Hi,

Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and
pass the result back to a calling program in COBOL? We were able to
add up to 15 digit numbers without any problems, but we started facing
issues once we go above 15 digits.

Thanks,
Venkat

Nov 8 '07
14 8666
thehobbit wrote:
>
Thank you for the details jack... actually am a nincompoop in C...
more of a COBOL programmer... actually what we are trying to do is
add 4 20 digit integers and return the sum back to the COBOL
program. This is because COBOL has a limitation of 18 digits. When
we added 15 4 digit integer numbers we got the expected sum,
however the moment we started adding 16 4 digit integers then the
sum was all wrong... the code we kind of wrote is as follows;
Please let us know if you have any suggestions...
Top-posting has lost all the previous content. Please avoid it.

You are not adding integers - you are adding strings. Your problem
is double. First arrange to cleanly limit the input strings, i.e.
detect the rightmost and leftmost digits in each. Then you can
simply perform sequential addition, adding individual digits and
propagating carries. You have to be able to drop propagation past
the most significant digit of the input.

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/ (taming google)
<http://members.fortune city.com/nnqweb/ (newusers)

--
Posted via a free Usenet account from http://www.teranews.com

Nov 8 '07 #11
thehobbit wrote:
) Thank you for the details jack... actually am a nincompoop in C...
) more of a COBOL programmer... actually what we are trying to do is add
) 4 20 digit integers and return the sum back to the COBOL program. This
) is because COBOL has a limitation of 18 digits. When we added 15 4
) digit integer numbers we got the expected sum, however the moment we
) started adding 16 4 digit integers then the sum was all wrong... the
) code we kind of wrote is as follows; Please let us know if you have
) any suggestions...

Can't you roll your own bignum function in cobol ?
That is, one that adds four 'strings' together that consist of difits.
I mean, just adding numbers is quite easy.

Here's a snippet in C that does the same.
I'm sure you can easily rewrite that to COBOL.
It could even be made a lot simpler by right-aligning the numbers,
so you don't first have to find the length.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Function to add 4 integers
* Assumes the integers contain only digits '0' through '9'.
*/

void ccadd(char *inp1,char *inp2,char *inp3,char *inp4,char *out)
{
int res;
size_t i1, i2, i3, i4, io;

i1 = strlen(inp1);
i2 = strlen(inp2);
i3 = strlen(inp3);
i4 = strlen(inp4);
io = i1;
if (i2 io) io = i2;
if (i3 io) io = i3;
if (i4 io) io = i4;
io = io + 1;

out[io] = 0;
res = 0;

while (io 0) {
if (i1 0) res += (inp1[--i1] - '0');
if (i2 0) res += (inp2[--i2] - '0');
if (i3 0) res += (inp3[--i3] - '0');
if (i4 0) res += (inp4[--i4] - '0');
out[--io] = (res % 10) + '0';
res = res / 10;
}
}

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Nov 8 '07 #12
thehobbit wrote:
Hi,

Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and
pass the result back to a calling program in COBOL? We were able to
add up to 15 digit numbers without any problems, but we started facing
issues once we go above 15 digits.
Using an old COBOL compiler?

I while back, I took a look at IBM's decNumber C library:

http://www2.hursley.ibm.com/decimal/decnumber.html

which provide an implementation of the upcoming IEEE 754 revision. I
haven't used it yet, but if there are issues with the library, there are
tons of multi-precision C packages out there, see e.g. GNU GMP.

How to interface, COBOL and C, is specified in your platform doc, we
cannot tell.

--
Tor <bw****@wvtqvm. vw | tr i-za-h a-z>
Nov 8 '07 #13
"thehobbit" <ve**********@g mail.coma écrit dans le message de news:
11************* ********@k35g20 00...legro ups.com...
>
Thank you for the details jack... actually am a nincompoop in C...
more of a COBOL programmer... actually what we are trying to do is add
4 20 digit integers and return the sum back to the COBOL program. This
is because COBOL has a limitation of 18 digits. When we added 15 4
digit integer numbers we got the expected sum, however the moment we
started adding 16 4 digit integers then the sum was all wrong... the
code we kind of wrote is as follows; Please let us know if you have
any suggestions...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Function to add 4 integers
*/

void ccadd(char *inp1,char *inp2,char *inp3,char *inp4,char *out)
{
long long int_inp1,int_in p2,int_inp3,int _inp4,int_out;
int_inp1 = atoi(inp1);
int_inp2 = atoi(inp2);
int_inp3 = atoi(inp3);
int_inp4 = atoi(inp4);
You should use atoll or stroll for these conversions if the numbers can
exceed INT_MAX
int_out = int_inp1 + int_inp2 + int_inp3 + int_inp4;
sprintf(out,"%l f",int_out);
Here you are using the wrong format: it should be %lld assuming your C
library supports long long
Also there is no guarantee the result of the conversion will "fit" in the
destination buffer ``out''.
}
The format error may well be the source of your problem, but the solution
posted by Willem is more general, as it can handle numbers of any size
(still assuming the destination is large enough)

--
Chqrlie.
Nov 9 '07 #14
On Nov 7, 10:13 pm, thehobbit <venkat.na...@g mail.comwrote:
Thank you for the details jack... actually am a nincompoop in C...
more of a COBOL programmer... actually what we are trying to do is add
4 20 digit integers and return the sum back to the COBOL program. This
is because COBOL has a limitation of 18 digits. When we added 15 4
digit integer numbers we got the expected sum, however the moment we
started adding 16 4 digit integers then the sum was all wrong... the
code we kind of wrote is as follows; Please let us know if you have
any suggestions...
The reason that COBOL has a limit of 18 digits is because the data
type has a limit of 18 digits.
A long long integer (or unsigned long long) generally consumes 8 bytes
of memory storage.
That means that you can store {typically} 2^64-1 in an unsigned long
long. Let's see what it looks like:
unsigned limit = 184467440737095 51615 (We are unable to store
999999999999999 99999 so 19 digits precision)
signed limit = 922337203685477 5807 (We are unable to store
999999999999999 9999 so 18 digits of precision)

C probably will not have any more success than COBOL at adding your
numbers together, because if they overflow in COBOL then they will
overflow in C.

So the real question is, "What is the exact problem that you are
trying to solve?" Chances are very good that the problem can be
solved in either COBOL or C or both.

If you need to store a 20 digit number in a comp type that occupies 8
bytes, then often you will meet with serious disappointment.
So what we need to know is, is this a total for a report? Is it a
value to be posted back to the database? Exactly what is the purpose
of the new total?
Once we know that, then you can decide on an algorithm to solve the
problem or a new data type for the database, or whatever.

Probably, your posts are more topical in news:comp.progr amming

[snip]

Nov 9 '07 #15

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

Similar topics

3
4613
by: Matt Smolic | last post by:
Does anyone know where I can get some info on creating customer account numbers, part numbers and such. In other words what the logic is behind their creation. I am not looking for code, just how they go about it. I don't want to use something like a phone number or anything else that may change over time. Any info would be greatly appreciated. Thanks Matt
11
17821
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is greater number than second 20 digit number ,then it should return.
0
1194
by: ptek | last post by:
Hi, Is it possible to show "001" in a NumericUpDown control when its value is "1" ? That is, displaying a fixed number of digits, 0 padded. I've read its documentation but I haven't found anything... Thanks
5
1695
by: sesling | last post by:
I have a database field that stores 8 and 9 digit values. I need to calculate the sum value using the first 8 digits. ex.of stored numbers 123456781 234567892 45678903 987654321 calculation should use 12345678 23456789
2
6404
by: Zytan | last post by:
This page http://blog.stevex.net/index.php/string-formatting-in-csharp/ shows: {0:0,0} but for single digit numbers, it prefixes a 0! Is there a way that works with single digit numbers? thanks Zytan
2
3432
by: puneet vyas | last post by:
hi,can any one put light on this question? thanks puneet vyas
7
21616
by: harijay | last post by:
Hi I am a few months new into python. I have used regexps before in perl and java but am a little confused with this problem. I want to parse a number of strings and extract only those that contain a 4 digit number anywhere inside a string However the regexp p = re.compile(r'\d{4}')
3
4716
by: Qxx1 | last post by:
hello everyone i have to code a simple calculator program that performs addition,multiplication,division n substraction operations. The operations are to be performed on 200 digit numbers (i.e 2131237287293.....upto 200 digits) however multiplication and division can be neglected but i need to perform atleast addition and substraction. I am done with the calculator code, but the 200 digit thing is wat i am struck at. How to do it? wat is...
3
1770
by: shalskedar | last post by:
In my Database for 1 of the fields as "ID " i need to display the criteria as 10/2...followed by some characters till 90/2--- for ex-10/2001 11/2008 ---- ---- 90/2005
0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10090
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.